Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert animated gif to video on linux server while preserving frame rate

how do I convert an animated gif to a video (e.g. h264@mp4) programmatically on a linux server?

I need this to process user generated content which should be output as several defined video formats; therefore its possible, that users may want to process animated gif files. I already have a set of working php scripts to transcode videofiles to specific formats (like vpx@webm and h264@mp4, scaled to specific resolutions) using avconv, but herefore I need video input.

Usual ways seem to be to extract the frames of the gif and then encode it, like

convert file.gif file%03d.png 
avconv -i file%03d.png file.mp4

But this discards the frame rate, determined by the pause-informations within the gif-file. Its possible to define a framerate to avconv with -r, but

  • this does not respect the pause between frames, as they can differ (like 1st frame 100ms pause, 2nd frame 250ms pause, 3rd frame 100ms pause, ...)
  • as the input comes from users, it may even vary, as some gifs may have 5fps and others 30fps

I noticed that avconv is able to process gifs by itself and therefore may respect the correct pauses, but when I do (like similarily described in How to convert GIF to Mp4 is it possible?)

avconv -i file.gif -r 30 file.mp4

avconv will only take the first frame of the gif, while it detects the file at least as video:

Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
  Stream #0.0: Video: gif, pal8, 640x480, 25 tbn

(example gif 'file.gif' has 15 frames, each with 100ms pause => 1.5s duration, looping)

  • What am I missing? Whats going wrong?
  • Are there probably better tools for this use case?
  • What are big sites like e.g. 9gag using to transcode uploaded gifs to video?
like image 343
pmedia Avatar asked Apr 10 '15 12:04

pmedia


1 Answers

Yet Another Avconv Bug (YAAB)

ffmpeg has better GIF demuxing support (and improved GIF encoding). I recommend ditching avconv and getting ffmpeg (the real one from FFmpeg; not the old charlatan from Libav). A static build is easy, or you can of course compile.

Example

ffmpeg -i in.gif -c:v libx264 -pix_fmt yuv420p -movflags +faststart out.mp4

See the FFmpeg Wiki: H.264 Encoding Guide for more examples.

like image 113
llogan Avatar answered Nov 14 '22 08:11

llogan