-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.py
219 lines (168 loc) · 4.9 KB
/
master.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import operator
import os
import re
import time
import xml.etree.cElementTree as ET
from collections import namedtuple
import amqp
import ceph
import config
import ffmpeg
import ffprobe
import log
import utils
Call = namedtuple('Call', ['begin', 'end', 'function', 'failure', 'result'])
def split(_id, body):
up = ceph.ceph()
splitlist = ffmpeg.split(body['path'], body['video'], body['metadata'])
if len(splitlist) == 0:
return False
index = -1
for i in splitlist:
index += 1
i = i.rstrip('\n\r')
keyname = '%s-%s' % (i, index)
path = '%s/%s' % (config.tmpdir, i)
args = {'status': 'SPLITED_CHUNK',
'path': {'type': 'ceph', 'filename': keyname},
'id': _id,
'chunkid': index,
}
up.upload(path, keyname, amqp.watcher.pub, args)
up.sync()
return True
# shitty code: changed are inplace, this is not really handled..
def fix_audio_xml(root, adaptationSet, metadata):
root.remove(adaptationSet)
for representation in adaptationSet:
for baseurl in representation.iter('BaseURL'):
match = re.match('[0-9]+-stream([0-9]+).m4s', baseurl.text)
if not match:
continue
streamid = match.group(1)
AS = ET.SubElement(root, 'AdaptationSet')
AS.set('segmentAlignment', 'true')
try:
lang = metadata[streamid]['lang']
AS.set('lang', lang)
representation.set('lang', lang)
except KeyError:
pass
AS.append(representation)
def fix_dash(mpd, vtts, video, metadata):
tree = ET.parse(mpd)
root = tree.getroot()
ns = '{urn:mpeg:dash:schema:mpd:2011}'
for elem in root.getiterator():
if elem.tag.startswith(ns):
elem.tag = elem.tag[len(ns):]
for child in root:
if child.tag == 'Period':
root = child
break
for adaptationSet in root:
attr = adaptationSet.attrib
if 'contentType' not in attr or attr['contentType'] != 'audio':
continue
fix_audio_xml(root, adaptationSet, metadata)
as_list = {}
index = 0
for i in vtts:
index += 1
AS = ET.SubElement(root, 'AdaptationSet')
AS.set('mimeType', 'text/vtt')
if i['lang'] in as_list:
lang = '%s-%s' % (i['lang'], index)
else:
lang = i['lang']
as_list[i['lang']] = True
AS.set('lang', lang)
repre = ET.SubElement(AS, 'Representation')
repre.set('id', '%s' % (index,))
repre.set('bandwidth', '256')
repre.set('lang', lang)
baseurl = ET.SubElement(repre, 'BaseURL')
baseurl.text = '%s-%s-%s.vtt' % (video, i['lang'], i['i'])
tree.write(mpd)
def upload_hls(upload, m3u8, video):
upload.upload(m3u8, '%s.m3u8' % (video,), public=False)
match = re.compile('.*\.ts$')
for root, dirs, files in os.walk(config.tmpdir):
for name in files:
if re.match(match, name):
upload.upload(os.path.join(root, name), name)
def upload_dash(upload, mpd, vtts, video, metadata):
for i in vtts:
upload.upload(i['file'], '%s-%s-%s.vtt' % (video, i['lang'], i['i']), public=False)
meta = {}
numb = 0
for i in metadata:
if metadata[i]['type'] != 'subtitle':
meta[numb] = metadata[i]
numb += 1
for i, _ in sorted(meta.items(), key=operator.itemgetter(0)):
tmp = '%s-stream%s.m4s' % (video, i)
upload.upload('%s/%s' % (config.tmpdir, tmp), tmp, public=False)
upload.upload(mpd, '%s.mpd' % (video,), public=False)
# job must be chunkid-ordered
def merge(_, body):
jobs = body['job']
files = []
if len(jobs) == 0:
return None
for i in jobs:
files.append('%s-encoded' % (i['path'],))
job = jobs[0]
try:
bigfile, m3u8, mpd, vtts = ffmpeg.makePlaylists(files, job['video'], job['metadata'])
except Exception as e:
log.log('merge error on master %s : %s' % (job['master'], e.stderr))
return None
up = ceph.ceph()
if m3u8 is not None:
upload_hls(up, m3u8, job['video'])
if mpd is not None and vtts is not None:
fix_dash(mpd, vtts, job['video'], job['metadata'])
upload_dash(up, mpd, vtts, job['video'], job['metadata'])
up.sync()
duration = ffprobe.get_duration(bigfile)
return duration
def probe(_, body):
return ffprobe.ffprobe(body['path'])
def do_job():
utils.setup_tmpdir(config.tmpdir)
calls = {
'TO_SPLIT': Call('SPLITING', 'SPLITED', split, False, 'None'),
'TO_PROBE': Call('PROBING', 'PROBED', probe, None, 'metadata'),
'TO_MERGE': Call('MERGING', 'MERGED', merge, None, 'duration'),
}
method, props, body = amqp.master.get()
if method is None or body is None:
time.sleep(1)
return
status = body['status']
_id = body['id']
if status not in calls:
log.error('Bug found: unknown message received: %s' % (body,))
amqp.master.ack(method.delivery_tag)
return
call = calls[status]
output = {'id': _id, 'status': call.end}
amqp.watcher.notify(_id, call.begin)
result = call.function(_id, body)
output[call.result] = result
if result is call.failure:
output['status'] = 'FAILED'
amqp.watcher.pub(output)
amqp.master.ack(method.delivery_tag)
if status == 'TO_SPLIT':
try:
os.remove(body['path'])
except FileNotFoundError:
pass
def run():
while True:
try:
do_job()
except Exception as e:
log.log('Error found: %s' % (e,))