FFmpeg Tips and Tricks

Video stabilisation

FFmpeg can use the vid.stab library to stabilize (i.e. remove shakiness) video. Some guides say it needs to be installed explicitly (vid.stab on Arch Linux, libvidstab* on Debian) but on both my systems it already came as a dependency of ffmpeg.

It requires a 2-pass process.

vid="somevideo.mkv"
ffmpeg -i "$vid" -vf vidstabdetect -f null -
ffmpeg -i "$vid" -vf vidstabtransform "${vid%.*}.stab.${vid##*.}"

The first pass will create a transforms.trf file in the current directory. It happened at ~0.4x speed on an old thinkpad, heating up all 4 cores.
The second pass (at ~0.3x speed) uses it.
In other words, it will take roughly 5-6x the length of the video to stabilise it.

At first I wasn't happy with the results so I increased shakiness for the first pass:

ffmpeg -i "$vid" -vf 'vidstabdetect=shakiness=10:accuracy=15' -f null -

In the second pass I set smoothing=0 which assumes that the camera is static (which is the case in my example video):

ffmpeg -i "$vid" -vf 'vidstabtransform=smoothing=0' "${vid%.*}.stab.${vid##*.}"

Here's a before/after side-by-side comparison:

ffmpeg -i "$vid" -i "${vid%.*}.stab.${vid##*.}" -filter_complex \
"[0:v]crop=iw/2:ih:0:0[left]; [1:v]crop=iw/2:ih:ow:0[right]; [left][right]hstack" \
"${vid%.*}.sxs.${vid##*.}"

Create a looping GIF from a (very short) video

You might want to reduce resolution and framerate drastically:

ffmpeg -i input.mp4 -vf "fps=10,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif

This produces a large file roughly the size of the original (in my case MP4@1920x1080). If you don't mind dithering artifacts, this will result in a file 1/3 the size:

ffmpeg -i input.mp4 -vf "fps=10,scale=640:-1" -loop 0 output.gif

I noticed that the GIF becomes a lot smaller if I first scale the video, then convert it to GIF:

ffmpeg -i input.mp4 -vf "scale=640:-1" input_scaled.mp4
ffmpeg -i input_scaled.mp4 -vf "fps=10" -loop 0 output.gif

Result (video from previous chapter).

Cut a video without re-encoding...

...at keyframes only (forsaking time accuracy), and without messing up timestamps:

ffmpeg -noaccurate_seek -ss [START_TIME] -i [INPUT] -t [DURATION] -c copy -avoid_negative_ts make_zero [OUTPUT]

As always with ffmpeg, the order of the options is relevant. Either -ss [START_TIME] or -t [DURATION] can be omitted.

Encode to 8 bit unsigned PCM WAV Mono

Typically used for game sounds. I had difficulties using the aformat filter, but this syntax worked:

ffmpeg -i speeddown.wav -af "areverse" -ar 22050 -acodec pcm_u8 -ac 1 speedup.wav

It reverses an existing audio snippet.
It would be nice if ffmpeg had an option to transcode a file, but use the same format as the input.

"Fix bad video"

I'm posting this reluctantly because I cannot well explain the problem: sometimes a "bad" video file will play OK, but searching or skipping is impossible. In this case it can help to convert it to an uncompressed raw format like ffvhuff:

ffmpeg -i bad_video.ext -c:v ffvhuff -c:a copy good_video.mkv

I chose MKV because not all containers support this uncompressed format.

This will take orders of magnitude more space then compressed video, but now you can convert it back to something compressed, and it won't be "broken" anymore.

I did not convert the audio here, and it should have no effect on "searchability".

In any case, the ffmpeg wiki has this to say about audio formats, and this SO Q&A has a few more examples.

Configuration?

I could not find out if it's possible to make ffmpeg read some configuration options. It respects a number of envrionment variables, but again, I could not find documentation for this anywhere.

Anyhow, one thing I really do not need is ffmpeg's huge "banner" listing compile options, so I'll at least add an alias to my shell's rc:

alias ffmpeg='ffmpeg -hide_banner'

Online documentation