-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmythtv-transcoder
executable file
·497 lines (392 loc) · 13.6 KB
/
mythtv-transcoder
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/python3
#
# Renders MythTV recordings into H.264 .mp4 files.
#
# Assumptions:
# - MythTV running on Ubuntu Linux
# - MythTV Python module
# - HandBrakeCLI
# - Installed in /usr/local/bin
#
#
# see https://www.mythtv.org/wiki/Transcode_Mpeg2_to_H264
#
# TODO:
# - verbosity
# - fix duration
import sys
import os
import re
import time
import datetime
import subprocess
import MythTV
stdout = None
dbgout = None
def outwrite(s):
if isinstance(s, str):
s = s.encode('utf-8')
stdout.write(s)
if dbgout:
dbgout.write(s)
def usage():
m = """Usage: transcoder <options>
-jobid <#> %JOBID% within backend for automatic use
-chanid <####> specify channel id for manual use
-starttime <YYYYMMDDHHMMSS> specify program start time for manual use
-keepcomm keep commercials
"""
outwrite(m)
def nuke(path):
try:
os.unlink(path)
outwrite('deleted ' + path + '\n')
except OSError:
pass
def openLogFile(dir, base, ext):
now = int(time.time())
matches = []
for ent in os.listdir(dir):
if ent.startswith(base) and ent.endswith(ext):
matches.append(ent)
matches.sort()
while len(matches) >= 20:
old = dir + '/' + matches.pop(0)
try:
os.unlink(old) # best effort
except OSError:
pass
for i in range(20): # 20 attempts
target = datetime.datetime.fromtimestamp(now + i)
ts = target.strftime('%Y%m%d%H%M%S')
path = dir + '/' + base + ts + ext
try:
fh = open(path, 'xb', 0) # exclusive, unbuffered
return fh
except FileExistsError:
pass
# if all else fails, share the file
path = dir + '/' + base + ext
return open(path, 'wb', 0) # unbuffered
###############################################################################
class Transcoder(object):
def __init__(self):
self.logger = Logger()
self.mm = MythMisc(self.logger)
self.hb = HandBrake(self.logger)
self.jobid = None
self.chanid = None
self.starttime = None
self.cutComm = True
self.mdb = None
self.job = None
self.rec = None
self.dir = '/tmp' # failsafe
self.origPath = None
self.curPath = None
self.base = None
self.deletePaths = {}
def setStartTime(self, s):
raw = datetime.datetime.strptime(s, '%Y%m%d%H%M%S')
utc = raw.replace(tzinfo = datetime.timezone.utc)
self.starttime = utc.astimezone() # local time zone
def checkArgs(self):
if self.jobid and self.chanid and self.starttime:
raise RuntimeError('jobid incompatible with chanid & starttime')
if not self.jobid:
if not self.chanid:
if not self.starttime:
raise RuntimeError(
'must specify jobid or chanid & starttime')
raise RuntimeError('no jobid or chanid supplied')
if not self.starttime:
raise RuntimeError('no jobid or starttime supplied')
def run(self):
try:
self.initJob()
self.getRecorded()
self.cutCommercials()
self.makeSmaller()
self.replaceRecorded()
except BaseException as ex:
self.logger.error(str(ex))
self.cleanup()
raise
self.cleanup()
def initJob(self):
msg = 'begin transcode'
self.mdb = MythTV.MythDB()
if self.jobid:
self.job = MythTV.Job(self.jobid, db = self.mdb)
self.chanid = self.job.chanid
self.starttime = self.job.starttime
self.logger.setJob(self.job)
msg += ' jobid=%d' % self.jobid
msg += ' chanid=%d starttime=%s' % (self.chanid, self.starttime)
self.logger.comment(msg)
def getRecorded(self):
rec = MythTV.Recorded((self.chanid, self.starttime), db = self.mdb)
sg = next(self.mdb.getStorageGroup(rec.storagegroup, rec.hostname))
pth = os.path.join(sg.dirname, rec.basename)
self.rec = rec
self.dir = sg.dirname
self.origPath = pth
self.curPath = pth
self.base = rec.basename.rsplit('.', 1)[0]
self.logger.comment('title=<%s> size=%d' % (rec.title, rec.filesize))
self.logger.toFile('description=<%s>' % rec.description)
def cutCommercials(self):
if not self.cutComm:
return
self.waitCommFlag()
outPath = os.path.join(self.dir, self.base) + '.cut'
self.mm.copySkipToCut(self.rec.chanid, self.rec.starttime)
self.nukeLater(outPath)
self.nukeLater(outPath + '.map')
try:
self.mm.transcribe(self.rec.chanid, self.rec.starttime, outPath)
finally:
self.mm.clearCutList(self.rec.chanid, self.rec.starttime)
self.curPath = outPath
def makeSmaller(self):
outPath = os.path.join(self.dir, self.base) + '.mp4'
try:
self.hb.recode(self.curPath, outPath)
except:
self.nukeNow(outPath)
raise
self.curPath = outPath
def replaceRecorded(self):
self.logger.comment('replacing stream data')
newSize = os.path.getsize(self.curPath)
pct = int(newSize * 100.0 / self.rec.filesize)
self.rec.basename = os.path.basename(self.curPath)
self.rec.filesize = newSize
self.rec.transcoded = 1
self.rec.seek.clean()
self.rec.markup.clean() # FIXME: ok?
self.rec.update()
self.nukeLater(self.origPath)
self.nukeLaterPat(self.dir, self.base, '.png')
self.mm.buildSeekTable(self.rec.chanid, self.rec.starttime)
if self.cutComm:
self.mm.clearSkipList(self.rec.chanid, self.rec.starttime)
self.logger.finish('transcode done, %d%% the size' % pct)
def waitCommFlag(self):
if not self.rec.commflagged:
self.logger.comment('no commercial scanning')
return
t0 = time.time()
go = True
while go:
ary = self.mdb.searchJobs(chanid = self.rec.chanid,
starttime = self.rec.starttime)
for a in ary:
if (a.type == a.COMMFLAG) and (a.status == a.RUNNING):
delta = int(time.time() - t0)
msg = 'waited %d secs for commercial flagging' % delta
self.logger.pause(msg)
time.sleep(10.0)
break
go = False
self.logger.comment('commercial flagging complete')
def nukeLater(self, path):
self.deletePaths[path] = True
def nukeLaterPat(self, dir, base, ext):
for ent in os.listdir(dir):
if ent.startswith(base) and ent.endswith(ext):
self.nukeLater(os.path.join(dir, ent))
def nukeNow(self, path):
nuke(path)
try:
del self.deletePaths[path]
except KeyError:
pass
def cleanup(self):
for path in self.deletePaths.keys():
nuke(path)
self.deleteList = {}
###############################################################################
class Logger(object):
UNKNOWN = 0
QUEUED = 1
PENDING = 2
RUNNING = 4
PAUSED = 6
FINISHED = 272
ERRORED = 304
def __init__(self):
self.job = None
self.verbose = 1 # FIXME
def setJob(self, job):
self.job = job
def reset(self, name):
self.name = name
self.oldVal = -1
def progress(self, numer, denom):
if denom:
val = int(numer * 100.0 / denom)
msg = self.name + ' ' + str(val) + '%'
else:
val = int(numer)
msg = self.name + ' ' + str(val)
if (val != self.oldVal):
self.oldVal = val
self.log(self.RUNNING, msg)
def comment(self, msg):
self.log(self.RUNNING, msg)
def pause(self, msg):
self.log(self.PAUSED, msg)
def finish(self, msg):
self.log(self.FINISHED, msg)
def error(self, msg):
self.log(self.ERRORED, msg)
def log(self, st, msg):
map = {'status': st, 'comment': msg}
self.push(map)
def push(self, map):
if self.job:
self.job.update(map)
self.toFile(map['comment'])
def toFile(self, msg):
if self.verbose > 0:
outwrite(msg + '\n')
###############################################################################
class Task(object):
def __init__(self, logger):
self.logger = logger
self.verbose = 1 # FIXME
def run(self, cmd, progRe, denom):
if self.verbose > 0:
outwrite(' '.join(cmd) + '\n')
po = subprocess.Popen(cmd, bufsize = 0,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT)
subprocess.call('renice +1 -p %d > /dev/null' % po.pid, shell = True)
subprocess.call('ionice -c 2 -n 5 -p %d' % po.pid, shell = True)
t0 = time.time()
while True:
buf = po.stdout.read(1024)
if not buf:
break
if dbgout:
dbgout.write(buf)
for lin in buf.split(b'\n'):
mat = progRe.search(lin)
if mat:
if denom:
self.logger.progress(float(mat.group(1)), denom)
else:
self.logger.progress(time.time() - t0, None)
return po.wait()
class MythMisc(Task):
dir = '/usr/bin'
dummyRe = re.compile(rb'(\d)')
def __init__(self, logger):
Task.__init__(self, logger)
def timeStr(self, starttime):
utc = starttime.astimezone(datetime.timezone.utc)
return utc.strftime('%Y%m%d%H%M%S')
def preamble(self, prog, chanid, starttime):
cmd = [os.path.join(self.dir, prog)]
cmd.append('--chanid')
cmd.append(str(chanid))
cmd.append('--starttime')
cmd.append(self.timeStr(starttime))
return cmd
def copySkipToCut(self, chanid, starttime):
cmd = self.preamble('mythutil', chanid, starttime)
cmd.append('--gencutlist')
self.logger.reset('gen cutlist')
rc = self.run(cmd, self.dummyRe, None)
if rc:
raise RuntimeError('mythutil returned %d' % rc)
def clearCutList(self, chanid, starttime):
cmd = self.preamble('mythutil', chanid, starttime)
cmd.append('--clearcutlist')
self.logger.reset('clear cutlist')
rc = self.run(cmd, self.dummyRe, None)
if rc:
raise RuntimeError('mythutil returned %d' % rc)
def clearSkipList(self, chanid, starttime):
cmd = self.preamble('mythutil', chanid, starttime)
cmd.append('--clearskiplist')
self.logger.reset('clear skiplist')
rc = self.run(cmd, self.dummyRe, None)
if rc:
raise RuntimeError('mythutil returned %d' % rc)
def buildSeekTable(self, chanid, starttime):
cmd = self.preamble('mythcommflag', chanid, starttime)
cmd.append('--rebuild')
self.logger.reset('rebuild seektable')
rc = self.run(cmd, self.dummyRe, None)
if rc:
raise RuntimeError('mythcommflag returned %d' % rc)
def transcribe(self, chanid, starttime, outPath):
cmd = self.preamble('mythtranscode', chanid, starttime)
cmd.append('--mpeg2')
cmd.append('--honorcutlist')
cmd.append('--outfile')
cmd.append(outPath)
self.logger.reset('transcribe')
rc = self.run(cmd, self.dummyRe, None)
if rc:
raise RuntimeError('mythtranscode returned %d' % rc)
class HandBrake(Task):
bin = '/usr/bin/HandBrakeCLI'
opts = ['--deinterlace', '--encoder', 'x264', '--quality', '22',
'--format', 'av_mp4', '--aencoder', 'copy']
pctRe = re.compile(rb'Encoding:[^%]*\s(\d+[.]\d+)\s*%')
def __init__(self, logger):
Task.__init__(self, logger)
def recode(self, inPath, outPath):
cmd = [self.bin]
cmd.extend(self.opts)
cmd.append('--input')
cmd.append(inPath)
cmd.append('--output')
cmd.append(outPath)
self.logger.reset('encode h.264')
rc = self.run(cmd, self.pctRe, 100.0)
if rc:
raise RuntimeError('HandBrake returned %d' % rc)
###############################################################################
def main(args = None):
global stdout
global dbgout
num = os.getpid() % 20
stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) # unbuffered
dbgout = openLogFile('/tmp', 'transcoder', '.out')
if args is None:
args = sys.argv[1 : ]
tc = Transcoder()
try:
while args and args[0].startswith('-'):
arg = args.pop(0)
if arg == '-jobid':
tc.jobid = int(args.pop(0))
elif arg == '-chanid':
tc.chanid = int(args.pop(0))
elif arg == '-starttime':
tc.setStartTime(args.pop(0))
elif arg == '-keepcomm':
tc.cutComm = False
else:
raise RuntimeError('unrecognized option: ' + arg)
tc.checkArgs()
except BaseException as ex:
usage()
outwrite('\n%s\n' % ex)
return 1
try:
tc.run()
return 0
except BaseException as ex:
outwrite('\n%s\n' % ex)
return 1
if __name__ == '__main__':
sys.exit(main())
# Local Variables:
# mode: indented-text
# indent-tabs-mode: nil
# End: