summaryrefslogtreecommitdiffstats
path: root/linhes/linhes-system/myth2videos
blob: c4d3246647d39bf6f8e42d8cc59100b4d4f30437 (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
#!/bin/sh
# copy recording to videos
# version 0.3

# usage:
# first parameter must be %DIR%/%FILE% of the recording
# second parameter must be the desired base name of the output
# third parameter must be %CHANID% if you set USECUTLIST=Y
# fourth parameter must be %STARTTIME% if you set USECUTLIST=Y
# 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/bin/myth2videos "%DIR%/%FILE%" "%TITLE% - %SUBTITLE%" "%CHANID%" "%STARTTIME%" "%JOBID%"

# options:
USECUTLIST=Y		# Y or N

# where the video is stored
OUT_DIR=/data/storage/disk0/media/video


#------FUNCTIONS---------------
update_comment()
# Arg_1 = COMMENT
{
if [ $NO_JOBID -eq 0 ]; then
    `jobqueue_helper.py -j ${JOBID} -cs "${1}"`
fi
}

update_status()
# Arg_1 = status code
{
if [ $NO_JOBID -eq 0 ]; then
    `jobqueue_helper.py -j ${JOBID} -cs "${1}"`
fi
}

check_myth_jobcmds()
# check the myth database for stop pause or resume commands
{
if [ $NO_JOBID -eq 0 ]; then
    CURRENT_CMD=`jobqueue_helper.py -m "select cmds from jobqueue where id = ${JOBID}"`
    case "$CURRENT_CMD" in
	# JOB_RUN
	0) ;;
	# JOB_PAUSE
    1) `jobqueue_helper.py -j ${JOBID} -ss 6`;;
	# JOB_RESUME
    2) `jobqueue_helper.py -j ${JOBID} -ss 4`
       `jobqueue_helper.py -j ${JOBID} -cmds 0`;;
	# JOB_STOP
    4) `jobqueue_helper.py -j ${JOBID} -ss 5`
       `jobqueue_helper.py -j ${JOBID} -cmds 0`
       clean_up_files
       echo "Copy Cancelled" >> $LOGFILE
       `jobqueue_helper.py -j ${JOBID} -ss 320`
 	   exit ;;
    esac
fi
}

clean_up_files()
# clean up left over files
{
unlink $TMPFILE 2> /dev/null
unlink $TMPFILE.map 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

# create temp filename so multiple instances won't conflict
TMPNAME=toVIDEOS-$$
TMPFILE=/data/storage/disk0/media/tmp/$TMPNAME.mpg
MENINPUTFILE=$1
TITLE=`echo $2 | sed 's/\//_/g'`

# log file location
LOGFILE=/var/log/mythtv/myth2videos.log
CDate="`date`"
echo "" >> $LOGFILE
echo $CDate >> $LOGFILE
echo "File to copy: $MENINPUTFILE     Name: $TITLE" >> $LOGFILE
echo "$2   $3   $4   $5" >> $LOGFILE

# start timer
beforetime="$(date +%s)"

check_myth_jobcmds

# check if using cutlist
if [ $USECUTLIST = Y ]; then
    MYTHCOMMFRAMES=`mythutil --getcutlist --chanid "$3" --starttime "$4" | grep 'Cutlist:' | cut -d \  -f 2`
    echo $MYTHCOMMFRAMES
    if [ -n "$MYTHCOMMFRAMES" ]; then
        echo "Extracting Cutlist..." >> $LOGFILE
        update_comment "Extracting Cutlist..."
        /usr/bin/nice -n19 /usr/bin/mythtranscode --chanid "$3" --starttime "$4" --outfile "$TMPFILE" --mpeg2 --honorcutlist
    else
        update_comment "Copying Recording..."
        cp "$MENINPUTFILE" "$TMPFILE"
    fi
elif [ $USECUTLIST = N ]; then
    update_comment "Copying Recording..."
    cp "$MENINPUTFILE" "$TMPFILE"
fi

# make output filename unique
OUTPUTFILE=$OUT_DIR/$TITLE.mpg
i=1
while [ -e "$OUTPUTFILE" ]
do
    OUTPUTFILE=$OUT_DIR/$TITLE-$i.mpg
    i=`expr $i + 1`
done

# move temp file to output location
chown mythtv "$TMPFILE" && mv "$TMPFILE" "$OUTPUTFILE"

# stop timer
aftertime="$(date +%s)"
seconds="$(expr $aftertime - $beforetime)"

ERROR=$?
if [ $ERROR -eq 0 ]; then
    echo "File Encoded Successfully: $OUTPUTFILE" >> $LOGFILE
    hours=$((seconds / 3600))
    seconds=$((seconds % 3600))
    minutes=$((seconds / 60))
    seconds=$((seconds % 60))
    echo "Encoding took $hours hour(s) $minutes minute(s) $seconds second(s) @ $current_FPS fps." >> $LOGFILE
    update_status 272
    update_comment "Encode Successful. Encoding Time: $hours hour(s) $minutes minute(s) $seconds second(s)"
else
    update_status 304
    update_comment "Encode Failed.  Exit status: $ERROR"
    echo "ERROR: $ERROR" >> $LOGFILE
fi

clean_up_files