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
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With