Transcodehb.sh

From MythTV Official Wiki
Revision as of 19:20, 14 October 2015 by Jr3us (talk | contribs) (handbrake transcode.sh shell script)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a shell script I created for transcoding using handbrakecli. It is very limited in the initial number of options, but can be modified as needed. It does use the information from the mythtv xml file to gather and update database information.

#!/bin/bash
# this script will be called using the following parameters: 
# %DIR%, %CHANID%, %STARTTIME%, and %STARTTIMEUTC% and optional field for 'dontremove'
# the optional field dontremove can be anything. if blank then will be removed
# script to use handbrakecli to decrease the size of the video file,
# transcoding into mp4.
# This requires version .10.0 or newer of handbrake due to the use of the
# meaningful return values introduced in that version.

set -x

## Variables you may want or need to tweak below:

logfile="/tmp/transcodehb$$.log"
confxml="/etc/mythtv/config.xml"
# directory=$1    # this is no longer used since it is not a pathname
directory="/var/lib/mythtv/recordings"

# default ( and only ) options. tweak as you see fit. these work well for me.
# This will use the normal preset that comes with handbrake,
# trims off the letterbox, and sets qual to 20
# and set deinterlace to the slow method

addedopts="--preset=Normal -d slow"

## End of variables you may need to tweak.

(

# get the input information:

# this was originally written around the time mythtv switched from
# using localtime to using utc time.
# STARTTIME will no longer be used so $3 will be ignored.
# starttimeutc will be used henceforth.

chanid=$2
starttime=$4
starttimeutc=$4
dontremove=$5

echo "STARTING transcodehb for ${chanid}_${starttime}"

infile=$directory/${chanid}_${starttime}.mpg
basefile=${chanid}_${starttime}.mp4

#verify infile exists as an mpg or nuv file

if [ ! -f $infile ]
then
    infile=$directory/${chanid}_${starttime}.nuv
    basefile=${chanid}_${starttime}.mp4
    if [ ! -f $infile ]
    then
        infile=$directory/${chanid}_${starttimeutc}.mpg
        basefile=${chanid}_${starttimeutc}.mp4
        if [ ! -f $infile ]
        then
            infile=$directory/${chanid}_${starttimeutc}.nuv
            basefile=${chanid}_${starttimeutc}.mp4
            if [ ! -f $infile ]
                then
                echo "The inputfile $infile doesn't exist as a NUV OR MP4!"
                exit 4
            fi
        fi
    fi
fi

outfile=$directory/${basefile}
reqdopts="-i $infile -o $outfile"
hbcmd="/usr/bin/HandBrakeCLI $addedopts $reqdopts"

echo infile = $infile
echo outfile = $basefile

# next, get the db info from the mythtv config file:

hostname=`grep Host $confxml | sed -e "s;</.*>;;" | sed -e "s;<.*>;;" | sed -e "s/  *//g"`
username=`grep UserName $confxml | sed -e "s;</.*>;;" | sed -e "s;<.*>;;" | sed -e "s/  *//g"`
passwd=`grep Password $confxml | sed -e "s;</.*>;;" | sed -e "s;<.*>;;" | sed -e "s/  *//g"`
dbname=`grep DatabaseName $confxml | sed -e "s;</.*>;;" | sed -e "s;<.*>;;" | sed -e "s/  *//g"`

# nice the handbrake command to give other processes priority.
nice -n 19 $hbcmd
hbretval=$?

# latest handbrake now returns a valid value if there are errors converting.
if [ $hbretval -ne 0 ]
then
        echo "Conversion did not occur correctly"
	echo $hbretval
        exit $hbretval
fi

# prior to using the return value from handbrake, mplayer was used to
# determine if the conversion was successful.
# return=`mplayer -identify -vo null -ao null $outfile 2>&1`
# echo $return | grep "av_open_input_stream() failed" 
# retval=$?
# 
# if [ $retval -ne 1 ]
# then
#       # echo "Conversion did not occur correctly"
#       # echo $return
#       # exit $retval
# fi


# If the new file is greater than or = to 20% of the original file
# then the file is considered valid.
# probably don't need the following any more since handbrake now returns
# a meaningful value.

echo "now checking if valid output file"

if [ -f $outfile ]
then
    # need the size of the new file generated.

    set -k `wc -c $outfile`
    nfsize=$1
    set -k `wc -c $infile`
    ofsize=$1

    testsize=`expr $nfsize \* 15`

    if [ $testsize -le $ofsize ]
    then
        echo "Something is wrong with the new file"
        exit 2
    fi
else
    echo "outfile $outfile was not created"
    exit 3
fi

# time to update the db with info:

# exit 0

mysql --user="${username}" --password="${passwd}" -h "${hostname}" "${dbname}" <<-EOF
        update recorded set
        basename = "$basefile",
        filesize = $nfsize,
        bookmark = 0,
        editing = 0,
        cutlist = 0,
        commflagged = 0
        where chanid = $chanid and starttime = "$starttimeutc";
        delete from recordedmarkup where chanid = $chanid and starttime = "$starttimeutc";
        delete from recordedseek where chanid = $chanid and starttime = "$starttimeutc";
EOF

# exit 0

if [ "x$dontremove" = "x" ]
then
    set -x
    rm $infile $infile*.png
    set +x
fi

echo "transcode completed."

) > "$logfile" 2>&1

exit 0