Difference between revisions of "Script - RemoveCommercials"

From MythTV Official Wiki
Jump to: navigation, search
m (adjust categories)
Line 105: Line 105:
 
         cat << EOF | mysql mythconverg
 
         cat << EOF | mysql mythconverg
  
 
 
[[Category:Scripts]]
 
 
[[Category:User_Job_Scripts]]
 
[[Category:User_Job_Scripts]]
 +
[[Category:Transcode_Scripts]]

Revision as of 20:50, 12 November 2010

Warning.png Warning: This behavior is not supported within the mythcommflag/mythtranscode programs themselves as the commercial detector is not considered sufficiently accurate to permanently remove sections of video without a manual confirmation.

The follow script can be setup as a User Job to automatically flag and remove commercials from a recording.


Script.png remove_commercials.sh

#!/bin/sh
VIDEODIR=$1
FILENAME=$2
# Sanity checking, to make sure everything is in order.
if [ -z "$VIDEODIR" -o -z "$FILENAME" ]; then
        echo "Usage: $0 <VideoDirectory> <FileName>"
        exit 5
fi
if [ ! -f "$VIDEODIR/$FILENAME" ]; then
        echo "File does not exist: $VIDEODIR/$FILENAME"
        exit 6
fi
# The meat of the script. Flag commercials, copy the flagged commercials to
# the cutlist, and transcode the video to remove the commercials from the
# file.
mythcommflag -f $VIDEODIR/$FILENAME
ERROR=$?
if [ $ERROR -gt 126 ]; then
        echo "Commercial flagging failed for ${FILENAME} with error $ERROR"
        exit $ERROR
fi
mythcommflag --gencutlist -f $VIDEODIR/$FILENAME
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Copying cutlist failed for ${FILENAME} with error $ERROR"
        exit $ERROR
fi
mythtranscode --honorcutlist --showprogress -i $VIDEODIR/$FILENAME -o $VIDEODIR/$FILENAME.tmp
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Transcoding failed for ${FILENAME} with error $ERROR"
        exit $ERROR
fi
mv $VIDEODIR/$FILENAME $VIDEODIR/$FILENAME.old
mv $VIDEODIR/$FILENAME.tmp $VIDEODIR/$FILENAME
mythcommflag -f $VIDEODIR/${FILENAME} --rebuild
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Rebuilding seek list failed for ${FILENAME} with error $ERROR"
        exit $ERROR
fi
mythcommflag --clearcutlist -f $VIDEODIR/$FILENAME
ERROR=$?
if [ $ERROR -eq 0 ]; then
        # Fix the database entry for the file
        cat << EOF | mysql mythconverg
UPDATE 
        recorded
SET
        cutlist = 0,
        filesize = $(ls -l $VIDEODIR/$FILENAME | awk '{print $5}') 
WHERE
        basename = '$FILENAME';
EOF
        exit 0
else
        echo "Clearing cutlist failed for ${FILENAME} with error $ERROR"
        rm -f $VIDEODIR/$FILENAME.tmp
        exit $ERROR
fi

See the discussion tab for tips on using the mpeg2 lossless transcoding.

If you use this script and have commercial auto-skip set then during playback you will notice that MythTV skips over parts of your program. This is because the information about where commercials are located is still available. Adding this query after the UPDATE and before the EOF will remove the commercial points and allow things to run smoothly.

DELETE FROM 
       `recordedmarkup`
WHERE 
       CONCAT( chanid, starttime ) IN (
               SELECT 
                       CONCAT( chanid, starttime )
               FROM 
                       recorded
               WHERE 
                       basename = '$FILENAME'
      );


Please note that this script will save the original file as ${FILENAME}.old. You should periodically remove these files to save space. If you want to remove them automatically, you could use the following command:

find /var/video -name \*.old -ctime 2 -exec rm {} \;

Place this command into a script in /etc/cron.daily or into crontab. This will remove all backup files that are older than 2 days.

Note:

I update this script as I make changes to my own system. You should watch this article so you're notified of changes as they occur. If you do not have a wiki account and do not wish to create one you should check back periodically to get changes.

Note2:

The above presumes that your root account has no password set. If you have a root password set, you'll get the following error in /var/log/mythtv/mythbackend.log: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

You can fix this by modifying the script to be (presuming you have mythtv as a user with mythtv as the password):

       cat << EOF | mysql -u mythtv -pmythtv mythconverg

instead of

       cat << EOF | mysql mythconverg