Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ffmpeg convert gif to mp4, for best cross browser compatibility
### Full command line options
```
ffmpeg -f gif -i FOO.gif -pix_fmt yuv420p -c:v libx264 -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' BAR.mp4
```
### Notie
* 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.
### Obtain static binary ffmpeg
* http://johnvansickle.com/ffmpeg/
@AlexanderMatveev

This comment has been minimized.

Copy link

@AlexanderMatveev AlexanderMatveev commented Nov 12, 2018

Thanks for the gist! 👍
I have one question: how to fit video to needed size?

@teleworm1337x

This comment has been minimized.

Copy link

@teleworm1337x teleworm1337x commented Aug 23, 2019

hello !
i need to convert a 25 minutes video into multiple gifs, each gif duration shoud be 4 second.
please help

@yuis-ice

This comment has been minimized.

Copy link

@yuis-ice yuis-ice commented Nov 7, 2019

Here is a function to convert all of gif on current directory to mp4.

giftomp4(){
  : copy all of gif on current dir to mp4
  for i in *.gif; do ffmpeg -f gif -i "$i" -pix_fmt yuv420p -c:v libx264 -movflags +faststart -filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2' "$(basename "${i%.*}").mp4" ; done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment