ffmpeg Recipes

I use the almighty ffmpeg a lot to convert, edit, and tag my personal media. This is a living document of my most used command line recipes.

Setting Metadata

ffmpeg -i in.mkv -map 0 \
  -metadata title="…" -metadata:s:v:0 title="…" \
  -metadata:s:a:0 title="English" -metadata:s:a:0 language="en" \
  -metadata:s:a:1 title="Deutsch" -metadata:s:a:1 language="de" \
  -metadata:s:s:0 title="Deutsch" -metadata:s:s:0 language="de" \
  -c copy out.mkv

Trim/Cut/Clip Video With Timestamps

ffmpeg -i in.mkv \
  -ss 00:01:00 -to 00:02:00 \
  out.mkv

Note that using -c copy would again be faster, but then clipping relies on keyframes at the targeted start and end time and might be less accurate. Re-encode for higher accuracy.

Extract Frames as Images

It sometimes happens that galleries or storyboard sketches in video format only play for less than a second because each still image is just one frame. I then prefer extracting these frames as images and store them as a PDF.

Naïve Approach

ffmpeg -i in.mp4 out%04d.png

Better Approach

If each still image occupies more than one frame, this approach is superior because it only selects frames that are different from their predecessor.

ffmpeg -i in.mp4 \
  -filter_complex "select=gt(scene\,0.01)" \
  -fps_mode passthrough \
  out%04d.png

Via Chapters

If still images are marked by chapters, capturing the first frame of their respective start times can be a good approach.

ffprobe -i in.mkv -v error  -print_format json -show_chapters | \
  jq ".chapters[] | .start_time" | \
  xargs -I{} ffmpeg -i in.mkv -v error -ss {} -frames 1 out-{}.png

Modifying Chapter Data

This is a two-stage process: I extract the metadata into a txt file, change the chapter’s titles, and mux the altered metadata into a new file.

ffmpeg -i in.mkv -f ffmetadata metadata.txt
vim metadata.txt
ffmpeg -i in.mkv -i metadata.txt \
  -map_metadata 1 -map_chapters 1 \
  -c copy out.mkv