Schnee (schnee) wrote,
Schnee
schnee

Using ffmpeg to convert MP4s to GIFs

"GIF" has become a bit of a misnomer in recent years, with various services referring to any animated image as a GIF even when it's not in GIF format (Telegram, looking at you) — if you find yourself having saved a "GIF" that's really an MP4 and you want to convert it to an actual GIF, ffmpeg can help you, assuming you know how to use it:

$ ffmpeg -i input.mp4 -vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif
ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.1 (GCC) 20200523
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
  Duration: 00:00:05.76, start: 0.000000, bitrate: 237 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 480x360 [SAR 1:1 DAR 4:3], 235 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> gif (native))
Press [q] to stop, [?] for help
Output #0, gif, to 'output.gif':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: gif, pal8, 480x360 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 25 fps, 100 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.54.100 gif
[Parsed_palettegen_1 @ 0000000004839f00] 255(+1) colors generated out of 1322 colors; ratio=0.192890
[Parsed_palettegen_1 @ 0000000004839f00] Dupped color: FF1A211C
[Parsed_palettegen_1 @ 0000000004839f00] Dupped color: FF5B625B
[Parsed_palettegen_1 @ 0000000004839f00] Dupped color: FF646965
frame=  144 fps=133 q=-0.0 Lsize=    4653kB time=00:00:05.73 bitrate=6652.5kbits/s speed=5.31x
video:4653kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000420%
$ 

The key thing here are the video filters (-vf). You (probably) want an optimal palette for your GIF; ffmpeg can both generate and use palettes, but you'll have to use a filter network: split creates two copies of the input, one of which (s0) is fed to palettegen; the other copy (s1) and the palette produced by palettegen (p) are fed into paletteuse (which, for obvious reasons, needs both a movie and a palette to use).

Whew!

Tags: animations, ffmpeg, gif, useful stuff, videos
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments