threefive3 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scte35/__init__.py +27 -0
- scte35/base.py +189 -0
- scte35/bitn.py +193 -0
- scte35/commands.py +326 -0
- scte35/crc.py +49 -0
- scte35/cue.py +456 -0
- scte35/descriptors.py +549 -0
- scte35/hls.py +1171 -0
- scte35/packetdata.py +39 -0
- scte35/section.py +194 -0
- scte35/segment.py +128 -0
- scte35/segmentation.py +71 -0
- scte35/sixfix.py +194 -0
- scte35/spare.py +12 -0
- scte35/stream.py +686 -0
- scte35/streamtypes.py +127 -0
- scte35/sxp.py +350 -0
- scte35/upids.py +403 -0
- scte35/version.py +6 -0
- scte35/xml.py +355 -0
- threefive3-3.0.1.data/scripts/scte35 +365 -0
- threefive3-3.0.1.dist-info/LICENSE +24 -0
- threefive3-3.0.1.dist-info/METADATA +261 -0
- threefive3-3.0.1.dist-info/RECORD +26 -0
- threefive3-3.0.1.dist-info/WHEEL +5 -0
- threefive3-3.0.1.dist-info/top_level.txt +1 -0
scte35/cue.py
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
"""
|
|
2
|
+
scte35.Cue Class
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from base64 import b64decode, b64encode
|
|
6
|
+
import json
|
|
7
|
+
from .spare import print2
|
|
8
|
+
from .bitn import NBin
|
|
9
|
+
from .base import SCTE35Base
|
|
10
|
+
from .section import SpliceInfoSection
|
|
11
|
+
from .commands import command_map
|
|
12
|
+
from .descriptors import splice_descriptor, descriptor_map
|
|
13
|
+
from .crc import crc32
|
|
14
|
+
from .xml import Node
|
|
15
|
+
from .segmentation import table22
|
|
16
|
+
from .sxp import SuperXmlParser
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Cue(SCTE35Base):
|
|
20
|
+
"""
|
|
21
|
+
The scte35.Splice class handles parsing
|
|
22
|
+
SCTE 35 message strings.
|
|
23
|
+
Example usage:
|
|
24
|
+
|
|
25
|
+
>>>> import fu
|
|
26
|
+
>>>> Base64 = "/DAvAAAAAAAA///wBQb+dGKQoAAZAhdDVUVJSAAAjn+fCAgAAAAALKChijUCAKnMZ1g="
|
|
27
|
+
>>>> cue = scte35.Cue(Base64)
|
|
28
|
+
|
|
29
|
+
# cue.decode() returns True on success,or False if decoding failed
|
|
30
|
+
|
|
31
|
+
>>>> cue.decode()
|
|
32
|
+
True
|
|
33
|
+
|
|
34
|
+
# After Calling cue.decode() the instance variables can be accessed via dot notation.
|
|
35
|
+
|
|
36
|
+
>>>> cue.command
|
|
37
|
+
{'command_length': 5, 'name': 'Time Signal', 'time_specified_flag': True,
|
|
38
|
+
'pts_time': 21695.740089}
|
|
39
|
+
|
|
40
|
+
>>>> cue.command.pts_time
|
|
41
|
+
21695.740089
|
|
42
|
+
|
|
43
|
+
>>>> cue.info_section.table_id
|
|
44
|
+
|
|
45
|
+
'0xfc'
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, data=None, packet_data=None):
|
|
50
|
+
"""
|
|
51
|
+
data may be packet bites or encoded string
|
|
52
|
+
packet_data is a instance passed from a Stream instance
|
|
53
|
+
"""
|
|
54
|
+
self.command = None
|
|
55
|
+
self.descriptors = []
|
|
56
|
+
self.info_section = SpliceInfoSection()
|
|
57
|
+
self.bites = None
|
|
58
|
+
if data:
|
|
59
|
+
self._mk_bits(data)
|
|
60
|
+
self.packet_data = packet_data
|
|
61
|
+
self.dash_data = None
|
|
62
|
+
|
|
63
|
+
def __repr__(self):
|
|
64
|
+
return str(self.__dict__)
|
|
65
|
+
|
|
66
|
+
# decode
|
|
67
|
+
|
|
68
|
+
def decode(self):
|
|
69
|
+
"""
|
|
70
|
+
Cue.decode() parses for SCTE35 data
|
|
71
|
+
"""
|
|
72
|
+
bites = self.bites
|
|
73
|
+
self.descriptors = []
|
|
74
|
+
while bites:
|
|
75
|
+
bites = self.mk_info_section(bites)
|
|
76
|
+
bites = self._set_splice_command(bites)
|
|
77
|
+
bites = self._mk_descriptors(bites)
|
|
78
|
+
crc = hex(int.from_bytes(bites[0:4], byteorder="big"))
|
|
79
|
+
self.info_section.crc = crc
|
|
80
|
+
return True
|
|
81
|
+
return False
|
|
82
|
+
|
|
83
|
+
def _descriptor_loop(self, loop_bites):
|
|
84
|
+
"""
|
|
85
|
+
Cue._descriptor_loop parses all splice descriptors
|
|
86
|
+
"""
|
|
87
|
+
tag_n_len = 2
|
|
88
|
+
while len(loop_bites) > tag_n_len:
|
|
89
|
+
spliced = splice_descriptor(loop_bites)
|
|
90
|
+
if not spliced:
|
|
91
|
+
return
|
|
92
|
+
sd_size = tag_n_len + spliced.descriptor_length
|
|
93
|
+
loop_bites = loop_bites[sd_size:]
|
|
94
|
+
del spliced.bites
|
|
95
|
+
self.descriptors.append(spliced)
|
|
96
|
+
|
|
97
|
+
def _get_dash_data(self, scte35_dict):
|
|
98
|
+
if self.dash_data:
|
|
99
|
+
scte35_dict["dash_data"] = self.dash_data
|
|
100
|
+
return scte35_dict
|
|
101
|
+
|
|
102
|
+
def _get_packet_data(self, scte35_dict):
|
|
103
|
+
if self.packet_data:
|
|
104
|
+
scte35_dict["packet_data"] = self.packet_data.get()
|
|
105
|
+
return scte35_dict
|
|
106
|
+
|
|
107
|
+
def get(self):
|
|
108
|
+
"""
|
|
109
|
+
Cue.get returns the SCTE-35 Cue
|
|
110
|
+
data as a dict of dicts.
|
|
111
|
+
"""
|
|
112
|
+
if self.command and self.info_section:
|
|
113
|
+
scte35_data = {
|
|
114
|
+
"info_section": self.info_section.get(),
|
|
115
|
+
"command": self.command.get(),
|
|
116
|
+
"descriptors": self.get_descriptors(),
|
|
117
|
+
}
|
|
118
|
+
scte35_data = self._get_dash_data(scte35_data)
|
|
119
|
+
scte35_data = self._get_packet_data(scte35_data)
|
|
120
|
+
return scte35_data
|
|
121
|
+
return False
|
|
122
|
+
|
|
123
|
+
def get_descriptors(self):
|
|
124
|
+
"""
|
|
125
|
+
Cue.get_descriptors returns a list of
|
|
126
|
+
SCTE 35 splice descriptors as dicts.
|
|
127
|
+
"""
|
|
128
|
+
return [d.get() for d in self.descriptors]
|
|
129
|
+
|
|
130
|
+
def bytes(self):
|
|
131
|
+
"""
|
|
132
|
+
get_bytes returns Cue.bites
|
|
133
|
+
"""
|
|
134
|
+
return self.bites
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
def fix_bad_b64(data):
|
|
138
|
+
"""
|
|
139
|
+
fix_bad_b64 fixes bad padding on Base64
|
|
140
|
+
"""
|
|
141
|
+
while len(data) % 4 != 0:
|
|
142
|
+
data = data + "="
|
|
143
|
+
return data
|
|
144
|
+
|
|
145
|
+
def _int_bits(self, data):
|
|
146
|
+
"""
|
|
147
|
+
_int_bits convert a SCTE-35 Cue from integer to bytes.
|
|
148
|
+
"""
|
|
149
|
+
length = data.bit_length() >> 3
|
|
150
|
+
bites = int.to_bytes(data, length, byteorder="big")
|
|
151
|
+
return bites
|
|
152
|
+
|
|
153
|
+
def _hex_bits(self, data):
|
|
154
|
+
"""
|
|
155
|
+
_hex_bits convert a SCTE-35 Cue from hex to bytes.
|
|
156
|
+
"""
|
|
157
|
+
try:
|
|
158
|
+
i = int(data, 16)
|
|
159
|
+
i_len = i.bit_length() >> 3
|
|
160
|
+
bites = int.to_bytes(i, i_len, byteorder="big")
|
|
161
|
+
return bites
|
|
162
|
+
except (LookupError, TypeError, ValueError):
|
|
163
|
+
if data[:2].lower() == "0x":
|
|
164
|
+
data = data[2:]
|
|
165
|
+
if data[:2].lower() == "fc":
|
|
166
|
+
return bytes.fromhex(data)
|
|
167
|
+
return b""
|
|
168
|
+
|
|
169
|
+
def _b64_bits(self, data):
|
|
170
|
+
"""
|
|
171
|
+
_b64_bits decode base64 to bytes
|
|
172
|
+
"""
|
|
173
|
+
try:
|
|
174
|
+
return b64decode(self.fix_bad_b64(data))
|
|
175
|
+
except (LookupError, TypeError, ValueError):
|
|
176
|
+
return data
|
|
177
|
+
|
|
178
|
+
def _str_bits(self, data):
|
|
179
|
+
try:
|
|
180
|
+
self.load(data)
|
|
181
|
+
return self.bites
|
|
182
|
+
except (LookupError, TypeError, ValueError):
|
|
183
|
+
hex_bits = self._hex_bits(data)
|
|
184
|
+
if hex_bits:
|
|
185
|
+
return hex_bits
|
|
186
|
+
return self._b64_bits(data)
|
|
187
|
+
|
|
188
|
+
def _mk_bits(self, data):
|
|
189
|
+
"""
|
|
190
|
+
cue._mk_bits Converts
|
|
191
|
+
Hex and Base64 strings into bytes.
|
|
192
|
+
"""
|
|
193
|
+
if isinstance(data, bytes):
|
|
194
|
+
self.bites = self.idxsplit(data, b"\xfc")
|
|
195
|
+
self.decode()
|
|
196
|
+
return self.bites
|
|
197
|
+
if isinstance(data, int):
|
|
198
|
+
self.bites = self._int_bits(data)
|
|
199
|
+
self.decode()
|
|
200
|
+
return self.bites
|
|
201
|
+
if isinstance(data, dict):
|
|
202
|
+
self.load(data)
|
|
203
|
+
return self.bites
|
|
204
|
+
if isinstance(data, Node):
|
|
205
|
+
self.load(data.mk())
|
|
206
|
+
return self.bites
|
|
207
|
+
if isinstance(data, str):
|
|
208
|
+
self.bites = self._str_bits(data)
|
|
209
|
+
self.decode()
|
|
210
|
+
return self.bites
|
|
211
|
+
|
|
212
|
+
def _mk_descriptors(self, bites):
|
|
213
|
+
"""
|
|
214
|
+
Cue._mk_descriptors parses
|
|
215
|
+
Cue.info_section.descriptor_loop_length,
|
|
216
|
+
then call Cue._descriptor_loop
|
|
217
|
+
"""
|
|
218
|
+
if len(bites) < 2:
|
|
219
|
+
return False
|
|
220
|
+
dll = (bites[0] << 8) | bites[1]
|
|
221
|
+
self.info_section.descriptor_loop_length = dll
|
|
222
|
+
bites = bites[2:]
|
|
223
|
+
self._descriptor_loop(bites[:dll])
|
|
224
|
+
return bites[dll:]
|
|
225
|
+
|
|
226
|
+
def mk_info_section(self, bites):
|
|
227
|
+
"""
|
|
228
|
+
Cue.mk_info_section parses the
|
|
229
|
+
Splice Info Section
|
|
230
|
+
of a SCTE35 cue.
|
|
231
|
+
"""
|
|
232
|
+
info_size = 14
|
|
233
|
+
info_bites = bites[:info_size]
|
|
234
|
+
self.info_section.decode(info_bites)
|
|
235
|
+
return bites[info_size:]
|
|
236
|
+
|
|
237
|
+
def _set_splice_command(self, bites):
|
|
238
|
+
"""
|
|
239
|
+
Cue._set_splice_command parses
|
|
240
|
+
the command section of a SCTE35 cue.
|
|
241
|
+
"""
|
|
242
|
+
sct = self.info_section.splice_command_type
|
|
243
|
+
if sct not in command_map:
|
|
244
|
+
return False
|
|
245
|
+
iscl = self.info_section.splice_command_length
|
|
246
|
+
cmd_bites = bites[:iscl]
|
|
247
|
+
self.command = command_map[sct](cmd_bites)
|
|
248
|
+
self.command.command_length = iscl
|
|
249
|
+
|
|
250
|
+
self.command.decode()
|
|
251
|
+
del self.command.bites
|
|
252
|
+
return bites[iscl:]
|
|
253
|
+
|
|
254
|
+
# encode related
|
|
255
|
+
|
|
256
|
+
def _assemble(self):
|
|
257
|
+
dscptr_bites = self._unloop_descriptors()
|
|
258
|
+
dll = len(dscptr_bites)
|
|
259
|
+
self.info_section.descriptor_loop_length = dll
|
|
260
|
+
cmd_bites = self.command.encode()
|
|
261
|
+
cmdl = self.command.command_length = len(cmd_bites)
|
|
262
|
+
self.info_section.splice_command_length = cmdl
|
|
263
|
+
self.info_section.splice_command_type = self.command.command_type
|
|
264
|
+
# 11 bytes for info section + command + 2 descriptor loop length
|
|
265
|
+
# + descriptor loop + 4 for crc
|
|
266
|
+
self.info_section.section_length = 11 + cmdl + 2 + dll + 4
|
|
267
|
+
self.bites = self.info_section.encode()
|
|
268
|
+
self.bites += cmd_bites
|
|
269
|
+
self.bites += int.to_bytes(
|
|
270
|
+
self.info_section.descriptor_loop_length, 2, byteorder="big"
|
|
271
|
+
)
|
|
272
|
+
self.bites += dscptr_bites
|
|
273
|
+
|
|
274
|
+
def base64(self):
|
|
275
|
+
"""
|
|
276
|
+
base64 Cue.base64() converts SCTE35 data
|
|
277
|
+
to a base64 encoded string.
|
|
278
|
+
"""
|
|
279
|
+
if self.command:
|
|
280
|
+
self._assemble()
|
|
281
|
+
self._encode_crc()
|
|
282
|
+
return b64encode(self.bites).decode()
|
|
283
|
+
return False
|
|
284
|
+
|
|
285
|
+
def encode(self):
|
|
286
|
+
"""
|
|
287
|
+
encode is an alias for base64
|
|
288
|
+
"""
|
|
289
|
+
return self.base64()
|
|
290
|
+
|
|
291
|
+
def int(self):
|
|
292
|
+
"""
|
|
293
|
+
int returns self.bites as an int.
|
|
294
|
+
"""
|
|
295
|
+
self.encode()
|
|
296
|
+
return int.from_bytes(self.bites, byteorder="big")
|
|
297
|
+
|
|
298
|
+
def hex(self):
|
|
299
|
+
"""
|
|
300
|
+
hex returns self.bites as
|
|
301
|
+
a hex string
|
|
302
|
+
"""
|
|
303
|
+
return hex(self.int())
|
|
304
|
+
|
|
305
|
+
def _encode_crc(self):
|
|
306
|
+
"""
|
|
307
|
+
_encode_crc encode crc32
|
|
308
|
+
"""
|
|
309
|
+
crc_int = crc32(self.bites)
|
|
310
|
+
self.info_section.crc = hex(crc_int)
|
|
311
|
+
self.bites += int.to_bytes(crc_int, 4, byteorder="big")
|
|
312
|
+
|
|
313
|
+
def _unloop_descriptors(self):
|
|
314
|
+
"""
|
|
315
|
+
_unloop_descriptors
|
|
316
|
+
for each descriptor in self.descriptors
|
|
317
|
+
encode descriptor tag, descriptor length,
|
|
318
|
+
and the descriptor into all_bites.bites
|
|
319
|
+
"""
|
|
320
|
+
all_bites = NBin()
|
|
321
|
+
dbite_chunks = [dsptr.encode() for dsptr in self.descriptors]
|
|
322
|
+
for chunk, dsptr in zip(dbite_chunks, self.descriptors):
|
|
323
|
+
dsptr.descriptor_length = len(chunk)
|
|
324
|
+
all_bites.add_int(dsptr.tag, 8)
|
|
325
|
+
all_bites.add_int(dsptr.descriptor_length, 8)
|
|
326
|
+
all_bites.add_bites(chunk)
|
|
327
|
+
return all_bites.bites
|
|
328
|
+
|
|
329
|
+
def _load_info_section(self, gonzo):
|
|
330
|
+
"""
|
|
331
|
+
load_info_section loads data for Cue.info_section
|
|
332
|
+
isec should be a dict.
|
|
333
|
+
if 'splice_command_type' is included,
|
|
334
|
+
an empty command instance will be created for Cue.command
|
|
335
|
+
"""
|
|
336
|
+
if "info_section" in gonzo:
|
|
337
|
+
self.info_section.load(gonzo["info_section"])
|
|
338
|
+
|
|
339
|
+
def _load_command(self, gonzo):
|
|
340
|
+
"""
|
|
341
|
+
load_command loads data for Cue.command
|
|
342
|
+
cmd should be a dict.
|
|
343
|
+
if 'command_type' is included,
|
|
344
|
+
the command instance will be created.
|
|
345
|
+
"""
|
|
346
|
+
if "command" not in gonzo:
|
|
347
|
+
self._no_cmd()
|
|
348
|
+
return False
|
|
349
|
+
cmd = gonzo["command"]
|
|
350
|
+
if "command_type" in cmd:
|
|
351
|
+
self.command = command_map[cmd["command_type"]]()
|
|
352
|
+
self.command.load(cmd)
|
|
353
|
+
|
|
354
|
+
def _load_descriptors(self, dlist):
|
|
355
|
+
"""
|
|
356
|
+
Load_descriptors loads descriptor data.
|
|
357
|
+
dlist is a list of dicts
|
|
358
|
+
if 'tag' is included in each dict,
|
|
359
|
+
a descriptor instance will be created.
|
|
360
|
+
"""
|
|
361
|
+
## if not isinstance(dlist, list):
|
|
362
|
+
## raise Exception("\033[7mdescriptors should be a list\033[27m")
|
|
363
|
+
for dstuff in dlist:
|
|
364
|
+
dscptr = descriptor_map[dstuff["tag"]]()
|
|
365
|
+
dscptr.load(dstuff)
|
|
366
|
+
self.descriptors.append(dscptr)
|
|
367
|
+
|
|
368
|
+
def _no_cmd(self):
|
|
369
|
+
"""
|
|
370
|
+
_no_cmd raises an exception if no splice command.
|
|
371
|
+
"""
|
|
372
|
+
raise Exception("\033[7mA splice command is required\033[27m")
|
|
373
|
+
|
|
374
|
+
def load(self, gonzo):
|
|
375
|
+
"""
|
|
376
|
+
Cue.load loads SCTE35 data for encoding.
|
|
377
|
+
gonzo is a dict or json
|
|
378
|
+
with any or all of these keys
|
|
379
|
+
gonzo = {
|
|
380
|
+
'info_section': {dict} ,
|
|
381
|
+
'command': {dict},
|
|
382
|
+
'descriptors': [list of {dicts}],
|
|
383
|
+
}
|
|
384
|
+
"""
|
|
385
|
+
if isinstance(gonzo, bytes):
|
|
386
|
+
gonzo = gonzo.decode()
|
|
387
|
+
if isinstance(gonzo, str):
|
|
388
|
+
if gonzo.isdigit():
|
|
389
|
+
gonzo = int(gonzo)
|
|
390
|
+
self.bites = self._int_bits(int(gonzo))
|
|
391
|
+
self.decode()
|
|
392
|
+
return self.bites
|
|
393
|
+
if gonzo.strip()[0] == "<":
|
|
394
|
+
self.from_xml(gonzo)
|
|
395
|
+
return self.bites
|
|
396
|
+
gonzo = json.loads(gonzo)
|
|
397
|
+
if "command" not in gonzo:
|
|
398
|
+
self._no_cmd()
|
|
399
|
+
self._load_info_section(gonzo)
|
|
400
|
+
self._load_command(gonzo)
|
|
401
|
+
self._load_descriptors(gonzo["descriptors"])
|
|
402
|
+
self.encode()
|
|
403
|
+
return self.bites
|
|
404
|
+
|
|
405
|
+
def from_xml(self, gonzo):
|
|
406
|
+
"""
|
|
407
|
+
build_cue takes the data put into the gonzo dict
|
|
408
|
+
and builds a scte35.Cue instance
|
|
409
|
+
"""
|
|
410
|
+
sxp = SuperXmlParser()
|
|
411
|
+
dat = sxp.xml2cue(gonzo)
|
|
412
|
+
if isinstance(
|
|
413
|
+
dat, str
|
|
414
|
+
): # a string is returned for Binary xml tag, make sense?
|
|
415
|
+
self.bites = self._mk_bits(dat)
|
|
416
|
+
self.decode()
|
|
417
|
+
else:
|
|
418
|
+
self.load(dat) # a dict is returned for infosection xml.
|
|
419
|
+
# Self.encode() will calculate lengths and types and such
|
|
420
|
+
# self.encode()
|
|
421
|
+
|
|
422
|
+
def _xml_mk_descriptor(self, sis, ns):
|
|
423
|
+
"""
|
|
424
|
+
_mk_descriptor_xml make xml nodes for descriptors.
|
|
425
|
+
"""
|
|
426
|
+
for d in self.descriptors:
|
|
427
|
+
if d.has("segmentation_type_id") and d.segmentation_type_id in table22:
|
|
428
|
+
comment = f"{table22[d.segmentation_type_id]}"
|
|
429
|
+
sis.add_comment(comment)
|
|
430
|
+
sis.add_child(d.xml(ns=ns))
|
|
431
|
+
return sis
|
|
432
|
+
|
|
433
|
+
def xml(self, ns="scte35"):
|
|
434
|
+
"""
|
|
435
|
+
xml returns a scte35.Node instance
|
|
436
|
+
which can be edited as needed or printed.
|
|
437
|
+
xmlbin
|
|
438
|
+
"""
|
|
439
|
+
sis = self.info_section.xml(ns=ns)
|
|
440
|
+
cmd = self.command.xml(ns=ns)
|
|
441
|
+
sis.add_child(cmd)
|
|
442
|
+
sis = self._xml_mk_descriptor(sis, ns)
|
|
443
|
+
sis.mk()
|
|
444
|
+
return sis
|
|
445
|
+
|
|
446
|
+
def xmlbin(self, ns="scte35"):
|
|
447
|
+
"""
|
|
448
|
+
xml returns a scte35.Node instance
|
|
449
|
+
which can be edited as needed or printed.
|
|
450
|
+
xmlbin
|
|
451
|
+
"""
|
|
452
|
+
sig_attrs = {"xmlns": "https://scte.org/schemas/35"}
|
|
453
|
+
sig_node = Node("Signal", attrs=sig_attrs, ns=ns)
|
|
454
|
+
bin_node = Node("Binary", value=self.encode(), ns=ns)
|
|
455
|
+
sig_node.add_child(bin_node)
|
|
456
|
+
return sig_node
|