Mittwoch, 8. September 2010

Converting VDR recordings to Matroska (MP4/AC3)

After testing some tools, I decided to go with HandBrake which does an awesome job in automatic cropping.

The script below does start a HandBrake job using the ac3 audio stream if available, otherwise the first other mp2 stream. The script could be used as VDR postrecording hook for automatically convert recordings.

#!/bin/bash

LOGFILE="/var/log/vdr-convert.log"
INFILE="$1/00001.ts"
OUTDIR="$2"
AUDIO=`ffmpeg -i $INFILE 2>&1  |grep "Audio" |grep "ac3" |sed -e 's/.*Stream #0.\([0-9]\).*/\1/g'`
if [ "x$AUDIO" != "x" ] ; then
AUDIO="-a $AUDIO -E ac3"
else
AUDIO=`ffmpeg -i $INFILE 2>&1  |grep "Audio" |grep "mp2" |grep "stereo" |sed -e 's/.*Stream #0.\([0-9]\).*/\1/g'`
AUDIO="-a $AUDIO -E faac"
fi
TITLE=`egrep "^T" "$1/info"  |sed -e "s/^T //"`
EPISODE=`egrep "^S" "$1/info"  |sed -e "s/^S //"`
OUTFILE="$TITLE"
if [ "x$EPISODE" != "x" ]; then
OUTFILE="$OUTFILE-$EPISODE"
fi
OUTFILE="$OUTDIR/$OUTFILE.mkv"

echo "Executing: /usr/bin/HandBrakeCLI -i \"$INFILE\" -o \"$OUTFILE\" $AUDIO -b 2000" 1>> $LOGFILE
nice -n 19 /usr/bin/HandBrakeCLI -i "$INFILE" -o "$OUTFILE" $AUDIO -b 2000 2>> $LOGFILE 1>> /dev/null
echo "Converted $INFILE to $OUTFILE" >> $LOGFILE