Video Containers and Encoding: Good compression

I dislike `media' that one has to touch. Much better that the entire collection of audio and video should, along with home-directories, be safely on a redundant disk array somewhere in the cupboard, available to all the computers in the house or even to remote places if I'm travelling. Even DVDs can take up whole shelves easily, and as for VHS videos, ...

I therefore started many years ago ripping all such media and storing the result on harddisks. I of course wanted to improve the compression, since DVDs or even CDs would fill up the disks rather quickly otherwise.

I rip CDs to ~160kbps Ogg-vorbis audio, using oggenc -q5 -o outfile.ogg infile.wav , usually with grip.

I used to use mencoder to recode videos in one pass, using libavcodec to convert the audio to VBR mp3, and the video to 'vhq' mpeg4, with the following command:

mencoder "$infile" -noskip  -o "$outfile" -vf 480:360 -af volnorm=1 \
          -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:v4mv:vhq=4:vbitrate=450 \
          -oac mp3lame -lameopts vbr=3:br=96:aq=0

This gives a 480x360 pixel output, with 450kbps video and 96kbps audio. Hardly needful to say, with those bitrates, the quality was pretty bad with any significant movement of the picture. I used this because it was acceptable (then) at a normal viewing distance, and I really wanted the space, back in days when disks weren't quite as cheap as now.

Then, I discovered how very much better the quality could be from use of the H.264 video codec and multi-pass encoding (when the encoder goes through the video at least twice, checking which parts most need attention). I've not tried the libavcodec mpeg4 encoder in multi-pass mode, so perhaps a lot of the improvement is due to multi-pass rather than to H.264; but, from what I've read about it, H.264 is rather good anyway -- so I've kept using it.

Having practised this video encoding to get good commandlines and ideas of suitable bitrates, I sought a container format (e.g. AVI, Ogg, Matroska, which specify a way to combine video and audio, and perhaps more, into a single file, along with information about the required codecs). Matroska seemed the most thorough, since it allows pretty much anything to be included: multiple video, audio and subtitle streams, along with attached images, text, etc.

I wrote a script to take an existing (e.g. AVI) file or DVD, and to do a two-pass H.264 video encoding and a vorbis audio encoding, then combine them in a Matroska container; it was much based on things in a gentoo wiki. This was alright except for certain AVI files, from which the audio/video sync became really messed up after conversion. More than a year later I made a concerted effort to get it working, which was acheived by specifying an output video rate (-ofps 25)!

So, finally, I had a reliable way of getting pretty much any video source into a single format, with the potential for including multiple selectable subtitles, multiple audio streams, etc. This is very neat compared to separate subtitle files, and is much preferable to the profusion of codecs that one otherwise tends to end up with (remember: containers like AVI may contain stream in any of quite a few different a/v codecs' formats). Even just muxing an existing AVI directly into the matroska container was worth doing, as it gets a bit smaller (the AVI container wastes space), but I chose to recode everything. Some cartoons could be got down to as low as 150 or 200 kbps video without significant deterioration. Good DVDs were thoroughly acceptable at about 500 kbps video. Audio from oggenc quality 1 up to 5 was used, depending on the original quality.

I'm still horrified by how outdated many peoples' encodings are: people seem to think that a 200 MB file of a half-hour video will be bad quality -- and it is the way they often do it, with single-pass pathetic codecs -- but it could instead be about 150 MB and scarcely distinguishable from a DVD in spite of including all of, say, two audio tracks (e.g. two languages) and five subtitle streams. Particularly if one has scripted the transcoding, and can put multiple jobs in a loop, the extra time usually associated with better quality is not very important since it happens in the background (although increases in computer power consumption may bother you).

Here is the script: vid3mkv.sh that I ended up with, to do the most general case of conversion of video sources to a matroska file. The name is derived from the original `[general] video to matroska', but as this script uses three-pass encoding (for slightly better quality, they say -- just take out the middle pass and change pass-number to get it down to two) I bumped up the middle `2'. It may be useful to add -fps 25 (or some other rate) besides the output rate -ofps 25, e.g. to get certain weird .flv things ok. The script is run like this:

    ./vid3mkv.sh   INPUTSOURCE  outname  vid_kbps  aud_qual

As it stands, it will just deal with video and the first audio track (which it will call English). Modify the aud_list and/or sub_list variables, either in the file or in the environment, to cause other available streams to be included; aud_list takes one two-letter language name per line, and sub_list takes one subtitle-number:subtitle3lang pair per line, e.g.

export aud_list='en
sv'
export sub_list='1:eng
5:swe'
./vid3mkv.sh  '-dvd-device /dev/dvd dvd://3' newvideo  500 4
The available subtitles can be seen from mplayer's text output to a terminal when playing the track. Players such as VLC (GUI) and mplayer (keyboard commands) can switch between different audio/subtitle streams in the mkv file.

Page started: 2008-08-19
Last change: 2010-07-18