ffmpeg recipes
You can find the ffmpeg
documentation here.
I have it installed on macOS with brew install ffmpeg
. For best results, always keep ffmpeg
up to date.
Compress a screencast
Full-screen screencasts recorded with QuickTime tend to be on the large side. We can use ffmpeg
to obtain a smaller file size by simply converting it to MP4:
ffmpeg -i my-recording.mov my-recording.mp4
(For example, it brought a 40-second screencast from 38MB down to 4.5MB)
Screencast to GIF
There are many ways to turn a video into a GIF with ffmpeg
and other free tools.
The straightforward way is to:
ffmpeg -i my-recording.mov -pix_fmt rgb24 output.gif
Note that recent versions of
ffmpeg
, such as thev4.2.2
I'm on at the time of writing, can convert a video to a GIF without having to specify-pix_fmt rgb24
.
A few useful parameters to include:
-r
controls the framerate of the GIF-s
controls the dimensions of the GIF
The example below outputs a 320 × 240 GIF with 10 frames per second:
ffmpeg -i my-recording.mov -r 10 -s 320x240 output.gif
The -s
parameter distorts the input video to fit the specified size. If instead you want to maintain the original aspect ratio, use the scale
filter, to which -s
is actually a shortcut, directly:
ffmpeg -i my-recording.mov -vf scale=320:-1 output.gif
Use -1
as auto
when specifying the scale.
To speed up the video, use the setpts
filter:
ffmpeg -i my-recording.mov -filter:v "setpts=0.5*PTS" my-recording-sped-up.mp4
The command above makes an MP4 video at twice the speed of the original MOV. For some reason I couldn't apply the setpts
filter in a direct MOV to GIF pipeline, so I'm first making a sped-up MP4, then converting that to a GIF.
This method tends to output poorer-quality GIFs. A more complicated solution is explained in this answer on the Super User StackExchange, along with an article detailing how to obtain better-looking GIFs from ffmpeg
.
This gist walks you through creating a GIF with ffmpeg
and gifsicle
.
See also this article from GIPHY Engineering.
Loop a video N times
The -stream_loop N
option lets you loop (concatenate) the input video N times. In the example below, we generate output.mp4
which is twenty times the length of input.mp4
.
ffmpeg -stream_loop 20 -i input.mp4 -c copy output.mp4
GIF to WebM / MP4
See: Replace animated GIFs with video for faster page loads by Houssein Djirdeh and Jeremy Wagner.
Gotchas
ffmpeg
in a Bash loop generates errors
Use ffmpeg -nostdin
to prevent ffmpeg
from exhausting stdin
. See: BashFAQ#089.