Search:  
Gentoo Wiki

MP4

This article is still a Stub. You can help Gentoo-Wiki by expanding it.

Contents

MP4

MP4 is a container format, just like AVI, OGM, MKV, etc. It supports many newer codecs and features, and has minimal overhead compared to AVI. For more information, visit wikipedia

Creating MP4 Files

Manually combine video and audio

MP4 is most commonly used with h264 and AAC. If you have your raw video and audio files already, you can mux them into the MP4 format using the following command:

MP4Box -fps <video fps> -add <raw video.264> -add <audio file.aac> <output.mp4>

DVD to MP4 Script

The following simple bash script encodes the chapter contents of a DVD to separate .mp4 files, as a single-pass encode set at constant quality (VBR).

NOTE that this script does not automatically determine the resolution or the framerate and instead relies on the user to set them appropriately in the script. Mplayer can be used to determine this: mplayer dvd://<chapter> -vf cropdetect -ao null -vo null, where <chapter> is the DVD chapter. Feel free to improve the script and automate this.

Before using the script, add the following to /etc/portage/package.use:

media-libs/x264-svn threads mp4
media-video/ffmpeg aac encode threads x264
media-video/mpeg4ip a52 alsa ffmpeg xvid -arts -esd id3 lame mmx
media-video/mplayer X aac alsa -arts cdparanoia dvd dvdread encode gif iconv -ipv6 jpeg lzo mmx mmxext opengl png rtc speex sse  sse2 theora truetype unicode vorbis win32codecs x264 xv xvid

then:

emerge -DNuav gpac mpeg4ip x264-svn-encoder ffmpeg mplayer

to run this script, provide the name of the dvd and a range of chapter numbers on the commandline. For example:

 ./dvd_to_mp4.sh my_christmas_dvd 1 12
File: dvd_to_mp4.sh
 
#!/bin/bash

resolution=720x480
fps=23.976

prefix=$1
range=`seq $2 $3`

yield() { /usr/bin/nice -n 19 $*; }

for n in $range; do
  title=$prefix$n
  
  ##
  #   Rip the mpeg-2 stream to a VOB file and the AC3 stream to a WAV file
  #
  mplayer dvd://$n -dumpstream -dumpfile "$title.vob"

  # detect resolution and framerate
  # mplayer title.vob -vf cropdetect -ao null -vo null 

  yield ffmpeg -i "$title.vob" -r $fps -ac 2 -vn -f wav -y "$title.wav"

  ##
  #  Normalize and encode the WAV to an MP3 file
  #
  normalize "$title.wav"
  yield lame --preset standard -q0 "$title.wav" -o "$title.mp3" &> /dev/null &

  ##
  #  Encode the VOB to an MPEG-4 h.264 file  
  #
  yield ffmpeg -i "$title.vob" -r $fps -pix_fmt yuv420p -f rawvideo -s $resolution - | \
  yield x264 --min-keyint 24 --keyint 480 --crf 21 --qpmin 3 --qpmax 80 --qcomp 1.0  \
       --me umh --subme 6 --ref 6 --bframes 3 --ipratio 1.40 --pbratio 1.30    \
       --deblock -2:-2 --no-psnr --b-rdo --weightb --mixed-refs --bime         \
       --partitions all --8x8dct --analyse all --b-pyramid --fps $fps          \
       --direct auto --threads 4 --output "$title.264" - $resolution
  
  ##
  #  Interleave the 264 and MP3 into an MP4 container file
  #
  yield MP4Box -add "$title.264#video" -add "$title.mp3#audio" -fps $fps "$title.mp4"

  ##
  #  Remove the the cruft
  #
  ls | grep -e "$title" | grep -ve '\.mp4$' | xargs -i rm "{}"
done
  

m2v and ac3 to mp4

ffmpeg -i ICE_AGE.m2v -i ICE_AGE.ac3 -threads 2 -b 604k -vcodec h264 -ab 384k -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 /home/j0inty/dvdshrink /Ice_Age_de_5.1.mp4

mpg to mp4

ffmpeg -map 0:0 -map 0:1 -i ICE_AGE_2.mpg -threads 2 -b 604k -vcodec h264 -ab 384k -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 Ice_Age_2_de_5.1.mp4
Retrieved from "http://www.gentoo-wiki.info/MP4"

Last modified: Wed, 23 Jul 2008 14:08:00 +0000 Hits: 10,255