Title: Howto: Create a high quality Matroska video with the x264 codec and vorbis/flac
Author: Benjamin Weber
License: (C) 2007 Benjamin Weber. This document is licensed under the terms of the
GNU General Public LicenseDate: So 5. Aug 19:05:50 CEST 2007
Source: The current version of this document is located at
http://howto.builtin.de/mkv_howto.html.
The problemMost users have the best movies in a mpeg file but this file needs a lot of space. How we can get the best result as a high quality video?
Which video codec?There are a lot of projects like DivX which wants to get a "good" quality with the smallest files. But if you decrease the file size the quality will get worse. You will have to get the right measure of compression with the right codec configuration. The
Xvid Codec is a good codec but if you see a
comparison of Xvid with
x264, you will choose x264 as your default codec. The codec is
still in development but you see what the possibilities of the current version are. Another advantage is the platform independence so you are not bound to any OS. We will use x264 instead of Xvid in this howto because of the better video quality.
Which audio codec?In this category you will find a lot of proprietary codecs like MPEG Layer 3 Codec (MP3) or Advanced Audio Coding (AAC). Especially the last one is usually used for combining with video codecs. But you have to use proprietary programs which are licensed and cost money. But why always a proprietary codec? There are enough codecs which you can use. One very popular is the Vorbis Codec. You can compare it with the MP3 codec and Vorbis has a better effectiveness than MP3. This codecs compress the audio data through cutting out of a lot of frequencies we humans can't hear. But the codec doesn't now exactly what we can and what we can't hear. So you have a drop in the quality. This codecs are called lossy audio codecs.
The opposite of the lossy audio codecs are the free lossless audio codecs. Here are only a few codecs, some proprietary and one free codec. If you want a proprietary codec then use iTunes, Real Player or whatever but if you want to do what you want, then you should use the Free Loseless Audio Codec (FLAC). It is open source and you can use it on most platforms. By the way: anywhere found a Linux version of the real iTunes?
How to put all good things together?Audio and Video? But how to put different things together in one file? Subtitles, many audio languages, chapters like on a real DVD? This question the
*Matroska* project was addicted to. This format (we will call it container format in the next time) has the most possibilities to put everything, and yes I really mean everything, together. You can choose your subtitle, in your language, and your language as audio stream. And Chapters are also possible. There are a few other free projects which set this as their goal but believe me, Matroska is the best. Are there any proprietary formats which can do that? Everyone tells that MP4 is the future but I don't understand why? Proprietary and only video and audio, very boring. If you found a better container format, please inform me! The Matroska format is very exotic and very unsupported (dvd players, software, etc.) but on computers it will work with the
*VLC* player,
*MPlayer* and a few other good players.
Needed PackagesYou can use the current development versions of this tools but we will do that for now in the easiest way:
apt-get install x264 mjpegtools mkvtoolnix mkvtoolnix-gui avidemux flac vorbis-tools
We need the tool Project X in this Howto. Download the actual version from
Doom9, extract it and make sure that Java is installed.
Demuxultiplexing and cutting of the MPEG TS fileNow we need a file to convert. Usually TV recordings are in the MPEG Transport Stream (TS) format what means the video and audio data are together in one stream. Cutting this raw file is not possible. So we need to release the being of together of the video and audio (Multiplexing). That we need is a Demultiplexer. The best tool for that is the Java program Project X. There are a lot of other programs but Project X directly corrects bad frames and attempts to fix the audio and video synchronisation.
Make a small start icon or script which starts like the following command line:
java -jar /path/to/your/ProjectX.jar
Start Project X with this command line (or your start button). If necessary select your prefered language and restart it. Now give Project X your MPEG TS file when you choose it through the File -> Add... dialog.
Then use the + button to select a selection which is your main video (without commercials etc.). Your output video will be everything what is indexed by the green line. The red line indexes what will be cut out.
After selected in the prepare ... menu demux press the play button. Then Project X will start the cutting and demultiplexing process. You will get a .m2v and a .mp2 file, where the M2V file only the video and the MP2 only the audio contains.
Convert the audio dataGet the raw audio data
First we need the raw audio data. This is quite easy to get:
mpg321 -w outputfile.wav inputfile.mp2
Then we have to choose our audio codec. Vorbis or Flac.
Encode audio with VorbisFor Vorbis do it like here:
oggenc -q 6 inputfile.wav
As output file we have now a vorbis encoded file. To select the quality of your output file you can chosse instead of 5 any other value between 1 and 10 while 10 means the best quality. Please keep in mind that more quality means the file needs more space.
Encode audio with Flac
This is similar to the previous process:
flac --best inputfile.wav
The only difference is that a better quality needs more CPU power and the space doesn't differ a lot. The reason for that is the loseless encoding of the Flac encoder.
Remove channel logo, deinterlace and other nice featuresAvidemux has a lot of possibilites. First we open your video file (.m2v). Then leave Avidemux index the video file.
Video codec preferencesNow choose x264 as video codec and select in the Audio menu as your Main Track "None. This is the point where you can adjust your encoder preferences.
For best results (it needs a lot of time to encode, depends on what CPU you are using.) here is a small script with my default options:
//AD <- Needed to identify//
var app = new Avidemux();
//** Video Codec conf **
app.video.codec("X264","2PASSBITRATE=3000","160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
28 00 00 00 1e 00 00 00 3c 00 00 00 0a 00 00 00 33 00 00 00 04 00 00 00 02 00 00 00 28 00 00 00
19 00 00 00 fa 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00
02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 06 00 00 00
10 00 00 00 02 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00
02 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 33 00 00 00 00 00 00 00 ");
//** Audio **
app.audio.reset();
app.audio.load("NONE","");
app.audio.codec("copy",128,0,"");
app.audio.normalizeMode=0;
app.audio.normalizeValue=0;
app.audio.delay=0;
app.audio.mixer("NONE");
app.setContainer("MP4");
//app.Exit();
//End of script
You can save that as any file and place it in ~/.avidemux/custom. After that you can load your default options if you select in the Custom menu your file.
Using filtersMost TV channels have their own logo. There is a filter which can attempt to remove the logo. If you like, then you can add this filter and adjust the position. The Deinterlacing filter is a nice filter which I recommend. There are really a lot of filters which you can use and as many as you want.
The filters are used during the whole (or if you want only in parts of your video file, then only in this selected sections) encoding process. The order of the filters is important for your end result. As an example you should use the filters in the following order (often used filters):
* Deinterlacing filters
* Delogo filters
* Denoise filters
* Sharpen (if necessary) filters
* Crop filters
* Resize filters
For other filters the best order you should find out by Learning by Doing and use your brain. The Deinterlacing should be done as first step because the other filters would screw up the interlacing. More experiences and stuff concerning the filters you will find in the
Doom9 Forums.
Put the encoded video and audio data togetherAs in the first chapter explained I will describe the process with the container format Matroska.
* Start the tool mmg on the command line.
* Use the add button to add your encoded video and audio files.
* If you have other audio languages, subtitles or chapters then you can add them in the GUI of mmg at this point.
* Set in the Global section all necessary preferences.
* Check the output file name and change it if necessary.
* Now start the muxing process with a click on the Start muxing button.
You should have as the result of this howto a mkv file.