DVD to MP4

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 with advanced encoding. Several techniques exist to convert to MP4.

Dependencies
mplayer is needed to measure certain values and can be used for encoding, however a lot of the USE flags aren't.

Playing movies in GNOME the gtk USE flag isn't required since mplayer provides keyboard shortcuts when viewing videos.

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.

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 multiple languages are present and record its audio id (aid).

Chapter Information
If there subtitles or chapters in the video they can be pulled and merged later.

Subtitles Information
View Languages of Subtitles

Play the subtitle it to be sure you have the correct one.

Pull Subtitles

Mencoder can also do it:

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 mplayer will need to record the size to crop.

signals to start detect 50000000 milliseconds into the stream.

Look at the crop info, it will be listed as such:

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:

Older videos may not be:

Convert Using ffmpeg
ffmpeg doesn't have chapter support at this time, but it is quicker with comparable quality to mencoder.

Values for Crop
If needing to crop the video, get the crop information listed above and add the crop values for the x and y positions. (e.g. )

Also will be needed is to to specify the aspect ratio. To get to correct aspect ratio crop a tiny segment of the video.

mplayer can output the aspect ratio.

Add aspect ratio following the crop values ( e.g. ) Also look for if mplayer changed the aspect ratio to the correct one.

The Various Variables
Converting to MP4 with ffmpeg, the variables need known:


 * ffmpeg will need to be told the birate to determine size and quality. The bitrate calculator works nice to give the kilobits value needed.
 * Specify the video track (0:0) followed by the audio track (0:2) in . ( 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  for a multi-processor system
 * is the number of audio channels,  is the audio bitrate.
 * Volume levels are lowered after encoding, consider increasing.
 * To test it first use the  and specify the duration in seconds.

Encode the Video
The command line to convert to MP4:


 * If doing mono sound set the channels to 1
 * Encoding a 90 minutes B&W move will take a few hours.
 * No second pass necessary

Bitrate Calculator

 * Figure your Bitrate If 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

For the second pass mencoder/x264 reads statistics from a logfile it created on the first pass and compresses video accordingly.


 * 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 to have the correct one

Now pull the audio

Convert the Audio to aac

AC3 to 6 channel PCM

PCM to AAC 5.1

Where  is the bitrate and   is the quality.

Test the 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.

Using mp4creator
After pulling both audio and video put the raw video in MP4 Container


 * 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

To optimize for streaming:

Notes:
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.

Links

 * ffmpeg compression
 * Ripping DVD to Matroska and H.264
 * Convert ac3 to aac