[mythtv-users] commflagging and cutlists

Yan Seiner yan at seiner.com
Sat Jan 18 23:18:45 UTC 2014


On 01/18/2014 03:09 PM, Michael Stucky wrote:
>
>
>
> I'm not sure why you need to look at the database. The actual symbols 
> are probably Theme specific, but using the Steppes theme and looking 
> at the highlighted recording (Media Library -> Watch Recordings -> All 
> Programs -> "Program Name") I have a light blue flag indicating that 
> the commflag job completed successfully and a darker blue handled 
> scissor indicating that I have edited the recording and now have a 
> cutlist.
>
I'm trying to script automatic transcoding to a roku3 (or h264/mkv for 
that matter.)

The script, such as it is, is attached.  Not quite a finished product 
(I'm having issues with audio sync on the roku with OTA MPEG2 -> h264 - 
works fine for other source material).

You need a functioning mediainfo (not strictly necessary as it's just 
used for information) and a working install of HandBrakeCLI.

I'd welcome sanity checking and/or feedback if anyone finds it useful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mythtv.org/pipermail/mythtv-users/attachments/20140118/abecdf25/attachment.html>
-------------- next part --------------
#!/bin/bash
#
# Save this as mythbrake.sh and make the file executable
# written by Yan Seiner, yan at seiner.com
#
# based on the previous mythbrake.sh script by
# Written by Ares Drake, ares.drake at gmail.com
# and on the mythtv-transcode-h264.sh
# written by Defcronyke Webmaster, copyright 2012.
#
# Licenced under GPL v3
# This Script shall be called as MythTV user job. It transcodes the DVB recordings (mpeg files) using Handbrake. It first checks whether the recording is HDTV. If so it will be reencoded with H.264 to save space. SDTV will have commercials cut out if necessary and will then be transcoded to H.263 (Xvid, DivX).
#
#USAGE:######################
# This Sript shall be called as a MythTV user job like as follows:
# /path/to/mythbrake.sh "%DIR%" "%FILE%" "%CHANID%" "%STARTTIME%" "%TITLE%" "%SUBTITLE%" "%CATEGORY%" "%PROGSTART%"
#############################
#
#
#REQUIREMENTS################
# You need to have the following programs installed:
# mediainfo: http://mediainfo.sourceforge.net/
# handbrake with dependencies: http://www.handbrake.fr
# Installation of these is convered on their sites
#############################


######SOME CONSTANSTS FOR USER EDITING######
############################################
logdir="/var/log/mythtv/transcodelogs" #change to your needs for logs
errormail="xxx at yyyy" # this email address will be informed in case of errors
outdir="/...../mythtv/recordings" # specify directory where you want the transcoded file to be placed
dbuser="mythtv"
dbpass="mythtv"
db="mythconverg"

# Number of threads to use (default uses all threads)
numthreads="auto"

######DEFINE SOME BASIC VARIABLES######
#######################################
scriptstarttime=$(date +%F-%H%M%S)
mythrecordingsdir="$1" # specify directory where MythTV stores its recordings
file="$2"
# using sed to sanitize the variables to avoid problematic file names, only alphanumerical, space, hyphen and underscore allowed, other characters are transformed to underscore
subtitle="$(echo "$6" | sed 's/[^A-Za-z0-9_-]/_/g')"
title="$(echo "$5" | sed 's/[^A-Za-z0-9_-]/_/g')"
category="$(echo "$7" | sed 's/[^A-Za-z0-9_-]/_/g')"
chanid="$3"
starttime="$4"
progstart="$8"
if [ -z "$category" ]
then
   category="Unknown" #name for unknown category, Unbekannt is German for unknown
fi
logfile="$logdir/$scriptstarttime-$title.log"
touch "$logfile"
chown mythtv:mythtv "$logfile"
chmod a+rw "$logfile"

filename="${title}_$(echo $progstart | sed 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1-\2-\3Z\4H\5/').mkv" # can be customized
outfile="$outdir/${filename}"

mypid=$$
tmpsql=/tmp/sql-$mypid.sql

######Variables finished

######DO SOME LOGGING######
###########################
echo "Transcode job $title starting at $scriptstarttime" >> "$logfile"
echo "Original file: $mythrecordingsdir/$file" >> "$logfile"
echo "Target file: $outfile" >> "$logfile"
echo "ChanId: $chanid Time: $starttime" >> "$logfile"

echo args: $@ >> "$logfile"
echo mythrecordingsdir: $mythrecordingsdir >> "$logfile"
echo file: $file >> "$logfile"
echo title: $title >> "$logfile"
echo subtitle: $subtitle >> "$logfile"
echo category: $category >> "$logfile"

echo filename: $filename >> "$logfile"
echo outfile: $outfile >> "$logfile"


######SOURCEFILE CHECK######
############################
if [ ! -f "$mythrecordingsdir/$file" ]
then
   #source file does not exist
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Error at $scriptstoptime: Source file not found " >> "$logfile"
   echo "Maybe wrong path or missing permissions?" >> "$logfile"
   mail -s "Mythtv Sourcefile Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
fi

echo "###################################" >> "$logfile"

#######COMMFLAG CHECK#######
############################
commflag=$(mysql -e "SELECT commflagged FROM recorded \
  WHERE chanid=$chanid AND starttime=$starttime;"\
  --batch --skip-column-names -u$dbuser -p$dbpass $db)

if [ $commflag = '0' ] ; then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Commflag at $scriptstoptime" >> "$logfile"
   echo "File $outfile not commflagged yet" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Commflag Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "Commflagged; proceeding." >> "$logfile"
fi

########CUTLIST CHECK#######
############################
cutlist=$(mysql -e "SELECT cutlist FROM recorded \
   WHERE chanid = $chanid AND starttime = $starttime;"\
   --batch --skip-column-names -u$dbuser -p$dbpass $db)

if [ $cutlist = '0' ] ; then # we don't have a cutlist so let's make one

   echo No cutlist found; creating one. >> "$logfile"

   echo mythutil: /usr/bin/mythutil \
      --chanid "$chanid" --starttime "$starttime" --gencutlist >> "$logfile"
   /usr/bin/mythutil --chanid "$chanid" --starttime "$starttime" --gencutlist 2>> $logfile

   if [ $? != 0 ] ; then
      scriptstoptime=$(date +%F-%H%M%S)
      echo "Cutlist Error at $scriptstoptime" >> "$logfile"
      echo "Interrupted file $outfile" >> "$logfile"
      echo "###################################" >> "$logfile"
      mail -s "Mythtv Cutlist Error on $HOSTNAME" "$errormail" < "$logfile"
      mv "$logfile" "$logfile-FAILED"
      exit 1
   else
      echo "Cutlist Run successfull." >> "$logfile"
   
   fi
else
   echo Found cutlist. >> "$logfile"
fi

echo "###################################" >> "$logfile"

echo File info before commercial cuts: >> "$logfile"
mediainfo $mythrecordingsdir/$file | grep 'Complete name' -A3 >> "$logfile"

echo mythtranscode: /usr/bin/mythtranscode \
   --chanid "$chanid" --starttime "$starttime" --mpeg2 --honorcutlist >> "$logfile" >> "$logfile"
/usr/bin/mythtranscode \
   --chanid "$chanid" --starttime "$starttime" --mpeg2 --honorcutlist >> "$logfile" 2>> "$logfile"

if [ $? != 0 ] ; then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Transcoding Error at $scriptstoptime" >> "$logfile"
   echo "Interrupted file $outfile" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Transcoding Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "Transcoding Run successfull." >> "$logfile"
fi

echo File info after commercial cuts: >> "$logfile"
mediainfo $mythrecordingsdir/$file.tmp | grep 'Complete name' -A3 >> "$logfile"

# echo "UPDATE recorded SET basename='$file.tmp' WHERE chanid='$chanid' AND starttime='$starttime';" > $tmpsql
# fix seeking and bookmarking by removing stale db info
echo "DELETE FROM recordedseek WHERE chanid='$chanid' AND starttime='$starttime';" > $tmpsql
echo "DELETE FROM recordedmarkup WHERE chanid='$chanid' AND starttime='$starttime';" >> $tmpsql

echo "updating database" >> "$logfile"
cat $tmpsql >> "$logfile"

mysql --user=$dbuser --password=$dbpass $db < $tmpsql

if [ $? != 0 ] ; then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "mysql Error at $scriptstoptime" >> "$logfile"
   echo "update failed for file $outfile" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Mysql Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "mysql update successfull." >> "$logfile"
fi

echo "###################################" >> "$logfile"

hbopts="-e x264  \
  -q 20.0 \
  -a 1,1 \
  -E copy:ac3 \
  -B 160,160 \
  -6 dpl2,none \
  -R Auto,Auto \
  -D 0.0,0.0 \
  --audio-copy-mask ac3,dtshd,dts,mp3 \
  --audio-fallback ffac3 \
  -f mkv \
  -4 \
  --decomb \
  --loose-anamorphic \
  --modulus 2 \
  -m \
  --x264-preset medium \
  --h264-profile high \
  --h264-level 4.1"

echo handbrake: HandBrakeCLI -i "$mythrecordingsdir/$file.tmp" \
  -o "$outfile" $hbopts --optimize >> "$logfile"
HandBrakeCLI -i "$mythrecordingsdir/$file.tmp" -o "$outfile" $hbopts --optimize 2>> "$logfile"

if [ $? != 0 ]
# There were errors in the Handbrake Run.
then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Handbrake Error at $scriptstoptime" >> "$logfile"
   echo "Interrupted file $outfile" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Handbrake Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "Transcode Run successfull." >> "$logfile"
fi

#check if outfile exists
if [ ! -s "$outfile" ];
then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Output-Error at $scriptstoptime" >> "$logfile"
   echo "$outfile doesn't exist or is 0 bytes"  >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Output Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
fi

echo File info for h264: >> "$logfile"
mediainfo $outfile | grep 'Complete name' -A3 >> "$logfile"

newfilesize=`du -b "$outfile" | cut -f1`
echo "UPDATE recorded SET basename='$filename',filesize='$newfilesize',transcoded='1' WHERE chanid='$chanid' AND starttime='$starttime';" > $tmpsql

echo "updating database" >> "$logfile"
cat $tmpsql >> "$logfile"

mysql --user=$dbuser --password=$dbpass $db < $tmpsql

if [ $? != 0 ] ; then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "mysql Error at $scriptstoptime" >> "$logfile"
   echo "update failed for file $outfile" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Mythtv Mysql Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "mysql update successfull." >> "$logfile"
fi

echo "###################################" >> "$logfile"

echo "Removing temporary files: $mythrecordingsdir/$file.tmp $tmpsql" >> "$logfile"
rm $mythrecordingsdir/$file.tmp $tmpsql

if [ $? != 0 ] ; then
   scriptstoptime=$(date +%F-%H%M%S)
   echo "Cleanup error at $scriptstoptime" >> "$logfile"
   echo "could not remove temporary files: $mythrecordingsdir/$file.tmp $tmpsql" >> "$logfile"
   echo "###################################" >> "$logfile"
   mail -s "Cleanup Error on $HOSTNAME" "$errormail" < "$logfile"
   mv "$logfile" "$logfile-FAILED"
   exit 1
else
   echo "Cleanup successfull." >> "$logfile"
fi

scriptstoptime=$(date +%F-%H:%M:%S)
echo "Successfully finished at $scriptstoptime" >> "$logfile"
echo "Transcoded file: $outfile" >> "$logfile"

exit 0



More information about the mythtv-users mailing list