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?
Yet Another Avconv Bug (YAAB)
ffmpeg
has better GIF demuxing support (and improved GIF encoding). I recommend ditchingavconv
and gettingffmpeg
(the real one from FFmpeg; not the old charlatan from Libav). A static build is easy, or you can of course compile.Example
See the FFmpeg Wiki: H.264 Encoding Guide for more examples.
If for some reason you are required to use avconv and imagemagick, you may want to try something like this:
So, get the ticks in centiseconds for each frame of the gif (via identify), convert to multiple pngs, and then go through them while making duplicates based on the tick values. And don't you worry, the png files will still remain in consecutive order. Using the real FFmpeg is still the best way to go.