DVD to MP4
From Gentoo Linux Wiki
|
MP4, officially MPEG4 - Part 14 is a multimedia container format. This wiki focuses on converting standard MPEG-1 (.mpg) files to MPEG-4 Part 14. Several techniques exist to convert to MP4.
[edit] Dependencies
mplayer is needed to measure certain values and can be used for encoding, however a lot of the USE flags aren't.
echo "media-video/mplayer -esd -ftp -gtk -iconv -ipv6 -live -quicktime -real -samba -theora -win32codecs -xvid" >> /etc/portage/package.use
Playing movies in GNOME the gtk USE flag isn't required since mplayer provides keyboard shortcuts when viewing videos.
emerge -pv mplayer
Using ffmpeg to encode is faster and with quality equal to of using mencoder (part of mplayer), but is more limited as well. To use ffmpeg set some basic USE flags first.
echo "media-video/ffmpeg a52 aac encode sdl theora threads v4l vorbis x264 xvid" >> /etc/portage/package.use emerge -pv ffmpeg
[edit] Pulling Info from the Video
Best now to put the video on the hard disk for quicker processing. Leave the device connected (camcorder, dvd...) as some information will still need to be extracted from it.
Audio Information
Pick a language stream if mulitiple languages are present and record it's audio id (aid).
mplayer -v dvd://1 | grep "audio stream"
Chapter Information
If you have subtitles or chapters in your video you can pull them and merge them in later.
emerge media-sound/ogmtools dvdxchap -t movie.mpg /dev/hda > chapters.txt
Subtitles Information
View Languages of Subtitles
mplayer dvd:// -v -vo null -ao null | grep "subtitle"
subtitle ( sid ): 0 language: en
subtitle ( sid ): 1 language: es code>
Play the subtitle it to be sure you have the correct one.
mplayer -sid 0 movie.mpg
Pull Subtitles
emerge subtitleripper subtitle2vobsub -o vobsubs -i vts_01_0.ifo -a SID< subs-SID
Mencoder can also do it:
mencoder movie.mpg -nosound -ovc frameno -o /dev/null -slang en -vobsubout title
Crop Information
Older videos are created with a black border because older players (vcrs, dvd players...) wouldn't play videos that weren't a specific size. To remove them out we'll need to record the size to crop.
mplayer movie.mpg -vf cropdetect -sb 50000000
-sb code> signals to start detect 50000000 milliseconds into the stream.
Look at the crop info, it will be listed as such:
crop=[width]:[height]:[xposition]:[yposition] code>
Width and height tell the videos actual size and x and y position will tell the videos place within a border (if there is a black border around it).
New videos likely will already be cropped:
crop=720:480:0:0 code>
Older videos may not be:
crop=704:336:10:76 code>
[edit] Convert Using ffmpeg
ffmpeg doesn't' at this time have chapter support or subtitle support, but it is quicker with comparable quality to mencoder.
Values for Crop
If you need to crop the video, get the crop information listed above and add the crop values for the x and y positions. (e.g. -croptop 76 -cropbottom 76 -cropleft 10 -cropright 10 code>)
We'll also need to specify the aspect ratio if you do this. To get to correct aspect ratio we'll crop a small portion of the video.
mencoder movie.mpg -vf crop=704:336:10:76 -oac copy -ovc raw -endpos 120 -o movie-cropped.mpg
mplayer can tell you the aspect ratio.
mplayer -v movie-cropped.mpg | grep aspect
Add aspect ratio following the crop values ( e.g.-aspect 1.86 code> ) Also look for if mplayer changed the aspect ratio to the correct one.
The Various Variables
Converting to MP4 with ffmpeg, the variables we needs to know.
- ffmpeg will need to be told the birate to determine size and quality. The bitrate calculator works nice to give the kilobits value (
-b code> ) needed. - Specify the video track (0:0) followed by the audio track (0:2) in
-map code>. ( Audio tracks listed in mplayer starts at 128. ffmpeg uses the reverse order, i.e. 128 is 0:2 while 129 is 0:1.) - Specify
-threads code> for a multi-processor system -
-ac code> is the number of audio channels,-ab code> is the audio bitrate. - Volume levels are lowered after encoding, consider increasing
-vol code>. - To test it first use the
-t code> and specify the duration in seconds.
Encode the Video
The command line to convert to MP4 is pretty simple:
ffmpeg -map 0:0 -map 0:1 -i movie.mpg -threads 2 -b 604k -vcodec h264 -ac 1 -ab 256k -ar 44100 -acodec mpeg4aac -coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me hex -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -vol 500 movie.mp4
- If you're doing mono sound set the channels to 1
- Encoding a 90 minutes B&W move will take a few hours.
- No second pass necessary
[edit] Convert Using Mencoder
Bitrate Calculator
- Figure your Bitrate If you need to know the length use the letter "o" while playing the movie in mplayer to see the video length. Record the value in kilobits per second ( kbits/s ).
Encoding for mencoder is done with 2 passes. This will take awhile. An average movie will take 3-5 hours on the first pass and the second pass will take a little longer.
Transcode to Raw H.264
mencoder movie.mpg -vf pullup,softskip,crop=720:480:0:0,harddup -nosound -ovc x264 -x264encopts bitrate=1100:subq=5:bframes=3:b_pyramid:weight_b:turbo=1:pass=1 -of rawvideo -o movie.264
For the second pass mencoder/x264 reads statistics from a logfile it created on the first pass and compresses video accordingly.
mencoder movie.mpg -vf pullup,softskip,crop=720:480:0:0,harddup -nosound -ovc x264 -x264encopts bitrate=1100:subq=5:8x8dct:frameref=2:bframes=3:b_pyramid:weight_b:pass=2 -of rawvideo -o movie.264
- The number of cpus (threads=2 or auto...), can be put in after bitrate, unspecified the default is one, using more that one processor though will result in a slight quality lose.
Pull and Convert Audio
Now the mencoder video will need to be merged with the audio. First we need to extract it.
aid's start at 128. Listen to the aid to be sure you have the correct one
mplayer -aid 128 movie.mpg
Now pull the audio
mplayer movie.mpg -aid 128 -dumpaudio -dumpfile audio128.ac3
Convert the Audio to aac
AC3 to 6 channel PCM
a52dec audio128.ac3 -o wavdolby > audio128.wav
PCM to AAC 5.1
faac audio128.wav audio128.aac -b 256 -q 100
Where -b code> is the bitrate and -q code> is the quality.
Test the aac
mplayer audio128.aac
Normalize Audio
Unnecessary but because sound is sometimes designed for high end systems, listening on regular stereos can lead to the effect of constantly turning up or down the volume. Normalizing audio can be done with mp3's and wavs.
emerge normalize normalize-audio audio1.wav
[edit] Merging Streams
Using mp4creator
emerge mpeg4ip
After pulling both audio and video put the raw video in MP4 Container
mp4creator -create=movie.264 -timescale=29970 -rate=29.97 movie.mp4
- Framerate 25 is for PAL, NTSC uses ~29.97... Here's a decent reference of what analog television system is in your area. These values are important if you want to play these videos on analog television.
Merge the streams
mp4creator -create=audio128.aac movie.mp4
To optimize for streaming:
mp4creator - optimize output.mp4
MP4Box
NOTE: One, MP4Box has a 2GB file limit save size; and two, MP4Box can have trouble syncing audio and video correctly.
emerge gpac
MP4Box -fps 29.97 -chap chapters.txt -add movie.264 -add audio128.aac movie.mp4
[edit] Notes:
Another ffmpeg Method
Other Graphical Options
- DeVeDe
- DVD::Rip - emerge dvdrip
- Avidemux
- mplayer can at time collide with desktop effects (beryl, compiz-fusion...) best to disable if you have any problems.
- For a two hour video encoding 1 GB is reasonable. Some can even be compressed to 700MB to fit on a CD.
- Thanks bond for telling me mplayer can't pull straight to acc.
