Roland's homepage

My random knot in the Web

Using Matroška containers for videos

date: 2013-11-17
modified: 2015-08-02
reading time: 4 min.
category: howto
tags: matroska video
matroška logo


After using ffmpeg2theora and mkvtoolnix for a while (see also the previous installment) to re-encode videos into Theora video and Vorbis audio tracks encapsulated into the matroška container format, I found an easier way using ffmpeg. This article was updated to reflect that.

An example of the commands used to convert a show from a series DVD is given below; In the following examples > denotes the command interpreter’s prompt.

If the DVD contains a movie, you can find the largest track (the movie) using lsdvd.

> lsdvd /dev/cd1

Dump the track (number 14 in this example) to an mpeg file.

> mpv dvd://14 --dvd-device=/dev/cd1 --stream-dump=inferno.mpg

(Notice that in the meantime I’ve switched to the mpv player.)

Typically, a DVD contains a AC3 audio stream with 5+1 channels. Using ffprobe we can look into the file.

> ffprobe1 inferno.mpg | & sed -e '1,/Input/d'
Duration: 00:41:50.59, start: 0.287267, bitrate: 4774 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p, 720x576 [SAR 64:45 DAR 16:9],
25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x80]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s
Stream #0:2[0x81]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s
Stream #0:3[0x22]: Subtitle: dvd_subtitle
Stream #0:4[0x24]: Subtitle: dvd_subtitle
Stream #0:5[0x21]: Subtitle: dvd_subtitle

From viewing the video I learned that audio track 0:2 is a commentary track, which we’ll discard. The subsequent channels are subtitles which will also be discarded. There might be black bands on the top or size of the image. We don’t want to encode those, so we’ll check if the movie should be cropped.

> ffmpeg -i inferno.mpg -vf cropdetect -y -f avi /dev/null
[cropdetect @ 0x8068c4180] x1:9 x2:712 y1:2 y2:573 w:688 h:560 x:18 y:8 pos:4536334
pts:8160000 t:8.160000 crop=688:560:18:8

When running this command you should wait for the last piece of information crop=688:560:18:8 to stabilze, then terminate the program by pressing ctrl+c. You can use the -ss option to make sure you’re past the intros. Since the cropped size is smaller than the original 720x576, we will apply the cropping when encoding the show.

Now we will convert the movie to Theora video with Vorbis audio, using the standard quality levels. The crop video filter is used to remove black edges around the image. Furthermore the aconvert filter is used to convert the 5.1 audio to plain stereo. Since this movie is in English, I will also discard subtitles.

So the conversion is now done in a single step, which is as follows;

> ffmpeg -i inferno.mpg -c:v libtheora -q:v 6 -vf 'crop=688:560:18:8' \
-c:a libvorbis -q:a 3 -af 'aconvert=fltp:stereo' -sn inferno.mkv

The meanings of the options of this command are

  • -i inferno.mpg: Name the input file.
  • -c:v libtheora: Use libtheora as the video codec.
  • -q:v 6: Set the video quality to 6. This is the standard quality for Theora, but _not_ for ffmpeg, which uses 200 kib/s by default which gives a _very_ low quality image. The number should be in the range 0-10.
  • -vf 'crop=688:560:18:8': Use the cropping video filter.
  • -c:a libvorbis: Use libvorbis as the audio codec.
  • -q:a 3: Set the Vorbis audio quality to 3. This is also a number in the range 0-10. Since 3 is the default quality, this option could be omitted. I’m including it here to be explicit.
  • -af 'aconvert=fltp:stereo': Convert the sound to stereo.
  • -sn: Discard the subtitles.
  • inferno.mkv: Name the output file. The container format is deduced from this.

The file sizes are:

> du -m inferno.m*
313     inferno.mkv
1430    inferno.mpg
> file inferno.m*
inferno.mkv: EBML file, creator matroska
inferno.mpg: MPEG sequence, v2, program multiplex

The conversion ran at around 24 fps without cropping, which is almost real-time using one core. With cropping and sound conversion it runs at around 15 fps.

Original version: 2010-04-25

Related articles

←  Switching to the vim editor Setting the terminal title with urxvt and tcsh  →