November 11, 2019 — December 23, 2019

computers are awful
premature optimization

FFMPEG is a multitool for media and metadata. It is handy for video, or extracting audio from video, or whatever other permutation of these ingredients we might wish.

1 Documentation

Documentation is sensible but requires knowledge of the minor implementation details video formats which is one of the most boring domains of human endeavour imaginable, and something that only patent trolls and sometimes engineers are paid enough to care about. Thankfully we have copy-pasta and chatgpt. I fully expect that in the future no-one will use this software except via chatbots.

There is a wiki page for each major supported format, e.g. AAC audio and H.264 video.

There is a dummies’ guide to some extremely basic usecases.

UPDATE: now there is a less esoteric manual by itsfoss.

2 Installing

FFMPEG is conservative in its default install under homebrew, skipping anything that might conceivably infringe upon any patent in any jurisdiction, or anything that sounds like too much effort; I generally do not care about that because I am unlikely to be building anything using ffmpeg that would be liable for patent royalties such as a turkey commerical product. As such I prefer a more capable build:

brew install ffmpeg \
    --with-fdk-aac \
    --with-libsoxr \
    --with-libvorbis \

or even

brew install ffmpeg --with-all

3 Extract audio

If you wish to salvage pure audio for your sampling (up to you to ensure this is legal in your jurisdiction) by getting rid of the video track:

ffmpeg -i mangled_file.m4a -acodec copy -vn plain_audio_file.m4a

4 Trim

ffmpeg -i long_movie.mp4 -ss 36:33 -to 57:36 -c:v copy -c:a copy excerpt.mp4

5 Replace a video soundtrack

Stackoverflow advises on replacing a video soundtrack:

ffmpeg -i v.mp4 -i a.wav -c:v copy -map 0:v:0 -map 1:a:0 -shortest new.mp4

6 Turning outdated camera video into web-friendly video

Using these I have stitched together a workflow for, e.g. converting annoying camera video into something more compact and modern:

You could go for -c:v libx265 for an even fancier codec.

$ for i in 2 3 4 5 6 7 8 9 10 11 12 13; do
> ffmpeg -i filename_$i.avi \
    -c:v libx264 -preset slower -crf 22 \
    -c:a libfdk_aac -vbr 5 \
> done

7 Animated GIF conversion

See Animated GIFs.

8 Convert photos to a movie

Stitch photos to video

ffmpeg -framerate 5 -start_number 1234 -i IMG_%04d.JPG \
    -c:v libx264 -pix_fmt yuv420p -vf scale=1920:-2:flags=lanczos \
    -crf 20 -preset slow -c:a copy ../something.m4v

9 Compressing gigantic smartphone videos into a lower-fi web upload

Depend what you want, but here is a start that meets my needs based on a few different hints I found online

for i in *.MOV;
  do ffmpeg -i "$i" \
    -c:v libx265 \
    -preset slow -crf 23 \
    -filter:v scale=720:-1 \
    -af "highpass=f=150, equalizer=f=50:width_type=h:width=100:g=-15" \
    -c:a aac -strict experimental -b:a 192k "${i%.MOV}-ENCODED.MOV";

I’m also fond of this one because it does not preserve much metadata from your phone video; This is good. Those web people don’t need to know everything about your smartphone.

10 Incoming

Keunwoo Choi shows how to make an animated scrolling spectrogram.

Normalizing is easiest with wrapper script ffmpeg-normalize.

Advanced class: ffmpeg includes a programmatic control from ZeroMQ, so you can dynamically control filters while, e.g. playing video. There are controversies about its implementation.