[mdlug] Script for encoding random video files for the Droid

Ingles, Raymond Raymond.Ingles at compuware.com
Mon Mar 22 10:15:04 EDT 2010


 This script uses mencoder to re-encode video into H264 at 1000
kbits/sec for the Droid. It assumes the original video has a resolution
less than 854x480. If the video is larger, some scaling will need to be
done.

 Sincerely,

 Ray Ingles                                            (313) 227-2317

 "Science is what we understand well enough to explain to a computer.
          Art is everything else we do." - Donald Knuth

------------------------------------

#!/bin/bash
# Helps handling filenames with spaces
IFS=`printf '\n\t'`

## Set up parameters for encoding

# 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 [input video]"
  exit 0
fi

### Re-encode the video to H264 ###

# 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 $2 \
-o /dev/null -passlogfile ${TEMP_FILE_DIR}/pass.log -vf softskip,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 $2 \
-o ${TEMP_FILE_DIR}/${NEWNAME}.avi -passlogfile
${TEMP_FILE_DIR}/pass.log -vf softskip,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