Difference between revisions of "Script - RemoveCommercials"

From MythTV Official Wiki
Jump to: navigation, search
(Fix a couple of problems and inconsistencies pointed out by Jack Perveiler)
(Old version didn't work, move the new into place sooner so that we can remove the cutlist and rebuild the seek table. Also make exit codes correspond to the error in question.)
Line 2: Line 2:
  
 
  #!/bin/sh
 
  #!/bin/sh
# removecommercials.sh
 
#
 
# Copyright 2006 Zach White <zwhite-mythtv@darkstar.frop.org>
 
#
 
# Permission to use, copy, modify, and distribute this software and
 
# its documentation for any purpose and without fee is hereby granted,
 
# provided that the above copyright notice appear in all copies and
 
# that both that copyright notice and this permission notice appear in
 
# supporting documentation.
 
#
 
# THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
# FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 
# SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
#
 
 
  VIDEODIR=$1
 
  VIDEODIR=$1
 
  FILENAME=$2
 
  FILENAME=$2
Line 27: Line 9:
 
         exit 5
 
         exit 5
 
  fi
 
  fi
  if [ ! -f $VIDEODIR/$FILENAME ]; then
+
  if [ ! -f "$VIDEODIR/$FILENAME" ]; then
 
         echo "File does not exist: $VIDEODIR/$FILENAME"
 
         echo "File does not exist: $VIDEODIR/$FILENAME"
 
         exit 6
 
         exit 6
Line 38: Line 20:
 
  if [ $ERROR -gt 126 ]; then
 
  if [ $ERROR -gt 126 ]; then
 
         echo "Commercial flagging failed for ${FILENAME} with error $ERROR"
 
         echo "Commercial flagging failed for ${FILENAME} with error $ERROR"
         exit 1
+
         exit $ERROR
 
  fi
 
  fi
 
  mythcommflag --gencutlist -f $VIDEODIR/$FILENAME
 
  mythcommflag --gencutlist -f $VIDEODIR/$FILENAME
Line 44: Line 26:
 
  if [ $ERROR -ne 0 ]; then
 
  if [ $ERROR -ne 0 ]; then
 
         echo "Copying cutlist failed for ${FILENAME} with error $ERROR"
 
         echo "Copying cutlist failed for ${FILENAME} with error $ERROR"
         exit 2
+
         exit $ERROR
 
  fi
 
  fi
 
  mythtranscode --honorcutlist --showprogress -i $VIDEODIR/$FILENAME -o $VIDEODIR/$FILENAME.tmp
 
  mythtranscode --honorcutlist --showprogress -i $VIDEODIR/$FILENAME -o $VIDEODIR/$FILENAME.tmp
Line 50: Line 32:
 
  if [ $ERROR -ne 0 ]; then
 
  if [ $ERROR -ne 0 ]; then
 
         echo "Transcoding failed for ${FILENAME} with error $ERROR"
 
         echo "Transcoding failed for ${FILENAME} with error $ERROR"
         exit 3
+
         exit $ERROR
 
  fi
 
  fi
  mythcommflag -f $VIDEODIR/${FILENAME}.tmp --rebuild
+
mv $VIDEODIR/$FILENAME $VIDEODIR/$FILENAME.old
 +
mv $VIDEODIR/$FILENAME.tmp $VIDEODIR/$FILENAME
 +
  mythcommflag -f $VIDEODIR/${FILENAME} --rebuild
 
  ERROR=$?
 
  ERROR=$?
 
  if [ $ERROR -ne 0 ]; then
 
  if [ $ERROR -ne 0 ]; then
 
         echo "Rebuilding seek list failed for ${FILENAME} with error $ERROR"
 
         echo "Rebuilding seek list failed for ${FILENAME} with error $ERROR"
         exit 4
+
         exit $ERROR
 
  fi
 
  fi
 
  mythcommflag --clearcutlist -f $VIDEODIR/$FILENAME
 
  mythcommflag --clearcutlist -f $VIDEODIR/$FILENAME
 
  ERROR=$?
 
  ERROR=$?
 
  if [ $ERROR -eq 0 ]; then
 
  if [ $ERROR -eq 0 ]; then
        mv $VIDEODIR/$FILENAME $VIDEODIR/$FILENAME.old
 
        mv $VIDEODIR/$FILENAME.tmp $VIDEODIR/$FILENAME
 
 
         # Fix the database entry for the file
 
         # Fix the database entry for the file
         cat << EOF | mysql mythconverg
+
         cat << EOF  
  UPDATE
+
  UPDATE  
 
         recorded
 
         recorded
 
  SET
 
  SET
 
         cutlist = 0,
 
         cutlist = 0,
         filesize = $(ls -l $VIDEODIR/$FILENAME | awk '{print $5}')
+
         filesize = $(ls -l $VIDEODIR/$FILENAME | awk '{print $5}')  
 
  WHERE
 
  WHERE
 
         basename = '$FILENAME';
 
         basename = '$FILENAME';
Line 76: Line 58:
 
  else
 
  else
 
         echo "Clearing cutlist failed for ${FILENAME} with error $ERROR"
 
         echo "Clearing cutlist failed for ${FILENAME} with error $ERROR"
         rm $VIDEODIR/$FILENAME.tmp
+
         rm /usr/video/$FILENAME.tmp
         exit 5
+
         exit $ERROR
 
  fi
 
  fi
  

Revision as of 02:34, 6 October 2006

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

#!/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 
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 /usr/video/$FILENAME.tmp
        exit $ERROR
fi

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.