FFmpeg Tips and Tricks

Table of Contents

More will be added periodically as I remember to add them.

Dumping Attachments from Matroska Files

ffmpeg -y -v quiet -dump_attachment:t "" -i input.mkv

The -y -v quiet is not necessary but it prevents spam output if you script this, and automatically overwrites existing files.

Extracting Audio Streams

ffmpeg -i input.mkv -map a:X -c copy output.mka

Here, X is not to be taken literally but rather is the ID of the audio stream you wish to extract. It counts upward from 0 (not 1) and only counts audio streams (not video or subtitle).

Extracting Subtitle Streams

ffmpeg -i input.mkv -map s:X -c copy output.ass

Here, X is not to be taken literally but rather is the ID of the subtitle stream you wish to extract. It counts upward from 0 (not 1) and only counts subtitle streams (not video or audio).

Shifting Subtitle Streams

To add a fixed amount of subtitle delay to a subtitle file so the timestamps are all shifted, you can abuse ffmpeg's timestamp manipulation.

ffmpeg -itsoffset AMOUNT_OF_DELAY_TO_ADD -i input.ass output.ass

The timestamp AMOUNT_OF_DELAY_TO_ADD can be specified as a floating-point number of seconds, or also as MM:SS or HH:MM:SS. Basically if it makes sense as a time code then FFmpeg will interpret it correctly. (See ffmpeg-utils(1) for a rigorous description of time durations.) AMOUNT_OF_DELAY_TO_ADD can be either positive or negative. Positive shifts subtitles toward the end of the video, and negative will shift subtitles toward the start of the video.

Remuxing a BDMV

You can remux a BDMV provided that FFmpeg has been compiled with libbluray support.

ffmpeg -i bluray:"path/to/bluray/"

If the above recognizes the bluray, it will list the streams. Make sure you use the path to the directoring containing BDMV/ and not the BDMV/ itself.

This will work: ffmpeg -i bluray:paradox-spiral/This will not work: ffmpeg -i bluray:paradox-sprial/BDMV/

It's worth mentioning that you cannot truly remux to matroska since blurays use bluray_pcm as their audio codec and matroska doesn't support that. However, you can convert it safely to FLAC and not only will nobody care that it's not a "true remux" but the space saved is a bonus. Anyway, here's the command in full:

ffmpeg -i bluray:"path/to/bluray/" -map 0 -c copy -c:a flac remuxed-bluray.mkv

You should see some output like this. Note that this will be slow if you're going from one hard disk drive to the same one because the readhead will have to zigzag constantly, tho this is improved with a good filesystem and driver. Meaning, it should be faster on Linux than on Windows.

As for what the options do, -map 0 tells it to transfer every stream from input number 0 (i.e. the only input), since by default FFmpeg transfers the first video and audio stream only (I'm not sure about subtitles). -c copy tells it to streamcopy the streams without re-encoding them (what we want) but -c:a flac tells it to re-encode audio streams to FLAC before muxing. This occurs later on the command line so it trumps -c copy for the streams it applies to (order matters).