Sunday, January 29, 2012

DVD to MP4 with HandBrake (vidconv.sh)

Using Handbrake in a bash script.

This script processes my ISO file(s) and converts them to MP4.  The MP4 can then be used via UPNP or DLNA on my network devices.  I rip my DVDs to ISO and store them on my media server.  This is a living script and may change.  It takes a while to process each ISO at the moment.  I just want good quality when I view the video on a widescreen television.  The options for Handbrake I'm using:

-m                    Chapter markers
--main-feature   Find and process the main title.  By default, Title 1 is processed.
-e x264             Video encoder
-q 20                 Video quality
-r 29.97             Video frame rate
-b 2000             Video bitrate
-B 192               Audio bitrate
-i /mnt                This is the mount point for the ISO
-o                      Output file name
2> /dev/null        I don't want to see everything displayed to the screen - sending std error to nowhere land.

The script requires you to run as root since we are running mount and umount.

<<< --- vidconv.sh --->>>
#!/bin/bash

function ConvertIt()
{
    NewName=${1%%\.*}
    EXT=mp4
    if [ -f "$NewName.$EXT" ]; then
        echo "A file by the name of $NewName.$EXT already exists"
        return 1
    fi
    echo Processing:  $NewName
    mount -o loop "$1" /mnt
    HandBrakeCLI -m --main-feature -e x264 -q 20 -r 29.97 -b 2000 -B 192 -i /mnt -o "$NewName.$EXT" 2> /dev/null
    echo
    umount /mnt
}

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
if [ "$1" = "" ]; then
    echo "You need to tell me which ISO to convert"
    echo "Use -a to process all ISO files in the currentl directory"
    exit 1
fi

if [ "$1" = "-a" ]; then
        for files in *.iso
        do
                ConvertIt "$files"
        done
else
    if [ ! -f "$1" ]; then
        echo "The input file $1 does not exist."
        exit 1
    fi
    ConvertIt "$1"
fi

exit 0




No comments:

Post a Comment