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/packetdata.py ADDED
@@ -0,0 +1,39 @@
1
+ """
2
+ packetdata.py
3
+ """
4
+
5
+ from .base import SCTE35Base
6
+
7
+
8
+ class PacketData(SCTE35Base):
9
+ """
10
+ A PacketData instance is used
11
+ to hold packet pid, program, pcr and pts
12
+ for SCTE-35 packets.
13
+ """
14
+
15
+ def __init__(self, pid, prgm):
16
+ self.pid = hex(pid)
17
+ self.program = prgm
18
+ self.pcr = None
19
+ self.pts = None
20
+
21
+ def mk_pcr(self, table):
22
+ """
23
+ calculates and formats pcr
24
+ """
25
+ try:
26
+ pcr_ticks = table[self.program]
27
+ self.pcr = self.as_90k(pcr_ticks)
28
+ except:
29
+ pass
30
+
31
+ def mk_pts(self, table):
32
+ """
33
+ mk_pts calculates and formats pts
34
+ """
35
+ try:
36
+ pts_ticks = table[self.program]
37
+ self.pts = self.as_90k(pts_ticks)
38
+ except:
39
+ pass
scte35/section.py ADDED
@@ -0,0 +1,194 @@
1
+ """
2
+ section.py
3
+
4
+ SCTE35 Splice Info Section
5
+ """
6
+
7
+ from .bitn import BitBin
8
+ from .base import SCTE35Base
9
+ from .xml import Node
10
+
11
+
12
+ sap_map = {
13
+ "0x00": "Type 1 Closed GOP with no leading pictures",
14
+ "0x01": "Type 2 Closed GOP with leading pictures",
15
+ "0x02": "Type 3 Open GOP",
16
+ "0x03": "No Sap Type",
17
+ }
18
+
19
+
20
+ class SpliceInfoSection(SCTE35Base):
21
+ """
22
+ The SCTE-35 splice info section
23
+ data.
24
+ """
25
+
26
+ def __init__(self):
27
+ self.table_id = None
28
+ self.section_syntax_indicator = None
29
+ self.private = None
30
+ # sap_type used to be marked reserved.
31
+ self.sap_type = None
32
+ self.sap_details = None
33
+ self.section_length = None
34
+ self.protocol_version = None
35
+ self.encrypted_packet = None
36
+ self.encryption_algorithm = None
37
+ self.pts_adjustment = 0
38
+ self.cw_index = None
39
+ self.tier = None
40
+ self.splice_command_length = None
41
+ self.splice_command_type = None
42
+ self.descriptor_loop_length = 0
43
+ self.crc = None
44
+
45
+ def decode(self, bites):
46
+ """
47
+ InfoSection.decode
48
+ """
49
+ bitbin = BitBin(bites)
50
+ self.table_id = bitbin.as_hex(8)
51
+ if self.table_id != "0xfc":
52
+ raise ValueError(
53
+ ("splice_info_section.table_id should be 0xfc Not: ", self.table_id)
54
+ )
55
+ self.section_syntax_indicator = bitbin.as_flag(1)
56
+ self.private = bitbin.as_flag(1)
57
+ self.sap_type = bitbin.as_hex(2)
58
+ self.sap_details = sap_map[self.sap_type]
59
+ self.section_length = bitbin.as_int(12)
60
+ self.protocol_version = bitbin.as_int(8)
61
+ self.encrypted_packet = bitbin.as_flag(1)
62
+ self.encryption_algorithm = bitbin.as_int(6)
63
+ pts_adjustment_ticks = bitbin.as_int(33)
64
+ self.pts_adjustment = self.as_90k(pts_adjustment_ticks)
65
+ self.cw_index = bitbin.as_hex(8)
66
+ self.tier = bitbin.as_hex(12)
67
+ self.splice_command_length = bitbin.as_int(12)
68
+ self.splice_command_type = bitbin.as_int(8)
69
+
70
+ def _encode_table_id(self, nbin):
71
+ """
72
+ encode SpliceInfoSection.table_id
73
+ """
74
+ self.table_id = "0xfc"
75
+ nbin.add_hex(self.table_id, 8)
76
+
77
+ def _encode_section_syntax_indicator(self, nbin):
78
+ """
79
+ encode SpliceInfoSection.section_syntax_indicator
80
+ """
81
+ self.section_syntax_indicator = False
82
+ nbin.add_flag(self.section_syntax_indicator, 1)
83
+
84
+ def _encode_private_flag(self, nbin):
85
+ """
86
+ encode SpliceInfoSection.private
87
+ """
88
+ self.private = False
89
+ nbin.add_flag(self.private, 1)
90
+
91
+ def _encode_sap(self, nbin):
92
+ """
93
+ the 3 reserved bits now map SAP
94
+ """
95
+ if self.sap_type not in sap_map:
96
+ self.sap_type = "0x03"
97
+ self.sap_details = sap_map[self.sap_type]
98
+ nbin.add_hex(self.sap_type, 2)
99
+
100
+ def _encode_section_length(self, nbin):
101
+ """
102
+ encode SpliceInfoSection.section_length
103
+ """
104
+ if not self.section_length:
105
+ self.section_length = 11
106
+ nbin.add_int(self.section_length, 12)
107
+
108
+ def _encode_protocol_version(self, nbin):
109
+ """
110
+ encode SpliceInfoSection.protocol_version
111
+ """
112
+ if not self.protocol_version:
113
+ self.protocol_version = 0
114
+ nbin.add_int(self.protocol_version, 8)
115
+
116
+ def _encode_encrypted(self, nbin):
117
+ """
118
+ encode SpliceInfoSection.encrypted_packet
119
+ and SpliceInfoSection.encyption_algorithm
120
+ """
121
+ if not self.encrypted_packet:
122
+ self.encrypted_packet = False
123
+ nbin.add_flag(self.encrypted_packet)
124
+ if not self.encryption_algorithm:
125
+ self.encryption_algorithm = 0
126
+ nbin.add_int(self.encryption_algorithm, 6)
127
+
128
+ def _encode_pts_adjustment(self, nbin):
129
+ """
130
+ encode SpliceInfoSection.pts_adjustment_ticks
131
+ """
132
+ nbin.add_int(self.as_ticks(self.pts_adjustment), 33)
133
+
134
+ def _encode_cw_index(self, nbin):
135
+ """
136
+ encode SpliceInfoSection.cw_index
137
+ """
138
+ if not self.cw_index:
139
+ self.cw_index = "0x0"
140
+ nbin.add_hex(self.cw_index, 8)
141
+
142
+ def _encode_tier(self, nbin):
143
+ """
144
+ encode SpliceInfoSection.tier
145
+ """
146
+ if not self.tier:
147
+ self.tier = "0xfff"
148
+ nbin.add_hex(self.tier, 12)
149
+
150
+ def _encode_splice_command(self, nbin):
151
+ """
152
+ encode Splice.InfoSection.splice_command_length
153
+ and Splice.InfoSection.splice_command_type
154
+ """
155
+ if not self.splice_command_length:
156
+ self.splice_command_length = 0
157
+ nbin.add_int(self.splice_command_length, 12)
158
+ if not self.splice_command_type:
159
+ self.splice_command_type = 0
160
+ nbin.add_int(self.splice_command_type, 8)
161
+
162
+ def encode(self, nbin=None):
163
+ """
164
+ SpliceInfoSection.encode
165
+ takes the vars from an instance and
166
+ encodes them as bytes.
167
+ """
168
+ nbin = self._chk_nbin(nbin)
169
+ self._encode_table_id(nbin)
170
+ self._encode_section_syntax_indicator(nbin)
171
+ self._encode_private_flag(nbin)
172
+ self._encode_sap(nbin)
173
+ self._encode_section_length(nbin)
174
+ self._encode_protocol_version(nbin)
175
+ self._encode_encrypted(nbin)
176
+ self._encode_pts_adjustment(nbin)
177
+ self._encode_cw_index(nbin)
178
+ self._encode_tier(nbin)
179
+ self._encode_splice_command(nbin)
180
+ return nbin.bites
181
+
182
+ def xml(self, ns="scte35"):
183
+ """
184
+ xml create xml node for splice info section
185
+ """
186
+ sis_attrs = {
187
+ "xmlns": "https://scte.org/schemas/35",
188
+ "pts_adjustment": self.as_ticks(self.pts_adjustment),
189
+ "protocol_version": self.protocol_version,
190
+ "sap_type": self.sap_type,
191
+ "tier": self.tier,
192
+ }
193
+ sis = Node("SpliceInfoSection", attrs=sis_attrs, ns=ns)
194
+ return sis
scte35/segment.py ADDED
@@ -0,0 +1,128 @@
1
+ """
2
+ The scte35.Segment class
3
+ """
4
+
5
+ import os
6
+ from new_reader import reader
7
+ from .stream import Stream
8
+
9
+ AES = True
10
+ try:
11
+ import pyaes
12
+ except:
13
+ AES = False
14
+
15
+
16
+ class Segment(Stream):
17
+ """
18
+ The Segment class is a Sub Class of scte35.Stream
19
+ made for small, fixed size MPEGTS files,like HLS segments.
20
+
21
+ Segment Class Specific Features:
22
+
23
+ * Decryption of AES Encrypted MPEGTS.
24
+
25
+ * Segment.cues a list of SCTE35 cues found in the segment.
26
+
27
+
28
+ Example:
29
+
30
+ from scte35 import Segment
31
+
32
+ >>>> uri = "https://example.com/1.ts"
33
+ >>>> seg = Segment(uri)
34
+ >>>> seg.decode()
35
+ >>>> [cue.encode() for cue in seg.cues]
36
+ ['/DARAAAAAAAAAP/wAAAAAHpPv/8=',
37
+ '/DAvAAAAAAAAAP/wFAUAAAKWf+//4WoauH4BTFYgAAEAAAAKAAhDVUVJAAAAAOv1oqc=']
38
+
39
+
40
+ AES Encryption Example:
41
+
42
+ from scte35 import Segment
43
+
44
+ >>>> key = "https://example.com/aes.key"
45
+ >>>> IV=0x998C575D24F514AEC84EDC5CABCCDB81
46
+ >>>> uri = "https://example.com/aes-1.ts"
47
+
48
+ >>>> seg = Segment(uri,key_uri=key, iv=IV)
49
+ >>>> seg.decode()
50
+ >>>> {cue.packet_data.pts:cue.encode() for cue in seg.cues}
51
+
52
+ { 89718.451333: '/DARAAAAAAAAAP/wAAAAAHpPv/8=',
53
+ 89730.281789: '/DAvAAAAAAAAAP/wFAUAAAKWf+//4WoauH4BTFYgAAEAAAAKAAhDVUVJAAAAAOv1oqc='}
54
+
55
+ """
56
+
57
+ def __init__(self, seg_uri, key_uri=None, iv=None):
58
+ self.seg_uri = seg_uri
59
+ self.key_uri = key_uri
60
+ self.key = None
61
+ self.iv = None
62
+ self.cues = []
63
+ self.pts_start = None
64
+ self.pts_last = None
65
+ self.shush = False
66
+ self.tmp = None
67
+ self.duration = None
68
+ if AES:
69
+ if iv:
70
+ iv = iv.replace("\n", "")
71
+ self.iv = int.to_bytes(int(iv, base=16), 16, byteorder="big")
72
+ # if self.key_uri:
73
+ self._aes_get_key()
74
+ self._aes_decrypt()
75
+ super().__init__(self.seg_uri)
76
+
77
+ def __repr__(self):
78
+ return str(self.__dict__)
79
+
80
+ def _mk_tmp(self):
81
+ self.tmp = "tf-"
82
+ self.tmp += self.seg_uri.rsplit("/", 1)[-1]
83
+
84
+ def _aes_get_key(self):
85
+ with reader(self.key_uri) as quay:
86
+ self.key = quay.read()
87
+
88
+ def _aes_decrypt(self):
89
+ mode = pyaes.AESModeOfOperationCBC(self.key, iv=self.iv)
90
+ self._mk_tmp()
91
+ with open(self.tmp, "wb") as outfile, reader(self.seg_uri) as infile:
92
+ pyaes.decrypt_stream(mode, infile, outfile)
93
+ self.seg_uri = self.tmp
94
+
95
+ def _add_cue(self, cue):
96
+ """
97
+ _add_cue is called by a segment instance
98
+ to collect SCTE35 cues.
99
+ """
100
+ self.cues.append(cue)
101
+
102
+ def shushed(self):
103
+ """
104
+ shushed sets self.shush to true to suppress
105
+ printing SCTE-35 Cue data.
106
+ """
107
+ self.shush = True
108
+
109
+ def show_cue(self, cue):
110
+ """
111
+ show_cue prints SCTE35 Cue data
112
+ and calls add_cue to append the cue to
113
+ the Segment,cues list.
114
+ """
115
+ if not self.shush:
116
+ cue.show()
117
+ self._add_cue(cue)
118
+
119
+ def decode(self, func=None):
120
+ """
121
+ decode a mpegts segment.
122
+ """
123
+ super().decode(func=self.show_cue)
124
+ self.pts_start = self.as_90k(self.start.popitem()[1])
125
+ self.pts_last = self.as_90k(list(self.maps.prgm_pts.items())[0][1])
126
+ if self.tmp:
127
+ os.unlink(self.tmp)
128
+ self.duration = round(self.pts_last - self.pts_start, 6)
scte35/segmentation.py ADDED
@@ -0,0 +1,71 @@
1
+ """
2
+ segmentation.py
3
+
4
+ SCTE35 Segmentation Descriptor tables.
5
+ """
6
+
7
+ table20 = {
8
+ 0x00: "Restrict Group 0",
9
+ 0x01: "Restrict Group 1",
10
+ 0x02: "Restrict Group 2",
11
+ 0x03: "No Restrictions",
12
+ }
13
+
14
+
15
+ table22 = {
16
+ 0x00: "Not Indicated",
17
+ 0x01: "Content Identification",
18
+ 0x02: "Call Ad Server", # Addressable tv gonzo
19
+ 0x10: "Program Start",
20
+ 0x11: "Program End",
21
+ 0x12: "Program Early Termination",
22
+ 0x13: "Program Breakaway",
23
+ 0x14: "Program Resumption",
24
+ 0x15: "Program Runover Planned",
25
+ 0x16: "Program RunoverUnplanned",
26
+ 0x17: "Program Overlap Start",
27
+ 0x18: "Program Blackout Override",
28
+ 0x19: "Program Start ??? In Progress",
29
+ 0x20: "Chapter Start",
30
+ 0x21: "Chapter End",
31
+ 0x22: "Break Start",
32
+ 0x23: "Break End",
33
+ 0x30: "Provider Advertisement Start",
34
+ 0x31: "Provider Advertisement End",
35
+ 0x32: "Distributor Advertisement Start",
36
+ 0x33: "Distributor Advertisement End",
37
+ 0x34: "Provider Placement Opportunity Start",
38
+ 0x35: "Provider Placement Opportunity End",
39
+ 0x36: "Distributor Placement Opportunity Start",
40
+ 0x37: "Distributor Placement Opportunity End",
41
+ 0x38: "Provider Overlay Placement Opportunity Start",
42
+ 0x39: "Provider Overlay Placement Opportunity End",
43
+ 0x3A: "Distributor Overlay Placement Opportunity Start",
44
+ 0x3B: "Distributor Overlay Placement Opportunity End",
45
+ 0x3C: "Provider Promo Start",
46
+ 0x3D: "Provider Promo End",
47
+ 0x3E: "Distributor Promo Start",
48
+ 0x3F: "Distributor Promo End",
49
+ 0x40: "Unscheduled Event Start",
50
+ 0x41: "Unscheduled Event End",
51
+ 0x42: "Alternate Content Opportunity Start",
52
+ 0x43: "Alternate Content Opportunity End",
53
+ 0x44: "Provider Ad Block Start",
54
+ 0x45: "Provider Ad Block End",
55
+ 0x46: "Distributor Ad Block Start",
56
+ 0x47: "Distributor Ad Block End",
57
+ 0x50: "Network Start",
58
+ 0x51: "Network End",
59
+ }
60
+
61
+
62
+ """
63
+ DVB Equivalent Segmentation Type
64
+ """
65
+ dvb_table2 = {
66
+ 0x0: "No Equivalent",
67
+ 0x1: "Distributor Placement Opportunity",
68
+ 0x2: "Provider Placement Opportunity",
69
+ 0x3: "Distributor Advertisement",
70
+ 0x4: "Provider Advertisement",
71
+ }
scte35/sixfix.py ADDED
@@ -0,0 +1,194 @@
1
+ """
2
+ sixfix.py
3
+ """
4
+
5
+ import io
6
+ import sys
7
+ from scte35.crc import crc32
8
+ from scte35.bitn import NBin
9
+ from scte35.spare import print2
10
+ from scte35.stream import Stream
11
+
12
+ fixme = []
13
+
14
+
15
+ def passed(cue):
16
+ """
17
+ passed a no-op function
18
+ """
19
+ global fixme
20
+ fixme.append(int(cue.packet_data.pid, base=16))
21
+ return cue
22
+
23
+
24
+ class PreFix(Stream):
25
+ """
26
+ PreFix is used to gather 06 Bin data pids with SCTE-35.
27
+ """
28
+
29
+ def decode(self, func=passed):
30
+ super().decode(func=passed)
31
+ global fixme
32
+ fixme = list(set(fixme))
33
+ print("fixing these pids", fixme)
34
+ return fixme
35
+
36
+
37
+ class SixFix(Stream):
38
+ """
39
+ SixFix class
40
+ fixes bin data streams with SCTE-35 to 0x86 SCTE-35 streams
41
+ """
42
+
43
+ CUEI_DESCRIPTOR = b"\x05\x04CUEI"
44
+
45
+ def __init__(self, tsdata=None):
46
+ super().__init__(tsdata)
47
+ self.pmt_payload = None
48
+ self.con_pids = set()
49
+ self.out_file = "sixfixed-" + tsdata.rsplit("/")[-1]
50
+ self.in_file = sys.stdin.buffer
51
+
52
+ def _parse_by_pid(self, pkt, pid):
53
+ if pid in self.pids.tables:
54
+ self._parse_tables(pkt, pid)
55
+ if pid in self.pids.pmt:
56
+ if self.pmt_payload:
57
+ pkt = pkt[:4] + self.pmt_payload
58
+ return pkt
59
+
60
+ def convert_pids(self):
61
+ """
62
+ convert_pids
63
+ changes the stream type to 0x86 and replaces
64
+ the existing PMT as it writes packets to the outfile
65
+ """
66
+ active = io.BytesIO()
67
+ pkt_count = 0
68
+ chunk_size = 2048
69
+ if isinstance(self.out_file, str):
70
+ self.out_file = open(self.out_file, "wb")
71
+ with self.out_file as out_file:
72
+ for pkt in self.iter_pkts():
73
+ pid = self._parse_pid(pkt[1], pkt[2])
74
+ pkt = self._parse_by_pid(pkt, pid)
75
+ active.write(pkt)
76
+ pkt_count = (pkt_count + 1) % chunk_size
77
+ if not pkt_count:
78
+ out_file.write(active.getbuffer())
79
+ active = io.BytesIO()
80
+
81
+ def _regen_pmt(self, n_seclen, pcr_pid, n_proginfolen, n_info_bites, n_streams):
82
+ nbin = NBin()
83
+ nbin.add_int(2, 8) # 0x02
84
+ nbin.add_int(1, 1) # section Syntax indicator
85
+ nbin.add_int(0, 1) # 0
86
+ nbin.add_int(3, 2) # reserved
87
+ nbin.add_int(n_seclen, 12) # section length
88
+ nbin.add_int(1, 16) # program number
89
+ nbin.add_int(3, 2) # reserved
90
+ nbin.add_int(0, 5) # version
91
+ nbin.add_int(1, 1) # current_next_indicator
92
+ nbin.add_int(0, 8) # section number
93
+ nbin.add_int(0, 8) # last section number
94
+ nbin.add_int(7, 3) # res
95
+ nbin.add_int(pcr_pid, 13)
96
+ nbin.add_int(15, 4) # res
97
+ nbin.add_int(n_proginfolen, 12)
98
+ nbin.add_bites(n_info_bites)
99
+ nbin.add_bites(n_streams)
100
+ a_crc = crc32(nbin.bites)
101
+ nbin.add_int(a_crc, 32)
102
+ n_payload = nbin.bites
103
+ pad = 187 - (len(n_payload) + 4)
104
+ pointer_field = b"\x00"
105
+ if pad > 0:
106
+ n_payload = pointer_field + n_payload + (b"\xff" * pad)
107
+ self.pmt_payload = n_payload
108
+
109
+ def _parse_pmt(self, pay, pid):
110
+ """
111
+ parse program maps for streams
112
+ """
113
+ pay = self._chk_partial(pay, pid, self._PMT_TID)
114
+ if not pay:
115
+ return False
116
+ seclen = self._parse_length(pay[1], pay[2])
117
+ n_seclen = seclen + 6
118
+ if self._section_incomplete(pay, pid, seclen):
119
+ return False
120
+ program_number = self._parse_program(pay[3], pay[4])
121
+ pcr_pid = self._parse_pid(pay[8], pay[9])
122
+ self.pids.pcr.add(pcr_pid)
123
+ self.maps.pid_prgm[pcr_pid] = program_number
124
+ proginfolen = self._parse_length(pay[10], pay[11])
125
+ idx = 12
126
+ n_proginfolen = proginfolen + len(self.CUEI_DESCRIPTOR)
127
+ end = idx + proginfolen
128
+ info_bites = pay[idx:end]
129
+ n_info_bites = info_bites + self.CUEI_DESCRIPTOR
130
+ while idx < end:
131
+ # d_type = pay[idx]
132
+ idx += 1
133
+ d_len = pay[idx]
134
+ idx += 1
135
+ # d_bytes = pay[idx - 2 : idx + d_len]
136
+ idx += d_len
137
+ si_len = seclen - 9
138
+ si_len -= proginfolen
139
+ n_streams = self._parse_program_streams(si_len, pay, idx, program_number)
140
+ self._regen_pmt(n_seclen, pcr_pid, n_proginfolen, n_info_bites, n_streams)
141
+ return True
142
+
143
+ def _parse_program_streams(self, si_len, pay, idx, program_number):
144
+ """
145
+ parse the elementary streams
146
+ from a program
147
+ """
148
+ chunk_size = 5
149
+ end_idx = (idx + si_len) - 4
150
+ start = idx
151
+ while idx < end_idx:
152
+ pay, stream_type, pid, ei_len = self._parse_stream_type(pay, idx)
153
+ idx += chunk_size
154
+ idx += ei_len
155
+ self.maps.pid_prgm[pid] = program_number
156
+ self._set_scte35_pids(pid, stream_type)
157
+ streams = pay[start:end_idx]
158
+ return streams
159
+
160
+ def _parse_stream_type(self, pay, idx):
161
+ """
162
+ extract stream pid and type
163
+ """
164
+ npay = pay
165
+ stream_type = pay[idx]
166
+ el_pid = self._parse_pid(pay[idx + 1], pay[idx + 2])
167
+ if el_pid in self.con_pids:
168
+ if stream_type == 6:
169
+ npay = pay[:idx] + b"\x86" + pay[idx + 1 :]
170
+ ei_len = self._parse_length(pay[idx + 3], pay[idx + 4])
171
+ return npay, stream_type, el_pid, ei_len
172
+
173
+
174
+ def sixfix(arg):
175
+ """
176
+ sixfix converts 0x6 bin data mpegts streams
177
+ that contain SCTE-35 data to stream type 0x86
178
+ """
179
+ s1 = PreFix(arg)
180
+ sixed = s1.decode(func=passed)
181
+ global fixme
182
+ fixme = []
183
+ if not sixed:
184
+ print2("No bin data SCTE-35 streams were found.")
185
+ return
186
+ s2 = SixFix(arg)
187
+ s2.con_pids = sixed
188
+ s2.convert_pids()
189
+ print2(f'Wrote: sixfixed-{arg.rsplit("/")[-1]}')
190
+ return
191
+
192
+
193
+ if __name__ == "__main__":
194
+ sixfix(sys.argv[1])
scte35/spare.py ADDED
@@ -0,0 +1,12 @@
1
+ """
2
+ stuff.py functions and such common to scte35.
3
+ """
4
+
5
+ from sys import stderr
6
+
7
+
8
+ def print2(gonzo=b""):
9
+ """
10
+ print2 prints to 2 aka stderr.
11
+ """
12
+ print(gonzo, file=stderr, flush=True)