summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/linhes-scripts/removecommercials.sh
blob: 0497dd5b333f0ec0ca09abf65fa6796d948dbbbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/sh
# remove commercials from recordings using the user generated cutlist or
# optionally using the mythcommflag generated cutlist.
# version 0.3  9/1/2010

# usage:
# first parameter must be %DIR% of the recording
# second parameter must be %FILE% of the recording
# third parameter must be %CHANID%
# fourth parameter must be %STARTTIME%
# fifth parameter must be %JOBID% for the User Job status to be updated in MythTV
# in the mythtv setup screen invoke this script like this:
# MYTHTV User Job Command:
# /usr/LH/bin/removecommercials.sh "%DIR%" "%FILE%" "%CHANID%" "%STARTTIME%" "%JOBID%"

#-------OPTIONS----------------
# If no cutlist is found USE_MYTHCOMMFLAG_CUTLIST=YES will use mythcommflag 
#          to generate a cutlist.
# WARNING: Using this option could result in part of the recording being cut if 
#          mythcommflag incorrectly flagged the commercial.
# The USE_MYTHCOMMFLAG_CUTLIST option is saved in ~/.removecommercials.cfg and will
#          not be overwritten by updates to this script. The ~/.removecommercials.cfg
#          will be created by this script if it doesn't exist.
# To enable set USE_MYTHCOMMFLAG_CUTLIST=YES in ~/.removecommercials.cfg

[[ ! -f ~/.removecommercials.cfg ]] && printf "# If no cutlist is found USE_MYTHCOMMFLAG_CUTLIST=YES will use mythcommflag\n#          to generate a cutlist.\n# WARNING: Using this option could result in part of the recording being cut if\n#          mythcommflag incorrectly flagged the commercial.\nUSE_MYTHCOMMFLAG_CUTLIST=NO" > ~/.removecommercials.cfg
. ~/.removecommercials.cfg

# Auguments passed from command line
VIDEODIR=$1
FILENAME=$2
CHANID=$3
STARTTIME=$4
JOBID=$5

# database settings
BACKEND_HOSTNAME=${BACKEND_HOSTNAME:-"localhost"}
DBUSERNAME=${DBUSERNAME:-"mythtv"}
DBPASSWORD=${DBPASSWORD:-"mythtv"}
SQLCMD="mysql -u $DBUSERNAME --password=$DBPASSWORD -h $BACKEND_HOSTNAME mythconverg -e"

LOG="/dev/null"
#LOG="/var/log/mythtv/removecommercials.log"

#------FUNCTIONS---------------
update_comment()
# Arg_1 = COMMENT
{
if [ $NO_JOBID = 0 ]; then
    `$SQLCMD "update jobqueue set comment=\"$1\" where id=\"$JOBID\";"`
fi
}

update_status()
# Arg_1 = status code
{
if [ $NO_JOBID = 0 ]; then
    `$SQLCMD "update jobqueue set status=\"$1\" where id=\"$JOBID\";"`
fi
}

check_myth_jobcmds()
# check the myth database for stop pause or resume commands
{
if [ $NO_JOBID = 0 ]; then
    CURRENT_CMD=`$SQLCMD "select cmds from jobqueue where id=\"$JOBID\";" | sed '/[0-9]/!d'`
    case "$CURRENT_CMD" in
	# JOB_RUN
	0) ;;
	# JOB_PAUSE
	1) update_status 6 
	    kill -s STOP $TPID ;;
	# JOB_RESUME
	2) update_status 4
       `$SQLCMD "update jobqueue set cmds=\"0\" where id=\"$JOBID\";"` 
       kill -s CONT $TPID ;;
	# JOB_STOP
	4) update_status 5
	   `$SQLCMD "update jobqueue set cmds=\"0\" where id=\"$JOBID\";"`
	   kill -9 $TPID
       clean_up_files
       echo "Cancelled"
       update_status 320
 	   exit ;;
    esac
fi
}

check_background_progress()
#check progress in background
{
while [ `tail -1 $STATUSFILE | grep -c "Done"` = 0 ]
do
    sleep 5
    check_myth_jobcmds
    current_status=`tail -1 $STATUSFILE`
    if [ `expr match "$current_status" '.*\complete'` -ne 0 ]; then
        prog_percent=`echo "$current_status" | awk '{print $3}'`
        if [ -n "$prog_percent" ]; then
            echo "Removing Commercials - $prog_percent Completed"
            update_comment "Removing Commercials - $prog_percent Completed"
        fi
    fi
done
}

get_pid()
{
process_name=""
i1=1
while [ "$process_name" != "found" ]; do
   if [ "`ps $TPID | grep mythtranscode | sed 's_.*\(mythtranscode\).*_\1_'`" = "mythtranscode" ]; then
      process_name="found"
   else
      TPID=`expr $TPID + 1`
   fi
   i1=`expr $i1 + 1`
   if [ $i1 -gt 20 ]; then
      break
   fi
done
}

clean_up_files()
# clean up left over files
{
unlink $TMPFILE 2> /dev/null
unlink $TMPFILE.map 2> /dev/null
unlink $STATUSFILE 2> /dev/null
unlink $VIDEODIR/$FILENAME.tmp 2> /dev/null
}

#-------MAIN SCRIPT------------
# check if %JOBID% is passed from command line
JOBID=$5
if [ -z "$JOBID" ]; then
    NO_JOBID=1
else
    NO_JOBID=0
fi
# check if file is a .mpg
if [ `expr match "$FILENAME" '.*\.mpg'` -ne 0 ]; then
    MPEG="--mpeg2"
else
    MPEG=""
fi

# create temp filename so multiple instances won't conflict
TMPNAME=rmvCOMMS-$$
TMPFILE=$VIDEODIR/$FILENAME-$$
STATUSFILE=/myth/tmp/$TMPNAME-status.log

touch $STATUSFILE

update_status 4

check_myth_jobcmds

echo "" >> $LOG
date >> $LOG
$VIDEODIR/$FILENAME >> $LOG

# check for cutlist
MYTHCOMMFRAMES=`mythcommflag --getcutlist -f $VIDEODIR/$FILENAME | grep 'Cutlist:' | cut -d \  -f 2`

if [ $USE_MYTHCOMMFLAG_CUTLIST=YES ] && [ -z "$MYTHCOMMFRAMES" ]; then
    echo "Generating cutlist..." >> $LOG
    update_comment "Generating cutlist..."
    /usr/bin/nice -n19 /usr/bin/mythcommflag --gencutlist -f $VIDEODIR/$FILENAME
    MYTHCOMMFRAMES=`mythcommflag --getcutlist -f $VIDEODIR/$FILENAME | grep 'Cutlist:' | cut -d \  -f 2`
fi

if [ -n "$MYTHCOMMFRAMES" ]; then
    echo "Removing Commercials..." >> $LOG
    update_comment "Removing Commercials..."
    ( /usr/bin/nice -n19 /usr/bin/mythtranscode -c $CHANID -s $STARTTIME -o $TMPFILE $MPEG --honorcutlist --showprogress > $STATUSFILE 2>&1 ; echo "Done" >> $STATUSFILE ) &
    TPID=$!
    get_pid
    check_background_progress
    ERROR=$?
    if [ $ERROR -ne 0 ]; then
        echo "Transcoding failed for ${FILENAME} with error $ERROR" >> $LOG
        exit $ERROR
    fi

    check_myth_jobcmds
    # move temp file to output location
    echo "Moving file..." >> $LOG
    update_comment "Moving file..."
    if [ `$SQLCMD "select data from settings where value='SaveTranscoding';" | sed '/[0-9]/!d'` = 1 ]; then
        echo "DB is set to save transcoding" >> $LOG
        mv $VIDEODIR/$FILENAME $VIDEODIR/$FILENAME.old
    fi
    mv $TMPFILE $VIDEODIR/$FILENAME

    # file has changed, rebuild index
    echo "Rebuilding index..." >> $LOG
    update_comment "Rebuilding index..."
    mythcommflag -c $CHANID -s $STARTTIME --rebuild
    ERROR=$?
    if [ $ERROR -ne 0 ]; then
        echo "Rebuilding seek list failed for ${FILENAME} with error $ERROR" >> $LOG
        exit $ERROR
    fi

    # remove old cutlist
    echo "Removing old cutlist..." >> $LOG
    update_comment "Removing old cutlist..."
    mythcommflag -c $CHANID -s $STARTTIME --clearcutlist
    ERROR=$?
    if [ $ERROR -eq 0 ]; then
        # Fix the database entry for the file
        echo "Fixing database entry..." >> $LOG
        `$SQLCMD "UPDATE recorded SET cutlist = 0, filesize = $(ls -l $VIDEODIR/$FILENAME | awk '{print $5}') WHERE basename = '$FILENAME';"` > /dev/null
    else
        echo "Clearing cutlist failed for ${FILENAME} with error $ERROR" >> $LOG
        clean_up_files
        update_status 272
        update_comment "Error: Clearing cutlist failed."
        exit $ERROR
    fi

    # clear autoskip list
    echo "Clearing autoskip list..." >> $LOG
    `$SQLCMD "DELETE FROM recordedmarkup WHERE CONCAT( chanid, starttime ) IN (SELECT CONCAT( chanid, starttime ) FROM recorded WHERE basename = '$FILENAME');"` > /dev/null

    clean_up_files
    echo "Commercials Removed" >> $LOG
    update_status 272
    update_comment "Sucessfully Completed." 
else
    echo "No cutlist found." >> $LOG
fi