Script - commercialremover

From MythTV Official Wiki
Jump to: navigation, search

This script was started based upon the old script, which was needed as part of the MythGrid plugin development. It was necessary to change it so significantly to get the required behavior, that it seemed cleaner to post it as a new script.

Warning.png Warning: This behavior is not supported within the mythutil/mythtranscode programs themselves as the commercial detector is not considered sufficiently accurate to permanently remove sections of video without a manual confirmation. Original copies of the file will be stored with the .old extension, so you may want to delete them periodically, or just comment out the mv line below.


In order to run the script as a user job, add the following command as a user job in your backend setup.

commercialremover.sh %DIR% %FILE% %CHANID% %STARTTIMEUTC%


Important.png Note: This script attempts to acquire database configuration from /etc/mythtv/mysql.txt, /etc/mythtv/config.xml, and then ~/.mythtv/config.xml using whichever it finds first. If none are found or cannot be read, the script will not update the database to reflect the new file size, but the commercials have already been removed.

Also, if interested in creating logs be sure the user can edit /tmp/ folder.


The following script can be setup as a User Job to automatically flag and remove commercials from a recording. This script code was updated 11/21/2013 to work with MythTV 0.27. It should still work with 0.26 as well.


Script.png commercialremover.sh

#####   commercialremover.sh Script for MythTV, updated 2013/12/15
###
##	Made for MythTV 0.26 also tested against 0.27, by jmw for MythGrid
##	https://sourceforge.net/projects/mythgrid/ 
##
##      This script requires the User Job to pass additional arguments under MythTV 0.26 or higher
##	User job command:
##      ‘commercialremover.sh %DIR% %FILE% %CHANID% %STARTTIMEUTC%'
###
#####
VIDEODIR=$1
FILENAME=$2
CHAN=$3
START=$4

# Sanity checking, to make sure everything is in order. Modified to check $CHAN and $START for MythTV 0.25 or higher support
if [ -z "$VIDEODIR" -o -z "$FILENAME" -o -z "$CHAN" -o -z "$START" ]; then
        echo "Usage: $0 <VideoDirectory> <FileName>"
        exit 5
fi
if [ ! -f "$VIDEODIR/$FILENAME" ]; then
        echo "File does not exist: $VIDEODIR/$FILENAME"
        exit 6
fi

#create temp file name
FILENAMEPREFIX="${CHAN}_${START}"
INFILEPATH="$VIDEODIR/$FILENAME"
TEMPFILEPATH="$INFILEPATH.tmp"
MAPEXT=".map"

LOGFOLDERPATH="/tmp/mythtv/commercialRemoval"
if [ ! -d "$LOGFOLDERPATH" ]; then
	mkdir -p $LOGFOLDERPATH
fi

LOG="$LOGFOLDERPATH/${FILENAMEPREFIX}.log"

echo "Generating cutlist..."
mythutil -q --gencutlist --chanid $CHAN --starttime $START > /dev/null 2>&1
ERROR=$?
if [ $ERROR -ne 0 ]; then
	echo "Generating cutlist failed for ${FILENAME} with error $ERROR" >> $LOG 2>&1
	exit $ERROR
fi

echo "Transcoding..."
mythtranscode --honorcutlist --allkeys -v all,jobqueue --showprogress --mpeg2 -i $INFILEPATH -o $TEMPFILEPATH > /dev/null 2>&1
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Transcoding failed for ${FILENAME} with error $ERROR" >> $LOG 2>&1
        exit $ERROR
fi

#Save a copy of the original with the extension .old
#mv -f $INFILEPATH $INFILEPATH.old

#Overwrite the original file with the transcoded file
mv -f $TEMPFILEPATH $INFILEPATH
#Sometimes a .map file is generated, clean that up too
rm -f $TEMPFILEPATH$MAPEXT

#clear out the old cutlist
echo "Clearing cutlist..."
mythutil -q --clearcutlist --chanid $CHAN --starttime $START > /dev/null 2>&1

echo "Clearing skiplist..."
mythutil -q --clearskiplist --chanid $CHAN --starttime $START > /dev/null 2>&1

#Determine new filesize to update the db with
UPDATEDFILESIZE=$(du -b $INFILEPATH | awk '{print $1}') 

#Get mythtv database information
DBSERVER=""
DBUSER=""
DBNAME=""
DBPASS=""

#Try getting mythtv database information from existant files in the following order
MYTHDBFILE="/etc/mythtv/mysql.txt"
MYTHCONFIGFILE="/etc/mythtv/config.xml"
MYCONFIGFILE="~/.mythtv/config.xml"

if [ -f "$MYTHDBFILE" ]; then
	DBSERVER=$( grep "DBHostName=" $MYTHDBFILE | sed s/.*DBHostName=/\/ )
	DBUSER=$( grep "DBUserName=" $MYTHDBFILE | sed s/.*DBUserName=/\/ )
	DBNAME=$( grep "DBName=" $MYTHDBFILE | sed s/.*DBName=/\/ )
	# Determine database password
	DBPASS=$( grep "DBPassword=" $MYTHDBFILE | sed s/.*DBPassword=/\/ )
elif [ -f "$MYTHCONFIGFILE" ]; then
	DBSERVER=$( grep -E -m 1 -o "<DBHostName>(.*)</DBHostName>" $MYTHCONFIGFILE | sed -e 's,.*<DBHostName>\([^<]*\)</DBHostName>.*,\1,g' )
	DBUSER=$( grep -E -m 1 -o "<DBUserName>(.*)</DBUserName>" $MYTHCONFIGFILE | sed -e 's,.*<DBUserName>\([^<]*\)</DBUserName>.*,\1,g' )
	DBNAME=$( grep -E -m 1 -o "<DBName>(.*)</DBName>" $MYTHCONFIGFILE | sed -e 's,.*<DBName>\([^<]*\)</DBName>.*,\1,g' )
	DBPASS=$( grep -E -m 1 -o "<DBPassword>(.*)</DBPassword>" $MYTHCONFIGFILE | sed -e 's,.*<DBPassword>\([^<]*\)</DBPassword>.*,\1,g' )
elif [ -f "$MYCONFIGFILE" ]; then
	DBSERVER=$( grep -E -m 1 -o "<Host>(.*)</Host>" $MYCONFIGFILE | sed -e 's,.*<Host>\([^<]*\)</Host>.*,\1,g' )
	DBUSER=$( grep -E -m 1 -o "<UserName>(.*)</UserName>" $MYCONFIGFILE | sed -e 's,.*<UserName>\([^<]*\)</UserName>.*,\1,g' )
	DBNAME=$( grep -E -m 1 -o "<DatabaseName>(.*)</DatabaseName>" $MYCONFIGFILE | sed -e 's,.*<DatabaseName>\([^<]*\)</DatabaseName>.*,\1,g' )
	DBPASS=$( grep -E -m 1 -o "<Password>(.*)</Password>" $MYCONFIGFILE | sed -e 's,.*<Password>\([^<]*\)</Password>.*,\1,g' )
fi

if [ -z "$DBSERVER" -o -z "$DBUSER" -o -z "$DBPASS" -o -z "$DBNAME" ]; then
	# Update the database entry for the file size
	echo "UPDATE recorded SET filesize='${UPDATEDFILESIZE}' WHERE chanid='${CHAN}' && starttime='${START}'" | mysql -h ${DBSERVER} -u ${DBUSER} --password=${DBPASS} ${DBNAME}
fi

echo "Commericals removed for $FILENAME" >> $LOG 2>&1
exit 0

Version:
0.27
Works with 0.27
[Download commercialremover.sh] Watch Article For Changes