Sunday, May 15, 2011

Linux BASH script to encode video for the Motorola Xoom

Just a quick an dirty linux bash script that you can use on the command line to convert any video supported by FFmpeg to your Motorola Xoom running Android OS.

I have this setup as a bash alias in $HOME/.bash_profile. You can of course use it as a stand alone script if that is your preference.

Usage is quite simple: to_xoom <input file> [<destination directory>]
to_xoom /path/to/some_video_file.mkv /tmp/

Then all you have to do is send it to your tablet using ftp, or by mounting it.

to_xoom()
{
        # XOOM screen dimensions
        # set this to the width of your device, in pixels
        max_width="1280"

        set_dir=0
        dir=""
        in="$1"
        if [ -n "$2" ] && [ -d "$2" ]; then
                set_dir=1
                dir="$2/"
        fi
        if [ ! -f "$in" ]; then
                echo "Error: File: $in does not exist."
                return -1
        fi

        width="`ffprobe -show_streams \"${in}\" 2>/dev/null \
                | grep width \
                | cut -d'=' -f2`"

        if [ "$width" -gt "$max_width" ]; then
                width=${max_width}
        fi

        out="`echo $in | perl -pe '{ s/\..{3,4}$// }'`.mp4"
        if [ $set_dir -eq 1 ]; then
                out="`echo $out | perl -pe '{ s/^.*\/// }'`"
        fi

        echo "Converting $in to $out for Xoom playback"
        nice /usr/bin/ffmpeg \
                -i "${in}" \
                -vcodec libx264 \
                -vpre medium -crf 20 \
                -threads 0 -bf 1 \
                -b 20480k \
                -vf scale=${width}:-1 \
                -qscale 3 \
                -acodec libfaac -ab 192k \
                -ac 2 -ar 48000 -f mp4 "${dir}${out}"

        return $?
}

Post a Comment