[mdlug] Script for encoding DVDs for a Droid
Ingles, Raymond
Raymond.Ingles at compuware.com
Mon Mar 22 10:09:06 EDT 2010
Below is a script for encoding DVD video for playback on a Droid. It
uses mencoder to re-encode the original MPEG-2 stream to H264 with a
1000 kbit bitrate. This is a very CPU-intensive process. For example,
with a dual-core Athlon X2 at 2.8GHz, it took 201 minutes (three hours
and twenty-one minutes) to convert a 115-minute movie. The resulting
file was 915MB. It was tested on Ubuntu 9.10.
Note: this script assumes you want subtitles. If not, you'll need to
comment out/remove the appropriate lines. Note too: the formatting may
be messed up by this silly email program, so take care when cutting and
pasting.
Thanks to the h264enc script for helping to determine the appropriate
mencoder parameters.
Sincerely,
Ray Ingles (313) 227-2317
"The Philosopher's Syndrome: Mistaking a failure of imagination for
an insight into necessity." - Daniel Dennett
-----------------------------------
#!/bin/bash
# Helps handling filenames with spaces
IFS=`printf '\n\t'`
## Set up parameters for encoding
# If you're skipping frames, increase FPS. If duplicating, decrease FPS.
# Usual values are 24000/1001 (23.976), 24, 25 (PAL), 30000/1001
(29.97), 30.
#OFPS="24"
OFPS="24000/1001"
#OFPS="30000/1001"
# Height of the output video (Droid screen: 480)
SCALEH=480
# Width of the output video (Droid screen: 720)
SCALEW=854
# Where to put temporary files
TEMP_FILE_DIR=`mktemp -d`
# This is the key parameter we gotta have from the user.
# Spaces in the output file name are probably not a good idea.
if [ -n "$1" ]; then
NEWNAME=$1
else
echo Output file name not specified, aborting!
echo "Usage: " $0 " output-name [dvd-title] [dvd-device] [audio-track]
[subtitle]"
exit 0
fi
# These are optional parameters that can be tweaked but have default
values.
# Default to DVD title 1.
DVDTITLE=${2:-1}
# Device to read DVD from
# (You might want to change the default to match your system.)
DVDDEV=${3:-"/dev/dvd"}
# 128 is the default audio track on a DVD, usually
AUDIOTRACK=${4:-128}
# 0 is the default subtitle track on a DVD, usually
SUBTITLE=${5:0}
### Do the actual encoding. ###
# Two-pass encoding. Takes longer, but makes a noticeable improvement in
image
# quality for a given bitrate. More passes are possible, but hit the
point of
# diminishing returns very quickly, IMO.
nice -n 10 /usr/bin/mencoder -dvd-device ${DVDDEV} dvd://${DVDTITLE}
-aid ${AUDIOTRACK} -sid ${SUBTITLE} \
-o /dev/null -passlogfile ${TEMP_FILE_DIR}/pass.log -ofps ${OFPS} -vf
softskip,dsize=16:9,scale=${SCALEW}:${SCALEH}:0,harddup -sws 10
-channels 2 -oac faac -faacopts mpeg=4:object=2:br=128 -ovc x264
-x264encopts
pass=1:bitrate=1000:turbo=2:me=umh:me_range=16:dct_decimate:nointerlaced
:no8x8dct:nofast_pskip:trellis=0:partitions=p8x8,i4x4:mixed_refs:keyint=
240:keyint_min=24:psy_rd=0.8,0.0:frameref=1:bframes=0:b_adapt=0:noweight
_b:direct_pred=none:subq=8:chroma_me:nocabac:aq_mode=1:deblock:vbv_maxra
te=1500:vbv_bufsize=1000:level_idc=30:threads=auto:ssim:psnr
nice -n 10 /usr/bin/mencoder -dvd-device ${DVDDEV} dvd://${DVDTITLE}
-aid ${AUDIOTRACK} -sid ${SUBTITLE} \
-o ${TEMP_FILE_DIR}/${NEWNAME}.avi -passlogfile
${TEMP_FILE_DIR}/pass.log -ofps ${OFPS} -vf
softskip,dsize=16:9,scale=${SCALEW}:${SCALEH}:0,harddup -sws 10 -af
volnorm,lavcresample=48000:16:1 -srate 48000 -channels 2 -oac faac
-faacopts mpeg=4:object=2:br=128 -ovc x264 -x264encopts
pass=2:bitrate=1000:me=umh:me_range=16:dct_decimate:nointerlaced:no8x8dc
t:nofast_pskip:trellis=0:partitions=p8x8,i4x4:mixed_refs:keyint=240:keyi
nt_min=24:psy_rd=0.8,0.0:frameref=1:bframes=0:b_adapt=0:noweight_b:direc
t_pred=none:subq=8:chroma_me:nocabac:aq_mode=1:deblock:vbv_maxrate=1500:
vbv_bufsize=1000:level_idc=30:threads=auto:ssim:psnr
### Repackage the video and audio into an MP4 container. ###
# Extract the video and audio from the .avi produced above.
/usr/bin/mencoder ${TEMP_FILE_DIR}/${NEWNAME}.avi -nosound -ovc copy -of
rawvideo -o ${TEMP_FILE_DIR}/h264_video.h264
/usr/bin/MP4Box -aviraw audio ${TEMP_FILE_DIR}/${NEWNAME}.avi -out
${TEMP_FILE_DIR}/aac.raw
mv -f ${TEMP_FILE_DIR}/aac_audio.raw ${TEMP_FILE_DIR}/aac_audio.aac
# Bundle it up into an MP4
/usr/bin/mplayer ${TEMP_FILE_DIR}/${NEWNAME}.avi -noconfig all -loop 1
-identify -nosound -vo null -nocache -frames 1 2>/dev/null | grep
'^ID_VIDEO_FPS' | tail -n 1 | awk -F= '{print $2}' >
${TEMP_FILE_DIR}/mp4fps
/usr/bin/MP4Box -fps $(cat ${TEMP_FILE_DIR}/mp4fps) -tmp
${TEMP_FILE_DIR} -add ${TEMP_FILE_DIR}/h264_video.h264 -add
${TEMP_FILE_DIR}/aac_audio.aac#audio:name="LC-AAC Stereo" -itags
name="new_droid":comment="Tagged on $(date)" -ipod -mpeg4 -new
"${NEWNAME}.mp4"
# Clean up after ourselves.
rm -rf ${TEMP_FILE_DIR}
The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.
More information about the mdlug
mailing list