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/streamtypes.py ADDED
@@ -0,0 +1,127 @@
1
+ """
2
+
3
+ streamtypes.py
4
+
5
+ Used by scte35.Stream
6
+ On import stream_types generates a complete streamtype_map.
7
+
8
+ from .streamtypes import streamtype_map
9
+
10
+ """
11
+
12
+ streamtype_map = {
13
+ 0x00: "Reserved",
14
+ 0x01: "MPEG-1 video",
15
+ 0x02: "H.262 video",
16
+ 0x03: "MPEG-2 audio",
17
+ 0x04: "MPEG-2 halved sample rate audio",
18
+ 0x05: "MPEG-2 tabled data",
19
+ 0x06: "MPEG-2 binary data",
20
+ 0x07: "ISO/IEC 13522 (MHEG)",
21
+ 0x08: "H.222 and DSM CC",
22
+ 0x09: "H.222 and auxiliary data",
23
+ 0x0A: "DSM CC multiprotocol encapsulation",
24
+ 0x0B: "DSM CC U-N messages",
25
+ 0x0C: "DSM CC stream descriptors",
26
+ 0x0D: "DSM CC tabled data",
27
+ 0x0E: "auxiliary data",
28
+ 0x0F: "ADTS AAC ",
29
+ 0x10: "MPEG-4 H.263 based video",
30
+ 0x11: "MPEG-4 LOAS multi-format framed audio",
31
+ 0x12: "MPEG-4 FlexMux",
32
+ 0x13: "MPEG-4 FlexMux",
33
+ 0x14: "DSM CC ",
34
+ 0x15: "ID3",
35
+ 0x16: "Sectioned metadata",
36
+ 0x17: "DSM CC Data Carousel metadata",
37
+ 0x18: "DSM CC Object Carousel metadata",
38
+ 0x19: "Synchronized Download Protocol metadata",
39
+ 0x1A: "IPMP",
40
+ 0x1B: "H.264",
41
+ 0x1C: "MPEG-4 raw audio)",
42
+ 0x1D: "MPEG-4 text",
43
+ 0x1E: "MPEG-4 auxiliary video",
44
+ 0x1F: "SVC (MPEG-4 AVC sub-bitstream)",
45
+ 0x20: "MVC (MPEG-4 AVC sub-bitstream)",
46
+ 0x21: "JPEG 2000 video",
47
+ # 0x22 - 0x23:"Reserved.",
48
+ 0x24: " H.265",
49
+ # 0x25 - 0x41:"Reserved.",
50
+ 0x42: "Chinese Video Standard",
51
+ # 0x43 - 0x7e:"Reserved.",
52
+ 0x7F: " DRM Trash",
53
+ 0x80: "H.262 with DES-64-CBC ",
54
+ 0x81: "AC-3",
55
+ 0x82: "SCTE subtitle",
56
+ 0x83: "Dolby TrueHD audio ",
57
+ 0x84: "AC-3",
58
+ 0x85: "DTS 8 channel audio for Blu-ray",
59
+ 0x86: "SCTE-35",
60
+ 0x87: "AC-3 up to 16 channel audio for ATSC",
61
+ # 0x88 - 0x89:"Privately defined.",
62
+ 0x90: "Blu-ray Presentation Graphic Stream (subtitling)",
63
+ 0x91: "ATSC DSM CC Network Resources table",
64
+ # 0x92 - 0xBF:"Privately defined.",
65
+ 0xC0: "DigiCipher II text",
66
+ 0xC1: "AC-3 with AES-128-CBC ",
67
+ 0xC2: "ATSC DSM CC synchronous data",
68
+ # 0xC3 - 0xCE:"Privately defined.",
69
+ 0xCF: "ADTS AAC with AES-128-CBC frame encryption",
70
+ 0xD0: "Privately defined.",
71
+ 0xD1: "BBC Dirac ",
72
+ 0xD2: "AVS2 ",
73
+ 0xD3: "AVS3 Audio",
74
+ 0xD4: "AVS3 Video ",
75
+ # 0xD5 - 0xDA:"Privately defined.",
76
+ 0xDB: " H.264 with AES-128-CBC slice encryption",
77
+ # 0xDC - 0xE9:"Privately defined.",
78
+ 0xEA: "Windows janky media v9",
79
+ # 0xEB - 0xFB:"Privately defined.",
80
+ 0xFC: "KLV",
81
+ }
82
+
83
+
84
+ others = {
85
+ "Reserved": list(range(0x22, 0x23))
86
+ + list(range(0x25, 0x41))
87
+ + list(range(0x43, 0x7E)),
88
+ "Private": list(range(0x88, 0x89))
89
+ + list(range(0x92, 0xBF))
90
+ + list(range(0xC3, 0xCE))
91
+ + list(range(0xD5, 0xDA))
92
+ + list(range(0xDC, 0xE9))
93
+ + list(range(0xEB, 0xFB))
94
+ + list(range(0xFD, 0xFF)),
95
+ }
96
+
97
+
98
+ def hex_literal(integer):
99
+ """
100
+ Return hex literal
101
+ """
102
+ return int(hex(integer), base=16)
103
+
104
+
105
+ def add_stream_types(alist, streamtype):
106
+ """
107
+ add_stream_types dynamically
108
+ adds a range of a stream type,
109
+ like "Reserved",to streamtype_map
110
+ """
111
+ for i in alist:
112
+ j = hex_literal(i)
113
+ streamtype_map[j] = streamtype
114
+
115
+
116
+ def mk_streamtype_map(others):
117
+ """
118
+ mk_streamtype_map dynamically adds
119
+ stream types to streamtype_map
120
+ """
121
+ for k, v in others.items():
122
+ add_stream_types(v, k)
123
+ keys = list(streamtype_map.keys())
124
+ keys.sort()
125
+
126
+
127
+ mk_streamtype_map(others)
scte35/sxp.py ADDED
@@ -0,0 +1,350 @@
1
+ """
2
+ sxp.py
3
+ home of the SuperXmlParser class
4
+
5
+ """
6
+
7
+ from xml.sax.saxutils import escape, unescape
8
+ from .xml import t2s, un_camel, un_xml, strip_ns, iter_attrs
9
+ from .descriptors import descriptor_map, k_by_v
10
+ from .segmentation import table20, table22
11
+ from .upids import upid_map
12
+
13
+
14
+ class SuperXmlParser:
15
+
16
+ def _split_attrs(self, node):
17
+ node = node.replace("='", '="').replace("' ", '" ')
18
+ attrs = [x for x in node.split(" ") if "=" in x]
19
+ return attrs
20
+
21
+ def mk_attrs(self, node):
22
+ """
23
+ mk_attrs parses the current node for attributes
24
+ and stores them in self.stuff[self.active]
25
+ """
26
+ if "!--" in node:
27
+ return False
28
+ attrs = self._split_attrs(node)
29
+ parsed = {
30
+ x.split('="')[0]: unescape(x.split('="')[1].split('"')[0]) for x in attrs
31
+ }
32
+ it = iter_attrs(parsed)
33
+ return it
34
+
35
+ def parsed(self, tag, sub_data):
36
+ """
37
+ parsed returns tag, attrs and value
38
+ for an xml element
39
+ """
40
+ value = ""
41
+ attrs = {}
42
+ if tag[0] != "/":
43
+ tag = strip_ns(tag)
44
+ o_data = sub_data
45
+ if ">" in sub_data:
46
+ ridx = sub_data.index(">")
47
+ this_node = sub_data[: ridx + 1]
48
+ attrs = self.mk_attrs(this_node)
49
+ sub_data = sub_data[ridx + 1 :]
50
+ if "<" in sub_data:
51
+ lidx = sub_data.index("<")
52
+ value = sub_data[:lidx].strip()
53
+ sub_data = sub_data[lidx:].replace("\t", "")
54
+ return {
55
+ "tag": tag,
56
+ "attrs": attrs,
57
+ "this": value,
58
+ "xml": o_data,
59
+ }
60
+
61
+ def mk_tag(self, data):
62
+ """
63
+ mk_tag parse out the
64
+ next available xml tag from data
65
+ """
66
+ tag = data[1:].split(" ", 1)[0].split(">", 1)[0]
67
+ return tag.strip()
68
+
69
+ def mk_element(self, data, tag):
70
+ """
71
+ mk_element slices out the tag element
72
+ inclusive: <tag > stuff</tag> and <tag attr="1"/>
73
+ """
74
+ try:
75
+ return data[: data.index(f"</{tag}>") + len(tag) + 2]
76
+ except:
77
+ return data[: data.index("/>") + 1]
78
+
79
+ def mk(self, exemel, target="SpliceInfoSection"):
80
+ """
81
+ parse parses an xml string for a SCTE-35 Cue.
82
+ """
83
+ results = []
84
+ sub_data = None
85
+ data = exemel.replace("\n", "").strip()
86
+ while data:
87
+ sub_data = None
88
+ tag = self.mk_tag(data)
89
+ if target in tag: # don't even consider namespace
90
+ sub_data = self.mk_element(data, tag)
91
+ results.append(sub_data + ">")
92
+ data = data.replace(sub_data, "")
93
+ else:
94
+ data = data.split(">", 1)[1]
95
+ if "<" in data:
96
+ data = data[data.index("<") :]
97
+ else:
98
+ return results
99
+
100
+ def gimme(self, tags, exemel):
101
+ """
102
+ gimme get all instances of tag from exemel and return a list of them.
103
+ """
104
+ the_list = []
105
+ for tag in tags:
106
+ element_list = [self.parsed(tag, sd) for sd in self.mk(exemel, tag)]
107
+ if element_list:
108
+ the_list.extend(element_list)
109
+ return the_list
110
+
111
+ def gimme_one(self, tag, exemel):
112
+ """
113
+ gimme_one get the first instance of tag from exemel and return it.
114
+ """
115
+ one = self.gimme([tag], exemel)
116
+ if one:
117
+ return one[0]
118
+
119
+ def spliceinfosection(self, exemel):
120
+ """
121
+ spliceinfosection parses exemel for info section data
122
+ and returns a loadable dict
123
+ """
124
+ my_name = "SpliceInfoSection"
125
+
126
+ out = self.gimme_one(my_name, exemel)
127
+ if out:
128
+ return out["attrs"]
129
+ return {}
130
+
131
+ def command(self, exemel):
132
+ """
133
+ command parses exemel for a splice command
134
+ and returns a loadable dict
135
+
136
+ """
137
+ ts = self.timesignal(exemel)
138
+ if ts:
139
+ return ts
140
+ si = self.spliceinsert(exemel)
141
+ if si:
142
+ return si
143
+ pc = self.privatecommand(exemel)
144
+ if pc:
145
+ return pc
146
+ return {}
147
+
148
+ def timesignal(self, exemel):
149
+ """
150
+ timesignal parses exemel for TimeSignal data
151
+ and creates a loadable dict for the Cue class.
152
+ """
153
+ my_name = "TimeSignal"
154
+ ts = self.gimme_one("TimeSignal", exemel)
155
+ if ts:
156
+ splice_time = self.splicetime(ts["xml"])
157
+ ts["attrs"].update(splice_time)
158
+ setme = {
159
+ "name": "Time Signal",
160
+ "command_type": 6,
161
+ }
162
+ ts["attrs"].update(setme)
163
+ return ts["attrs"]
164
+ return {}
165
+
166
+ def privatecommand(self, exemel):
167
+ """
168
+ privatecommand parses exemel for PrivateCommand
169
+ data and creates a loadable dict for the Cue class.
170
+ """
171
+ my_name = "PrivateCommand"
172
+ pc = self.gimme_one("PrivateCommand", exemel)
173
+ if pc:
174
+ setme = {
175
+ "name": "Private Command",
176
+ "command_type": 255,
177
+ "private_bytes": pc["this"],
178
+ }
179
+ pc["attrs"].update(setme)
180
+ return pc["attrs"]
181
+ return {}
182
+
183
+ def spliceinsert(self, exemel):
184
+ """
185
+ spliceinsert parses exemel for SpliceInsert data
186
+ and creates a loadable dict for the Cue class.
187
+ """
188
+ my_name = "SpliceInsert"
189
+ si = self.gimme_one(my_name, exemel)
190
+ if si:
191
+ setme = {
192
+ "name": my_name,
193
+ "command_type": 5,
194
+ "event_id_compliance_flag": True,
195
+ "program_splice_flag": False,
196
+ "duration_flag": False,
197
+ }
198
+ splice_time = self.splicetime(exemel)
199
+ if splice_time:
200
+ setme["program_splice_flag"] = True
201
+ si["attrs"].update(setme)
202
+ si["attrs"].update(splice_time)
203
+ break_duration = self.breakduration(exemel)
204
+ si["attrs"].update(break_duration)
205
+ return si["attrs"]
206
+ return {}
207
+
208
+ def splicetime(self, exemel):
209
+ """
210
+ splicetime parses xml from a splice command
211
+ to get pts_time, sets time_specified_flag to True
212
+ """
213
+ splicetime = self.gimme_one("SpliceTime", exemel)
214
+ if splicetime:
215
+ return {
216
+ "pts_time": splicetime["attrs"]["pts_time"],
217
+ "time_specified_flag": True,
218
+ }
219
+ return {}
220
+
221
+ def breakduration(self, exemel):
222
+ """
223
+ breakduration parses xml for break duration, break_auto_return
224
+ and sets duration_flag to True.
225
+ """
226
+ break_duration = self.gimme_one("BreakDuration", exemel)
227
+ if break_duration:
228
+ return {
229
+ "break_duration": break_duration["attrs"]["duration"],
230
+ "break_auto_return": break_duration["attrs"]["auto_return"],
231
+ "duration_flag": True,
232
+ }
233
+ return {}
234
+
235
+ def segmentationdescriptor(self, dscptr):
236
+ """
237
+ segmentationdescriptor creates a dict to be loaded.
238
+ """
239
+ my_name = "SegmentationDescriptor"
240
+ setme = {
241
+ "tag": 2,
242
+ "identifier": "CUEI",
243
+ "name": "Segmentation Descriptor",
244
+ "segmentation_event_id_compliance_indicator": True,
245
+ "program_segmentation_flag": True,
246
+ "segmentation_duration_flag": False,
247
+ "delivery_not_restricted_flag": True,
248
+ "segmentation_event_id": hex(dscptr["attrs"]["segmentation_event_id"]),
249
+ }
250
+ if dscptr["attrs"]["segmentation_type_id"] in table22:
251
+ dscptr["attrs"]["segmentation_message"] = table22[
252
+ dscptr["attrs"]["segmentation_type_id"]
253
+ ]
254
+ dscptr["attrs"].update(setme)
255
+ dr = self.deliveryrestrictions(dscptr["xml"])
256
+ dscptr["attrs"].update(dr)
257
+ the_upid = self.upids(dscptr["xml"])
258
+ dscptr["attrs"].update(the_upid)
259
+ return dscptr["attrs"]
260
+
261
+ def upids(self, exemel):
262
+ """
263
+ upids parses out upids from a splice descriptors xml
264
+ """
265
+ ulist = self.gimme(["SegmentationUpid"], exemel)
266
+ if len(ulist) == 1:
267
+ try:
268
+ seg_upid = bytes.fromhex(ulist[0]["this"].lower().replace("0x", ""))
269
+ except ValueError:
270
+ seg_upid = ulist[0]["this"]
271
+ seg_upid_type = ulist[0]["attrs"]["segmentation_upid_type"]
272
+ the_upid = {
273
+ "segmentation_upid": ulist[0]["this"],
274
+ "segmentation_upid_type": seg_upid_type,
275
+ "segmentation_upid_type_name": upid_map[seg_upid_type][0],
276
+ "segmentation_upid_length": len(seg_upid),
277
+ }
278
+ return the_upid
279
+
280
+ def availdescriptor(self, dscptr):
281
+ my_name = "AvailDescriptor"
282
+ setme = {
283
+ "tag": 0,
284
+ "identifier": "CUEI",
285
+ "name": my_name,
286
+ }
287
+ dscptr["attrs"].update(setme)
288
+ return dscptr["attrs"]
289
+
290
+ ## def dtmfdescriptor(self,dscptr)
291
+ ## """
292
+ ## Load an DTMFDescriptor from XML
293
+ ## """
294
+ ## setme={"tag": 1,
295
+ ## "identifier": "CUEI",
296
+ ## "name":my_name,}
297
+ ## gonzo["DTMFDescriptor"]["dtmf_chars"] = gonzo["DTMFDescriptor"].pop("chars")
298
+ ## self.load(gonzo["DTMFDescriptor"])
299
+ ## self.dtmf_count = len(self.dtmf_chars)
300
+
301
+ def timedescriptor(self, dscptr):
302
+ my_name = "TimeDescriptor"
303
+ setme = {
304
+ "tag": 3,
305
+ "identifier": "CUEI",
306
+ "name": my_name,
307
+ }
308
+ dscptr["attrs"].update(setme)
309
+ return dscptr["attrs"]
310
+
311
+ def descriptors(self, exemel):
312
+ dmap = {
313
+ "AvailDescriptor": self.availdescriptor,
314
+ # "DTMFDescriptor",
315
+ "SegmentationDescriptor": self.segmentationdescriptor,
316
+ # "TimeDescriptor",
317
+ }
318
+ out = []
319
+ dlist = self.gimme(list(dmap.keys()), exemel)
320
+ for dscptr in dlist:
321
+ out.append(dmap[dscptr["tag"]](dscptr))
322
+ return out
323
+
324
+ def deliveryrestrictions(self, exemel):
325
+ dr = self.gimme_one("DeliveryRestrictions", exemel)
326
+ if dr:
327
+ setme = {
328
+ "delivery_not_restricted_flag": False,
329
+ "device_restrictions": table20[dr["attrs"]["device_restrictions"]],
330
+ }
331
+ dr["attrs"].update(setme)
332
+ return dr["attrs"]
333
+ return {}
334
+
335
+ def xml2cue(self, exemel):
336
+ """
337
+ xml2cue returns a base64 string for xmlbin
338
+ and a dict for xml
339
+ """
340
+ bindata = self.gimme_one("Binary", exemel)
341
+ if bindata:
342
+ return bindata["this"]
343
+ splice_info = self.spliceinfosection(exemel)
344
+ cmd = self.command(exemel)
345
+ dscptrs = self.descriptors(exemel)
346
+ return {
347
+ "info_section": splice_info,
348
+ "command": cmd,
349
+ "descriptors": dscptrs,
350
+ }