Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old October 8th, 2006   #1
Heinz
First Cup of Ubuntu
 
Heinz's Avatar
 
Join Date: Apr 2005
Location: Germany
Beans: 10
Xubuntu Jaunty Jackalope (testing)
HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

MPlayer and MEncoder are amazing tools not only for watching but also for backing up DVD content. This HOWTO demonstrates how to create a very high quality rip with next generation video (H.264/x264/MPEG-4 AVC) serveral audio tracks (Vorbis in this case, can be other formats like AC3, MP3) and subtiles (vobsubs) in a Matroska container. In order to install the necessary applications you will need the multiverse repository (https://help.ubuntu.com/community/Repositories/Ubuntu).

1. Install needed applications

Quote:
sudo apt-get install mplayer mencoder normalize-audio vorbis-tools mkvtoolnix mkvtoolnix-gui gpac x264-bin
2. Rip DVD to harddisk

Quote:
mplayer dvd://1 -v -dumpstream -dumpfile title.vob
  • 1 is the stream you want to rip.

3. Subtitles

In order to determine which subtitles are available on your DVD run the following command

Quote:
mplayer dvd:// -v -vo null -ao null | grep "subtitle"
3.1 Rip subtitles

Quote:
mencoder dvd://1 -nosound -ovc frameno -o /dev/null -slang en -vobsubout title
  • 1 is the stream we extract the subs from
  • slang is the desired language (en, de, fr, etc.)
  • title is the basename of the vobsub files, in this case title.idx and title.sub

4. Audio

In order to determine which audio tracks are available on your DVD run the following command

Quote:
mplayer dvd:// -v | grep "audio stream"
In case you want to keep the original AC3 audio step forward to 4.4.

4.1 Convert audio to PCM

Quote:
mplayer title.vob -ao pcm:file=audio1.wav -vc dummy -aid 128 -vo null
  • title.vob is the stream we already ripped in step 2
  • audio1.wav is the name of the resulting PCM file
  • -aid 128 chooses the first audio track
If you would like to rip another audio track (e.g. commentary or different language) repeat the above with the next track number (-aid 129 would be the second track) and save as audio2.wav.

4.2 Normalize audio

Quote:
normalize-audio audio1.wav
Repeat on audio2.wav etc. if you have more than one audio track.

4.3 Encode audio into Ogg Vorbis

Quote:
oggenc -q5 audio1.wav
  • -q5 is the desired quality of the first track. Wikipedia: Many users feel that Vorbis reaches transparency (sound quality that is indistinguishable from the original source recording) at a quality setting of -q5, approximately 160 kbit/s. Additional audio tracks can be encoded accordingly with lesser quality in order to save disc space.

4.4 Keep original Dolby Digital AC3 audio

In case you do not want to compress audio but keep the original AC3 track simply extract it from the VOB with

Quote:
mplayer title.vob -aid 128 -dumpaudio -dumpfile title.ac3
5. Video

This example uses the two-pass-method and presumes progressive PAL video. Read here how to deal with telecined, interlaced and NTSC video.

First, we have to get rid of black borders around the movie. Hence we playback the file with the cropdetect filter.

Quote:
mplayer title.vob -vf cropdetect
Move a little forward in the movie using the arrow-up key and let MPlayer find the correct settings for you. If you are finished quit MPlayer and copy the part -vf crop=720:432:0:76 from the console. Of course your values might differ from this example.

5.1 Determine video bitrate

The choice of the bitrate depends. If you don't care about filesize anything above 1000 deliveres superb quality. If you do however plan the final file size to be about 700 MB or a multiple calculate like this (copied from MPlayer documentation):

If you aim at a certain size, you will have to somehow calculate the bitrate. But before that, you need to know how much space you should reserve for the audio track(s), so you should rip those first. You can compute the bitrate with the following equation: bitrate = (target_size_in_Mbytes - sound_size_in_Mbytes) * 1024 * 1024 / length_in_secs * 8 / 1000 For instance, to squeeze a two-hour movie onto a 702MB CD, with 60MB of audio track, the video bitrate will have to be: (702 - 60) * 1024 * 1024 / (120*60) * 8 / 1000 = 740kbps

5.2 Start video encoding process

Create a file which runs the first and second pass consecutively.

Quote:
gedit videnc
Paste the following into that file and adjust the cropping and video bitrate values with the ones you got from the procedures above


Code:
# First pass
mencoder -v\
         title.vob\
        -vf crop=720:432:0:76,harddup\
        -ovc x264 -x264encopts subq=4:bframes=3:b_pyramid:weight_b:turbo=1:pass=1:psnr:bitrate=1000\
        -oac copy\
        -of rawvideo\
        -o title.264

# Second pass
mencoder -v\
         title.vob\
        -vf crop=720:432:0:76,harddup\
        -ovc x264 -x264encopts subq=6:4x4mv:8x8dct:me=3:frameref=5:bframes=3:b_pyramid:weight_b:pass=2:psnr:bitrate=1000\
        -oac copy\
        -of rawvideo\
        -o title.264
x264encopts threads=auto will speed things up if you own a dual-core cpu.

Since MEncoder is not able to save directly into Matroska containers we encode the video in raw format convert it later into .mp4 and finally mux everything (video, audio, subtitles) together with mkvmerge. Interested in what all those options mean? If everything fits your needs save videnc.

Encoding of MPEG-4 AVC video is a time consuming matter. On my AMD Athlon64 3000+ a 90 minute movie takes about 3 hours for the first and again about 5 hours for the second pass. Best is to let your machine work over night while you sleep.

Run videnc
Quote:
sh videnc
6. Muxing

6.1 Mux video into MP4 container

Good morning!

If encoding went fine we are ready to put that x264 file into an MP4 container

Quote:
MP4Box -add title.264 title.mp4
You can already verify the result by playing it in MPlayer.

6.2 Muxing it all together into Matroska container

Start up the MKV files creator

Quote:
mmg
and simply drag & drop your files into the Input files box
  • title.mp4
  • audio1.ogg or audio.ac3
  • audio2.ogg
  • title.idx (not title.sub!)
You might have noticed that we did not scale the video during the encoding process. That is because Matroska handles the aspect ratio itself. Simply define the languages and track names in the Tracks box and choose the correct aspect ratio. Choose an output filename (the default would produce title.mkv) and hit Start muxing.

Changelog:
  • 09.10.2006 - fixed encoding parameters
  • 09.10.2006 - added telecined, interlaced, NTSC
  • 10.10.2006 - added encode to certain file size
  • 10.10.2006 - added determine audio and subtitle tracks, overworked document structure
  • 13.10.2006 - changed -oac copy instead of -nosound due to sync issues
  • 28.01.2007 - added option "harddup" to video filter chain, added support for dual-core cpus (thanks to NobodySpecial)

ToDo:
  • Rip chapters with OGMTools

Last edited by Heinz; January 28th, 2007 at 09:36 AM.. Reason: Update
Heinz is offline   Reply With Quote
Old October 9th, 2006   #2
abu_nawas
5 Cups of Ubuntu
 
Join Date: Aug 2006
Beans: 17
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

it's nice.
what about the interlaced movie?
abu_nawas is offline   Reply With Quote
Old October 9th, 2006   #3
Heinz
First Cup of Ubuntu
 
Heinz's Avatar
 
Join Date: Apr 2005
Location: Germany
Beans: 10
Xubuntu Jaunty Jackalope (testing)
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

http://www.mplayerhq.hu/DOCS/HTML/en...-telecine.html describes how to percept and handle interlaced video. I received good results by using the deinterlce postprocessing filter -vf pp=lb. As the documentation reads there are serveral filters available and it should be done after cropping and before scaling.
Heinz is offline   Reply With Quote
Old November 9th, 2006   #4
encho
Way Too Much Ubuntu
 
encho's Avatar
 
Join Date: Apr 2005
Location: Dublin
Beans: 90
The Feisty Fawn Testing
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

I have just seen this guide, nice one. But there is one point I wish to make.
Using matroska is cool, it is open source, etc. Also vorbis - excellent quality, small sizes, etc. But, if you really want to make your video archive, I suggest that you go with x264/avc (video) and faac/aac (audio), all muxed in mp4 container. Why? Because future HD DVD devices and Blue Ray are going to support that format straight from your file. Even now you can buy some classic dvd players - check the Nero certified devices.

And it is not huge difference for the encoding process - you still create x264 video stream - the only difference is in audio and container.

After a lot of researching, testing and waiting, I have found out that:

Quote:
ffmpeg -i %f -r 23.976 -pix_fmt yuv420p -f rawvideo -s 640x360 - | x264 --min-keyint 24 --keyint 240 --crf 23 --qpmin 5 --qpmax 51 --qcomp 0.75 --me umh --subme 6 --ref 6 --bframes 3 --ipratio 1.25 --pbratio 1.33 --filter -1,-1 --no-psnr --b-rdo --b-pyramid --weightb --mixed-refs --bime --trellis 2 --8x8dct --analyse all --fps 23.976 --output %f.264 - 640x360
produces the best results in one pass - constant quality based (where %f is input file - I am using thunar's scripts).
And:
Quote:
ffmpeg -i %f -r 23.976 -ac 2 -vn -f wav -y - | faac -q 90 - -o %f.aac
produces hi-quality aac audio.
Finally:
Quote:
MP4Box -add %f.264#video -add %f.aac#audio -fps 23.976 %f.aac.mp4
mux them together in MP4 container, very compatible and reliable.

I also got few tricks for converting pal to ntsc and vice versa.

Instead of having 4 gig mpegs or 1 gig Xvids, I have 500mb(!) hi quality mp4s in my video collection.

Even if you have your collection in mkv format (I did), there is an easy way to remux them to mp4 (without any quality loss). Just something like:
Quote:
mkvextract tracks %f 1:%f.264 2:%f.ogg && MP4Box -add %f.264#video -add %f.ogg#audio -fps 23.976 %f.ogg.mp4 && rm %f.264 && rm %f.ogg
Although you will have non-standard MP4 (with vorbis audio), but maybe those will be supported some time. You can always reencode audio as well.

Hope this helps to someone.
__________________
Registered Linux User #403318

Last edited by encho; November 9th, 2006 at 11:46 AM..
encho is offline   Reply With Quote
Old November 14th, 2006   #5
Aditz
Spilled the Beans
 
Aditz's Avatar
 
Join Date: Apr 2006
Beans: 13
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

I think it is better to use neroaacenc with wine. It has better quality than faac. Discussion: http://forum.doom9.org/showthread.php?t=113694
Aditz is offline   Reply With Quote
Old November 15th, 2006   #6
encho
Way Too Much Ubuntu
 
encho's Avatar
 
Join Date: Apr 2005
Location: Dublin
Beans: 90
The Feisty Fawn Testing
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

Quote:
Originally Posted by Aditz View Post
I think it is better to use neroaacenc with wine. It has better quality than faac. Discussion: http://forum.doom9.org/showthread.php?t=113694
It stands for 5 channel audio, as for 2 channels, faac is more than enough.
__________________
Registered Linux User #403318
encho is offline   Reply With Quote
Old November 18th, 2006   #7
the ev
Just Give Me the Beans!
 
the ev's Avatar
 
Join Date: Jun 2006
Beans: 49
Ubuntu 8.04 Hardy Heron
Question Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

Great tutorial!! Makes my switch from Windows/StaxRip much easier.

But here's my question. I've taken your steps and put them in a Bash script (of which I'm very proud of myself for doing, as my programming skills are pretty limited), but I was wondering if there was anyway to do a few things with it.

Here's the script:
Code:
#!/bin/bash
echo Name of the DVD?
read NAME
mkdir ~/$NAME
cd ~/$NAME
echo "Device? (/dev/hda or /dev/hdb)"
read DEVICE 
mplayer -dvd-device $DEVICE dvd://1 -v -dumpstream -dumpfile $NAME.vob
echo "Starting Crop detection - copy <-vf crop=*:*:*:*> for editing videnc!!"
mplayer $NAME.vob -vf cropdetect
cp ~/videnc ~/$NAME/
echo "Starting GEdit to modify Video encoding settings.  Close the window when done."
gedit videnc
echo "Extracting Audio..."
mplayer $NAME.vob -ao pcm:file=audio1.wav -vc dummy -aid 128 -vo null
echo "Normalizing Audio..."
normalize-audio audio1.wav
echo "Encoding Audio..."
oggenc -q6 audio1.wav
echo "Encoding Video..."
sh videnc
echo "Packaging Video..."
MP4Box -add $NAME.264 $NAME.mp4
echo "Packaging Movie..."
mkvmerge -o $NAME.mkv $NAME.mp4 audio1.ogg
echo "All done!!"
...And here are my questions:
1) Is there anyway to export the crop value from mplayer -vf crop to the videnc file automatically, rather than manually cutting and pasting? And if there isn't, is there anyway to have a break waiting on "Enter" before going to the next step (configuring videnc)?

2) Is there anyway to take the input for the local variable $NAME and automatically update the corresponding entries in videnc?

I know these are more scripting questions than ripping questions, but if you've got any insights on how I can clean up this script, I'd love to hear them. I'm trying to automate the process as much as possible, so I can just stick in the DVD and start the script.
__________________
-Ev

Gyf þu riht nimst, nelt ðu wifes wesan!
the ev is offline   Reply With Quote
Old November 18th, 2006   #8
the ev
Just Give Me the Beans!
 
the ev's Avatar
 
Join Date: Jun 2006
Beans: 49
Ubuntu 8.04 Hardy Heron
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

Oh, I forgot my third question:

Is there anyway to export the entire process to a log file? Even better, a time-stamped one?
__________________
-Ev

Gyf þu riht nimst, nelt ðu wifes wesan!
the ev is offline   Reply With Quote
Old November 25th, 2006   #9
Heinz
First Cup of Ubuntu
 
Heinz's Avatar
 
Join Date: Apr 2005
Location: Germany
Beans: 10
Xubuntu Jaunty Jackalope (testing)
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

@encho: I've given up on standalone players and only use Linux boxes for video playback. So, this guide mainly aims on best quality (imho) combined with a convenient container which handles vobsubs.

Having said that you are completely right about hardware support of MP4 files. You should use MP4 with AAC in this case.

@the ev: I'm not much of a script guru meself but a quick search found this link http://lists.mplayerhq.hu/pipermail/...ly/027662.html which invokes an automated cropping routine. Perhaps that will be useful for you. Linux offers a couple of tools to manipulate text files with. Hence automated editing of the videnc file with grep, sed and the like should not be a problem. I'm sure you can cope with that.
Heinz is offline   Reply With Quote
Old November 28th, 2006   #10
dkaplowitz
5 Cups of Ubuntu
 
Join Date: Oct 2005
Beans: 17
Ubuntu 7.10 Gutsy Gibbon
Send a message via ICQ to dkaplowitz Send a message via AIM to dkaplowitz Send a message via MSN to dkaplowitz Send a message via Yahoo to dkaplowitz
Re: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska

Cool guide, thanks for taking the time to post it all!
dkaplowitz is offline   Reply With Quote

Bookmarks

Tags
dvd, rip

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:00 PM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry