forked from jmoney1217/mythtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mythbrake.py
executable file
·205 lines (174 loc) · 6.49 KB
/
mythbrake.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
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
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
# 2015 Michael Stucky
# This script is based on Raymond Wagner's transcode wrapper stub.
# Designed to be a USERJOB of the form </path to script/transcode-h264.py %JOBID%>
from MythTV import Job, Recorded, System, MythDB, findfile, MythError, MythLog, datetime
from optparse import OptionParser
from glob import glob
from shutil import copyfile
import sys
import os
import errno
import time
import re
log_dir = '/home/mythtv/mythbrake_logs'
transcoder = '/usr/bin/HandBrakeCLI'
flush_commskip = True
build_seektable = True
class tee:
'redirects a write to multiple objects'
def __init__(self, *writers):
self.writers = writers
def write(self, string):
string = string.strip()
if len(string) > 0:
string = '%s\t%s\n' % (time.strftime('%m-%d %H:%M:%S'), string)
for writer in self.writers:
writer.write(string)
writer.flush()
def runjob(jobid=None, chanid=None, starttime=None):
db = MythDB()
if jobid:
job = Job(jobid, db=db)
chanid = job.chanid
starttime = job.starttime
rec = Recorded((chanid, starttime), db=db)
timestr = time.strftime("%m-%d-%y %H:%M:%S")
title_san = re.sub("\s", ".", rec.title)
print title_san
try:
os.mkdir(log_dir)
except OSError, e:
pass
log_file = os.path.join(log_dir, "%s_transcode_log_%s.txt" % (title_san, timestr))
trans_log_file = os.path.join(log_dir, "%s_transcode_log_%s.hb.txt" % (title_san, timestr))
commflag_log_file = os.path.join(log_dir, "%s_transcode_log_%s.cf.txt" % (title_san, timestr))
print 'Capturing log in %s...' % log_file
lfp = open(log_file, 'w')
sys.stdout = tee(sys.stdout, lfp)
sys.stderr = tee(sys.stderr, lfp)
print 'Logging into %s' % log_file
sg = findfile('/'+rec.basename, rec.storagegroup, db=db)
if sg is None:
print 'Local access to recording not found.'
sys.exit(1)
infile = os.path.join(sg.dirname, rec.basename)
tmpfile = '%s.tmp' % infile.rsplit('.',1)[0]
outfile = '%s.mp4' % infile.rsplit('.',1)[0]
print "Infile: %s" % infile
print "Outfile: %s" % outfile
# reformat 'starttime' for use with mythtranscode/ffmpeg/mythcommflag
starttime = str(rec.starttime.utcisoformat().replace(u':', '').replace(u' ', '').replace(u'T', '').replace('-', ''))
# Lossless transcode to strip cutlist
if rec.cutlist == 1:
if jobid:
job.update({'status':4, 'comment':'Removing Cutlist'})
task = System(path='mythtranscode', db=db)
try:
output = task('--chanid "%s"' % chanid,
'--starttime "%s"' % starttime,
'--mpeg2',
'--honorcutlist',
'-o "%s"' % tmpfile,
'2> /dev/null')
except MythError, e:
print 'Command failed with output:\n%s' % e.stderr
if jobid:
job.update({'status':304, 'comment':'Removing Cutlist failed'})
sys.exit(e.retcode)
else:
tmpfile = infile
# copyfile('%s' % infile, '%s' % tmpfile)
# Transcode to mp4
if jobid:
job.update({'status':4, 'comment':'Transcoding to mp4'})
task = System(path=transcoder, db=db)
try:
output = task('-v',
'-q 20.0',
'-e x264',
'-r 25',
'--crop 0:0:0:0',
'-d',
'-m',
'-x b-adapt=2:rc-lookahead=50:ref=3:bframes=3:me=umh:subme=8:trellis=1:merange=20:direct=auto',
'-i "%s"' % tmpfile,
'-o "%s"' % outfile,
'-4',
'--optimize 2 >> "%s"' % trans_log_file)
except MythError, e:
print 'Command failed with output:\n%s' % e.stderr
if jobid:
job.update({'status':304, 'comment':'Transcoding to mp4 failed'})
sys.exit(e.retcode)
print 'Done transcoding'
rec.basename = os.path.basename(outfile)
try:
print 'Deleting %s' % infile
os.remove(infile)
except OSError:
pass
print '''Cleanup the old *.png files'''
for filename in glob('%s*.png' % infile):
print 'Deleting %s' % filename
os.remove(filename)
try:
print 'Deleting %s' % tmpfile
os.remove(tmpfile)
except OSError:
pass
try:
print 'Deleting %s.map' % tmpfile
os.remove('%s.map' % tmpfile)
except OSError:
pass
rec.filesize = os.path.getsize(outfile)
rec.transcoded = 1
rec.seek.clean()
print 'Changed recording basename, set transcoded...'
if flush_commskip:
print 'Flushing commskip list...'
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()
print 'Updating recording...'
rec.update()
if jobid:
job.update({'status':4, 'comment':'Rebuilding seektable'})
if build_seektable:
print 'Rebuilding seek table'
task = System(path='mythcommflag')
task.command('--chanid %s' % chanid,
'--starttime %s' % starttime,
'--rebuild',
'> "%s"' % commflag_log_file)
print 'Job Done!...'
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')
parser.add_option('-v', '--verbose', action='store', type='string', dest='verbose',
help='Verbosity level')
opts, args = parser.parse_args()
if opts.verbose:
if opts.verbose == 'help':
print MythLog.helptext
sys.exit(0)
MythLog._setlevel(opts.verbose)
if len(args) == 1:
runjob(jobid=args[0])
elif opts.chanid and opts.starttime:
runjob(chanid=opts.chanid, starttime=opts.starttime)
else:
print 'Script must be provided jobid, or chanid and starttime.'
sys.exit(1)
if __name__ == '__main__':
main()