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).

Ripping a DVD

This one’s a bit complicated as FFmpeg does not (yet) have a DVD reader/demuxer based on libdvdnav, so we’re going to use the media player mpv to help us out with that part. First, make sure that mpv can play the DVD correctly. You’ll need either an ISO file, an ISO extracted to a directory, or a physical disc. A physical disc might be encrypted, in which case, you’ll also need libdvdcss. Much of this will come built-in with mpv, if it’s compiled in. If not, you might need to build it yourself.

In either, case, you’ll want to do this to check out that the DVD plays correctly:

mpv dvd:// --dvd-device=MY_DVD_ISO.iso

If you have a physical disc, you should instead point it to the location of the disc, such as /dev/sr0, or something like E: if you are on Windows, wherever it happens to be.

Once you confirmed that the DVD is playable, you just need to use the --stream-dump option:

mpv dvd:// --dvd-device=MY_DVD_ISO.iso --stream-dump=my-dvd-dump.mpg

This writes an MPEG-PS file, which you can then remux to a matroska file using either ffmpeg or mkvmerge:

ffmpeg -i my-dvd-dump.mpg -map 0 -c copy dump.mkv
mkvmerge -o dump.mkv my-dvd-dump.mpg

What about chapters? This will not write the chapters to the matroska file, because those are read directly from the library, not from the program stream that the library provides. However, there’s an mpv script: createchapter.lua. Place this script in your mpv scripts directory, which is ~/.config/mpv/scripts/ on Linux and macOS, and in %APPDATA%\scripts\mpv\ on Windows.

Once you’ve placed the script, play the DVD again, and then press Shift+B. It will create an XML file that contains Matroska chapter data. Now, you just need to add the chapter information to the matrosksa file with mkvmerge, which can be done like so:

mkvmerge --chapters createchapter.xml -o final.mkv dump.mkv

And there you have the DVD remuxed into a matroska file, with chapters.