-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcode.py
executable file
·121 lines (100 loc) · 3.9 KB
/
transcode.py
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
#!/usr/bin/env python
from MythTV import Job, Recorded, System, MythDB, mythproto, MythError, MythLog
from MythTV.static import LOGMASK, LOGLEVEL
from optparse import OptionParser
import sys
import os
################################
#### adjust these as needed ####
TRANSCODER = '/video/roku/transcode.sh'
flush_commskip = True
build_seektable = True
################################
class TranscodeJob:
def __init__(self):
self.l = MythLog('transcode.py', db=self.db())
def db(self):
return MythDB()
def log(self, message, level=LOGLEVEL.INFO, detail=None):
return self.l.log(LOGMASK.GENERAL, level, message, detail)
def logTB(self):
return self.l.logTB(LOGMASK.GENERAL)
def run(self, jobid=None, chanid=None, starttime=None):
if jobid:
job = Job(jobid, db=self.db())
chanid = job.chanid
starttime = job.starttime
rec = Recorded((chanid, starttime), db=self.db())
sg = mythproto.findfile(rec.basename, rec.storagegroup, db=self.db())
if sg is None:
self.log('Local access to recording not found.', LOGLEVEL.ERR)
sys.exit(1)
infile = os.path.join(sg.dirname, rec.basename)
#### list of segments to be cut
# rec.markup.gencutlist()
#### list of segments to keep
# rec.markup.genuncutlist()
del(rec)
task = System(path=TRANSCODER, db=self.db())
try:
outfile = task(infile).strip()
if not os.path.exists(outfile):
raise OSError('output file %s not found' % repr(outfile))
except MythError, e:
self.log('Transcode failed with output:', LOGLEVEL.ERR,
task.stderr)
sys.exit(task.returncode)
except OSError, e:
self.log('Transcode failed to produce an output file:',
LOGLEVEL.ERR, repr(e))
sys.exit(1)
rec = Recorded((chanid, starttime), db=self.db())
rec.basename = os.path.basename(outfile)
os.remove(infile)
rec.filesize = os.path.getsize(outfile)
rec.transcoded = 1
rec.seek.clean()
if flush_commskip:
for index,mark in reversed(list(enumerate(rec.markup))):
if mark.type in (rec.markup.MARK_COMM_START,
rec.markup.MARK_COMM_END):
del rec.markup[index]
rec.bookmark = 0
rec.cutlist = 0
rec.markup.commit()
rec.update()
if build_seektable:
try:
task = System(path='mythcommflag', db=self.db())
task.command('--chanid %s' % chanid,
'--starttime %s' % starttime)
except MythError, e:
self.log('Mythcommflag --chanid %s --starttime %s failed: %s' %
(chanid, starttime, str(e)), LOGLEVEL.ERR)
if jobid:
job.update({'status':272, 'comment':'Transcode Completed'})
def main():
parser = OptionParser(usage="usage: %prog [options] [jobid]")
parser.add_option('--chanid', action='store', type='int', dest='chanid',
help='Use chanid for manual operation')
parser.add_option('--starttime', action='store', type='int',
dest='starttime',
help='Use starttime for manual operation')
#MythLog.loadOptParse(parser)
#MythLog._optparseinput()
MythLog._setpath('/tmp')
opts, args = parser.parse_args()
j = TranscodeJob()
try:
if len(args) == 1:
j.run(jobid=args[0])
elif opts.chanid and opts.starttime:
j.run(chanid=opts.chanid, starttime=opts.starttime)
else:
print 'Script must be provided jobid, or chanid and starttime.'
sys.exit(1)
except:
j.logTB()
sys.exit(1)
if __name__ == '__main__':
main()