182

What command would I use to convert an mp4 or mv4 video file to an animated gif, and vice versa. That is, convert a animated gif to a mp4 or mv4.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175

5 Answers5

257

Here's what worked for me:

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4

movflags – This option optimizes the structure of the MP4 file so the browser can load it as quickly as possible.

pix_fmt – MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers.

vf – MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case.

Source: http://rigor.com/blog/2015/12/optimizing-animated-gifs-with-html5-video

d0n.xyz
  • 2,671
  • 1
  • 9
  • 2
  • 5
    Thanks this is the only version that worked for me on osx. – Pykler Jul 14 '16 at 23:52
  • 2
    This worked for me on Ubuntu 16.10 with ffmpeg 3.0.2, where the top answer didn't – cat Oct 23 '16 at 22:54
  • 5
    May as well crop instead of scaling (replace `scale` with `crop`), as you're only going to be cutting off at most 1 pixel. Scaling might make things blurry – Jezzamon Feb 01 '18 at 08:12
  • This cut off the last few seconds for me. Resulting mp4 ended early. – callum Feb 22 '18 at 13:06
  • What's the reasoning behind truncating, dividing and multiplying? How does this ensure "divisible by 2"-ness, and why does trunc alone not suffice? — Also: as far as I know, you can just write `"trunc(iw/2)*2:-2"` and the height automatically becomes divisible by 2 while respecting the aspect ratio. (Except: `crop` does not accept negative values …) – WoodrowShigeru Feb 12 '21 at 10:25
  • Never mind, I figured out why it's necessary. – WoodrowShigeru Feb 12 '21 at 10:44
  • This should be the accepted answer! – Melroy van den Berg Sep 30 '22 at 14:11
57

In my case, using ffmpeg directly did the trick and provided the best result:

$ ffmpeg -f gif -i infile.gif outfile.mp4
BenC
  • 1,081
  • 8
  • 11
  • 9
    produced a blank (corrupted) video on os x – Pykler Jul 14 '16 at 23:53
  • 2
    @Pykler you should probably increase verbosity and/or check the log file to understand what's happening. – BenC Jul 17 '16 at 19:02
  • 2
    Me too, probably the "divisible by 2 dimensions" suggested in vico Vault's answer. (which worked for me) – lapo Oct 24 '16 at 11:20
  • This method worked perfectly for me. (The top answer caused 3 seconds to be cut off the end.) EDIT: oh but it doesn't play on iOS :( – callum Feb 22 '18 at 13:07
  • A [web page](https://apple.stackexchange.com/questions/166553/why-wont-video-from-ffmpeg-show-in-quicktime-imovie-or-quick-preview) says you should just add -pix_fmt yuv420p – beefeather Aug 20 '18 at 16:06
  • This worked for me on Debian stable. – rootkea Jun 01 '19 at 12:20
  • This showing `1 file you tried adding is not supported.` error if upload to WhatsApp. Need `-pix_fmt yuv420p` and also `-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"`(to fix `not divisible by 2` error) to work. – 林果皞 Dec 20 '19 at 15:51
  • If video corrupted, try [this answer](https://unix.stackexchange.com/a/463675/409331). – young_souvlaki Sep 07 '21 at 17:54
14

A side note: the important thing is to specify -pix_fmt yuv420p lest your video might seem a black rectangle in various tools:

$ ffmpeg -i animated.gif -pix_fmt yuv420p output.mp4

References

beefeather
  • 241
  • 2
  • 3
  • Not just Apple, but also Windows media player sometimes can't play the output if you don't specify that option. And if you try playing it with other players, it would be a comeple gray screen with artifacts showing on video. – Shayan Dec 01 '19 at 13:28
  • 1
    I get `height not divisible by 2 (260x373)` and `Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height` error message. But [this answer](https://unix.stackexchange.com/a/294892/64403) works for me. – 林果皞 Dec 20 '19 at 15:36
  • 1
    observation - isn't the OP about taking animated gif as input (and/or output)? your answer has no gif as input or output – nhed Jul 24 '20 at 03:31
  • 1
    @nhed thanks, I slightly reworded it to put it back into the context – beefeather Jul 28 '20 at 12:14
9

If you want to make the output in "n loops", look at this solution in one shot

So, let's convert a normal.gif to loop.mp4

for 2 loops movie example:

ffmpeg -stream_loop 2 -i normal.gif loop.gif -y;ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4 -y

for 5 loops movie example:

ffmpeg -stream_loop 5 -i my.gif loop.gif -y;ffmpeg -i loop.gif -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" loop.mp4 -y

be aware: There's a -y option, so the output files will be overwritten

the -vf option is to resolve the ratio proportionality [width vs height]

PYK
  • 201
  • 2
  • 4
  • If you run the first `ffmpeg` command **without** `-y`, you'll have much less chance of accidentally overwriting something you don't want to. (There's also _probably_ a way to get `ffmpeg` to pipe video into itself, and do this in one piped command without the intermediate file and the overwrite.) – FeRD Sep 18 '19 at 19:15
4

Another way to convert GIF animation to video:

ffmpeg -i your_gif.gif -c:v libvpx -crf 12 -b:v 500K output.mp4

-crf values can go from 4 to 63. Lower values mean better quality. -b:v is the maximum allowed bitrate. Higher means better quality.

whitewings
  • 2,377
  • 7
  • 32
  • 53