DVD to streamable mp4 on Ubuntu 9.04
This guide is intended to explain how to encode a DVD to a streamable mp4 codec under Ubuntu with ffmpeg.
The first thing you need to do is get the dependencies, you can do this with the following command;
sudo apt-get install build-essential dvdrip subversion avidemux libfaac-dev libspeex-dev libgsm-dev libgsm1-dev libmp3lame-dev libtheora-dev libvorbis-dev libx264-dev libxvidcore4-dev
Or you can click on this apt link, which will install these dependencies for you;
Set up Programs
This part will require the terminal, to get to the terminal;
Applications > Accessories > Terminal
Then issue the following commands;
cd Documents ; mkdir src ; cd src
svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
This will download a snapshot of ffmpeg, in the same terminal do the following to configure and install ffmpeg;
./configure --extra-cflags=-fno-common --enable-memalign-hack --enable-pthreads --enable-libmp3lame --enable-libxvid --enable-libvorbis --enable-libtheora --enable-libspeex --enable-libfaac --enable-libgsm --enable-libx264 --enable-gpl --enable-nonfree
gcc qt-faststart.c -o qt-faststart
sudo cp qt-faststart /usr/local/bin/
We need to set up protected DVD decryption Ubuntu makes this pretty easy, do the following commands to set it up;
sudo sh install-css.sh
Ripping the DVD
Applications > Sound & Video > dvd::rip
or “dvdrip” from cli.
When You first start dvdrip it will ask you what Directories to use for the projects, It’s up to you which directory to use. Once you have done that you will need to put a DVD into your DVD drive and start a new project. To start a new project go to “File > New Project” in dvdrip. You will need to enter a name for the project;
When you have selected a name for the project hit the “Create Project” button and select ok to save the .rip file, then select “Rip Title” and “Read DVD table of contents”;
When you have done that you have to select the title that the film is on, which is almost always the one with the most frames;
To rip the dvd select “Rip selected title(s)/Chapter(s)” button, highlighted in blue on the above picture. dvd::rip will begin to rip the dvd, on my computer it takes about 10 minutes to rip a title.
Merge Vob files
At this point merge the vob files with the cat command;
cat title-001.vob title-002.vob title-003.vob title-004.vob >> output.vob
(filenames covered up because despite having paid for the DVDs and the act that I’m only doing this for personal use, I don’t want to take any chances)
Cut and Crop Film
The next step is to use avidemux to cut, crop and transcode the video to a format ready for conversion to mp4. Start avidemux with the following command;
open the merged vob file that you created in the previous step and when asked “This looks like mpeg
Do you want to index it?” select yes;
I takes a little while for avidemux to generate the index;
Select Output Formats
The first thing you need do in avidemux is select the output formats that you will use, for this I choose;
VIdeo: MPEG-4 ASP (lavc)
Audio: MP3 (LAME)
There are a couple of filters that you might want to take advantage of, for the video I use the crop filter, to do this select filter under the video section of the left hand side of avidemux and double click on crop;
This will bring up a window where you can set the crop dimensions, move the frame selector (highlighted in red) to a point in the movie where the border is well defined and press the Auto crop button (highlighted in green);
You might need to adjust the settings slightly, you should end up with something like this;
Click apply and then ok to accept these settings, then close the video filter window. The only filter that needs to be applied to the audio is the mixer setting, it needs to be set to stereo;
Audio > filters > mixer > stereo
Click ok and you will done with filters.
You can set the start and the end point of the video using the a and b buttons you can use this to cut out the titles at the start of the film and the credits at the end;
Move the frame selector (Red) to where you want the video to start (you can fine tune with the arrows to the left of the “a” button) and press the “a” button (Green), the frame number will be shown in the bottom right of the window (Blue). Do the same for where you want the movie to end, substituting the “a” button for the “b” button.
Now that everything is set up, press the save button, give the file a name (you need to include the extension as avidemux doesn’t do this automatically) and press ok. Avidemux should begin encoding;
Encode to mp4
The final step is to issue two commands that will convert the file that avidemux created, into a stream-able mp4 file, the first command does the actual conversion;
/usr/local/bin/ffmpeg -i "input.avi" -vcodec libx264 -flags +loop -cmp +chroma -deblockalpha 0 -deblockbeta 0 -b 1250k -maxrate 1500k -bufsize 4M -bt 600k -refs 1 -bf 3 -coder 1 -me_method umh -me_range 16 -subq 7 -partitions +parti4x4+parti8x8+partp8x8+partb8x8 -g 250 -keyint_min 25 -level 30 -qmin 10 -qmax 51 -qcomp 0.6 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec libfaac -ab 128k -ar 48000 -ac 2 "output.mp4"Where input.avi is the video created by Avidemux.
If you have a dual or quad core processor, it’s worth adding the “-threads” switch to that, so that you can get the most from your processor;
/usr/local/bin/ffmpeg -i "input.avi" -threads 4 -vcodec libx264 -flags +loop -cmp +chroma -deblockalpha 0 -deblockbeta 0 -b 1250k -maxrate 1500k -bufsize 4M -bt 600k -refs 1 -bf 3 -coder 1 -me_method umh -me_range 16 -subq 7 -partitions +parti4x4+parti8x8+partp8x8+partb8x8 -g 250 -keyint_min 25 -level 30 -qmin 10 -qmax 51 -qcomp 0.6 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec libfaac -ab 128k -ar 48000 -ac 2 "output.mp4"Example for a quad core processor.
and this command makes the file stream-able;
qt-faststart "output.mp4" "faststart-output.mp4"Where “faststart-output.mp4” is the streamable mp4 file.
If you have any suggestions,questions or comments about how I could improve is article please feel free to leave a comment. Thanks