Thursday, January 13, 2011

Converting Movies for Playback on Android

I have been working with a script that I picked up when I had a Motorola Q and an HTC Ozone both with the Windows 5-6 versions.  Once I picked up a new Android Phone, the Droid Incredible (love it!) I knew that I needed to change the format of the video file to x264.  Someday maybe I can change the format to WebM :)

This script doesn't do much, it basically allows me to quickly type my scriptname with a space and the name of the video file that I want to convert with an optional second parameter allowing for changing the name of the video file.

The scripts work easily on Linux, however the important part is usable on Windows or Mac as long as you have mplayer and mencoder installed on your machine.  The ability to unencrypt dvd encryption can be provided by decss and may break the law in your country.  Specifically this script is used by me to convert file formats of videos where I own the copyright, videos of my kids.  It should not be used for content that you do not own unless it falls under fair use and Americans can't circumvent DVD encryption because of the Digital Millennium Copyright Act (DMCA) even if you have fair use rights.

That being said here is the mencoder format that seems to work well for me on my Android phone.  For this example I will be converting kids.avi to kids_x264.mp4 but you could start with any format of video as long as mplayer can play it.  Because the command line is quite long I will be arbitrarily seperating the command into multiple lines to make it more readable.  I will be using the Linux notation using the '\' to signify the end of a line that continues on the next line.

mencoder kids.avi -idx -vf scale -zoom -xy 480 \
      -oac faac -faacopts mpeg=4:object=2:raw:br=128 -of lavf \
      -lavfopts format=mp4 -ovc x264 -sws 9 \
      -x264encopts nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh \
      -o kids_x264.mp4


This is my Linux script for Android:

#!/bin/sh
# This is from http://ianpurton.com/viewing-ipod-h264-videos-on-a-pocket-pc-with-tcpmp/
# Trying to create an easy way to convert video files for my smartphone
# Jeff Rasmussen 
SCREEN=480
BITRATE=512
2PASS=/tmp/$__2pass.log
AID=128
#AID=0  
# Tried to create a smart script that would automatically change the AID variable for DVD conversion 
if [ "$1" != "dvd" ]
    then AID=0
    else AID=128
fi 
if [ -e "$2PASS" ]
then
echo "Found a First pass log ($2PASS)"
echo -n "Do you want to start first pass over? (y/[n]): "
read overwrite
if [ $overwrite == "y" ]; then
echo "Removing Previous First Pass"
rm "$2PASS.0"
mv "$2PASS" "$2PASS.0"
fi
fi
#
# Crop Detect and Pass 1
#
mencoder "$1" -vf scale,cropdetect -zoom -xy "$SCREEN" \
-oac faac -faacopts mpeg=4:object=2:raw:br=128 -of lavf \
-lavfopts format=mp4 -ovc x264 -sws 9 \
-x264encopts nocabac:level_idc=30:bframes=0:bitrate="$BITRATE":threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh \
pass=1 -passlogfile "$2PASS" -o /dev/null > /tmp/$__cropdetect.log

CROP="$( awk '/-vf/{moo=$19} END{sub(").","", moo); print moo}' /tmp/$__cropdetect.log )"
echo $CROP > $__cropresult.log

if [ "$2" ]; then
echo "Custom Filename"
# Actually encode
echo mencoder "$1" -aid "$AID" -idx -vf scale,"$CROP" -zoom -xy "$SCREEN" \
     -oac faac -faacopts mpeg=4:object=2:raw:br=128 -of lavf \
     -lavfopts format=mp4 -ovc x264 -sws 9 \
     -x264encopts nocabac:level_idc=30:bframes=0:bitrate="$BITRATE":threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh \
     -passlogfile "$2PASS" -o "$2_x264.mp4"
# Use the default filename to produce video
else
mencoder "$1" -idx -vf scale,"$CROP" -zoom -xy "$SCREEN" \
     -oac faac -faacopts mpeg=4:object=2:raw:br=128 -of lavf \
     -lavfopts format=mp4 -ovc x264 -sws 9 \
     -x264encopts nocabac:level_idc=30:bframes=0:bitrate="$BITRATE":threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh \
     -passlogfile "$2PASS" -o "$1_x264.mp4"
fi

This is my Linux script for Windows Phone:
 #!/bin/sh
# This is from http://ianpurton.com/viewing-ipod-h264-videos-on-a-pocket-pc-with-tcpmp/
# Trying to create an easy way to convert video files for my smartphone
# Jeff Rasmussen
SCREEN=320
BITRATE=500
#BITRATE=687
2PASS=/tmp/$1_2pass.log
AID=128
#AID=0
if [ "$1" != "dvd" ]
    then AID=0
    else AID=128
fi
if [ -e "$2PASS" ]
then
echo "Found a First pass log ($2PASS)"
echo -n "Do you want to start first pass over? (y/[n]): "
read overwrite
if [ $overwrite == "y" ]; then
echo "Removing Previous First Pass"
rm $2PASS.0
mv $2PASS $2PASS.log.0
fi
fi
#
# Crop Detect
#
mencoder "$1" -oac mp3lame -ovc xvid -vf scale,cropdetect -zoom -xy "$SCREEN" -xvidencopts pass=1 -passlogfile $2PASS -o /dev/null > /tmp/cropdetect

CROP="$( awk '/-vf/{moo=$19} END{sub(").","", moo); print moo}' /tmp/$1_cropdetect.log )"
echo $CROP > $1_cropresult.txt

if [ "$2" ]; then
echo "Custom Filename"
# Actually encode
mencoder "$1" -aid "$AID" -mc 0 -oac mp3lame -lameopts abr:br=92 -idx -vf scale,"$CROP" -zoom -xy "$SCREEN" -ovc xvid -xvidencopts  autoaspect:chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:qpel:turbo:max_bframes=2:threads=2:pass=2:bitrate="$BITRATE" -passlogfile $2PASS -o "$2_xvid.avi"
# Use the default filename to produce video
else
mencoder "$1" -aid "$AID" -mc 0 -oac mp3lame -lameopts abr:br=92 -idx -vf scale,"$CROP" -zoom -xy "$SCREEN" -ovc xvid -xvidencopts autoaspect:chroma_opt:vhq=4:bvhq=1:quant_type=mpeg:qpel:turbo:max_bframes=2:threads=2:pass=2:bitrate="$BITRATE" -passlogfile $2PASS -o "$1_xvid.avi"
fi

1 comment:

  1. mencoder -ovc lavc -oac lavc 001.avi 002.avi 003.avi -o output.avi

    from: http://www.howforge.com/how-to-merge-video-files-using-mencoder

    ReplyDelete