Video to Gif Command Line Conversion

This is really just a side note for myself. I tend to do something only once in a blue moon, and then consequently forget how to do that again later. So, I’m writing this one down for my own future reference. Hopefully you will find it useful too.

Of course, I am using WordPress for this technical blog. And, because I am “resourceful” (a fancy term for frugal or cheap) I can’t upload videos to my free blog. Maybe someone else has this problem also, and has wondered how to get around it.

Most often, I don’t need a video for the things I post, but occasionally, I can’t show something with just a picture. Thus, I’ve found a good “cheat” is to convert the video into an animated gif file instead.

Previously, I was using online tools, such as ezgif.com, or others. But that takes up an excessive amount of internet, as you upload a video, burning my limited internet allotment (yes, there are still places on the planet where internet is not unlimited). Then you have to download the gif, only to find an issue with the settings, or a tweak you want to do, which starts the process over again.

So, I decided to do some research, and while this is nothing new, here is how I’ve found is a good way to make the conversion on your Linux computer:

~/Downloads$ ffmpeg -i autostart.mp4 -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm – | convert -delay 8 -loop 0 – output.gif

Where:

  • ffmpeg is the program
  • -i autostart.mp4 is the input file video name
  • -vf scale=320:-1 scales the video to a 320 by whatever video to preserve aspect ratio. Note that you can use 640, or whatever scaled size you desire.
  • -r 10 is the frame rate. Most cell phone video is recorded at 30 fps, so choosing 15 would be every other frame, or in this case 10, every third frame.
  • -f format and pipes, redirecting to the convert tool.
  • -delay 8 is the number of milliseconds between frames. This sets the flow and speed of the gif.
  • -loop 0 is the setting for continuous loop, however, most modern browsers use a continuous loop anyways, unless you specify something else.
  • – output.gif is the name of the output file.

The output at the console will look something like this:

~/Downloads$ ffmpeg -i autostart.mp4 -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm – | convert -delay 8 -loop 0 – output.gif
ffmpeg version 2.8.14-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 20160609
configuration: –prefix=/usr –extra-version=0ubuntu0.16.04.1 –build-suffix=-ffmpeg –toolchain=hardened –libdir=/usr/lib/x86_64-linux-gnu –incdir=/usr/include/x86_64-linux-gnu –cc=cc –cxx=g++ –enable-gpl –enable-shared –disable-stripping –disable-decoder=libopenjpeg –disable-decoder=libschroedinger –enable-avresample –enable-avisynth –enable-gnutls –enable-ladspa –enable-libass –enable-libbluray –enable-libbs2b –enable-libcaca –enable-libcdio –enable-libflite –enable-libfontconfig –enable-libfreetype –enable-libfribidi –enable-libgme –enable-libgsm –enable-libmodplug –enable-libmp3lame –enable-libopenjpeg –enable-libopus –enable-libpulse –enable-librtmp –enable-libschroedinger –enable-libshine –enable-libsnappy –enable-libsoxr –enable-libspeex –enable-libssh –enable-libtheora –enable-libtwolame –enable-libvorbis –enable-libvpx –enable-libwavpack –enable-libwebp –enable-libx265 –enable-libxvid –enable-libzvbi –enable-openal –enable-opengl –enable-x11grab –enable-libdc1394 –enable-libiec61883 –enable-libzmq –enable-frei0r –enable-libx264 –enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘autostart.mp4’:
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2018-04-20 14:05:43
Duration: 00:00:25.96, start: 0.000000, bitrate: 6103 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1280×720, 6448 kb/s, SAR 1:1 DAR 16:9, 29.88 fps, 30 tbr, 90k tbn, 180k tbc (default)
Metadata:
rotate : 90
creation_time : 2018-04-20 14:05:43
handler_name : VideoHandle
Side data:
displaymatrix: rotation of -90.00 degrees
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 48 kb/s (default)
Metadata:
creation_time : 2018-04-20 14:05:43
handler_name : SoundHandle
Output #0, image2pipe, to ‘pipe:’:

Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
encoder : Lavf56.40.101
Stream #0:0(eng): Video: ppm, rgb24, 320×569 [SAR 5121:5120 DAR 9:16], q=2-31, 200 kb/s, 10 fps, 10 tbn, 10 tbc (default)
Metadata:
handler_name : VideoHandle
creation_time : 2018-04-20 14:05:43
encoder : Lavc56.60.100 ppm
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> ppm (native))
Press [q] to stop, [?] for help
frame= 39 fps=0.0 q=-0.0 size= 20805kB time=00:00:03.90 bitrate=43700.4kbitsframe= 80 fps= 80 q=-0.0 size= 42676kB time=00:00:08.00 bitrate=43700.4kbitsframe= 121 fps= 80 q=-0.0 size= 64548kB time=00:00:12.10 bitrate=43700.4kbitsframe= 159 fps= 79 q=-0.0 size= 84819kB time=00:00:15.90 bitrate=43700.4kbitsframe= 200 fps= 80 q=-0.0 size= 106690kB time=00:00:20.00 bitrate=43700.4kbitsframe= 241 fps= 80 q=-0.0 size= 128562kB time=00:00:24.10 bitrate=43700.4kbitsframe= 245 fps= 80 q=-0.0 Lsize= 130696kB time=00:00:24.50 bitrate=43700.4kbits/s dup=0 drop=483
video:130696kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
~/Downloads$

Hopefully that is a tidy explanation. Keep in mind, the higher the -vf, the bigger the gif appears, but also the larger the file. The -r value will make the gif smoother as it goes up, but will also increase the gif size in MB!

Linux – keep it simple.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s