Skip to content

Instantly share code, notes, and snippets.

@gvoze32
Last active November 11, 2024 03:37
Show Gist options
  • Save gvoze32/95f96992a443e73c4794c342a44e0811 to your computer and use it in GitHub Desktop.
Save gvoze32/95f96992a443e73c4794c342a44e0811 to your computer and use it in GitHub Desktop.
Convert animated GIF to MP4 using ffmpeg in terminal.

To convert animation GIF to MP4 by ffmpeg, use the following command

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

Description

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.

Note

Add "-r 30" to specify the frame rate 30 frames/sec. So if you want 10 sec movie with frame rate 30/sec, you make GIF animation that has total 300 frames, then use it.

Features

  • Output mp4 is encoded with h264, support Firefox/Chrome/Safari in Windows, Mac OSX, Android, and iOS.
  • One mp4 file for all platforms, there is no need to encode an extra "webm" movie, which encoding speed is pretty slow.
  • Format as "yuv420p" for Firefox compatibility, the downside is color becomes less-saturate than original gif.
  • yuv420p only support even width/height, so crop filter is required
  • "-movflags +faststart" flags are optimized for online view in browser
  • Compression ratio typically 10:1, pretty awesome. note that if original gif is < 512KB, convert as mp4 is less efficient.
@ninapaley
Copy link

Please note that all the gifs are 24fps native. So to avoid stutter the mp4s should maybe be 24fps also? @toolness

@Dhv-0302
Copy link

Dhv-0302 commented Jul 31, 2023

what for converting gif to mp4 with transparent background ?

@emory
Copy link

emory commented Dec 11, 2023

what for converting gif to mp4 with transparent background ?

can you provide an example of an mpeg4 video with a transparent background? or are you asking if you can specify a background fill?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment