Repository navigation
Expand file tree
/
Copy pathstreamedit.py
More file actions
199 lines (174 loc) · 5.91 KB
/
Copy pathstreamedit.py
File metadata and controls
199 lines (174 loc) · 5.91 KB
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
"""
streamedit.py
An example of how to edit SCTE35 data
in MPEGTS packets on the fly and pass
the modifiied stream to stdout.
Use like:
pypy3 streamedit.py https://futzu.com/xaa.ts | pypy3 -c 'import threefive; threefive.decode()'
"""
from functools import partial
import sys
from threefive import Stream
class Stream2(Stream):
"""
Stream2 is a subclass of threefive.Stream
to demonstrate editing SCTE35 Cues in a MPEGTS stream.
Use like:
pypy3 streamedit.py myvideo.ts >> myvideoedited.ts
BEFORE:
{
"info_section": {
"table_id": "0xfc",
"section_syntax_indicator": false,
"private": false,
"sap_type": "0x3",
"sap_details": "No Sap Type",
"section_length": 42,
"protocol_version": 0,
"encrypted_packet": false,
"encryption_algorithm": 0,
"pts_adjustment": 0.0,
"cw_index": "0xff",
"tier": "0xfff",
"splice_command_length": 15,
"splice_command_type": 5,
"descriptor_loop_length": 10,
"crc": "0xd7165c79"
},
"command": {
"command_length": 15,
"command_type": 5,
"name": "Splice Insert",
"time_specified_flag": true,
"pts_time": 23683.480033,
"splice_event_id": 5690,
"splice_event_cancel_indicator": false,
"out_of_network_indicator": true,
"program_splice_flag": true,
"duration_flag": false,
"splice_immediate_flag": false,
"unique_program_id": 0,
"avail_num": 0,
"avail_expected": 0
},
"descriptors": [
{
"tag": 0,
"descriptor_length": 8,
"name": "Avail Descriptor",
"identifier": "CUEI",
"provider_avail_id": 0
}
],
"packet_data": {
"pid": "0x40b",
"program": 1030,
"pcr": 23677.003267,
"pts": 23677.030189
}
}
AFTER:
{
"info_section": {
"table_id": "0xfc",
"section_syntax_indicator": false,
"private": false,
"sap_type": "0x3",
"sap_details": "No Sap Type",
"section_length": 42,
"protocol_version": 0,
"encrypted_packet": false,
"encryption_algorithm": 0,
"pts_adjustment": 109.55, # <-- Changed
"cw_index": "0xff",
"tier": "0xfff",
"splice_command_length": 15,
"splice_command_type": 5,
"descriptor_loop_length": 10,
"crc": "0x82fd33d9"
},
"command": {
"command_length": 15,
"command_type": 5,
"name": "Splice Insert",
"time_specified_flag": true,
"pts_time": 23683.480033,
"splice_event_id": 5690,
"splice_event_cancel_indicator": false,
"out_of_network_indicator": true,
"program_splice_flag": true,
"duration_flag": false,
"splice_immediate_flag": false,
"unique_program_id": 999, # <-- Changed
"avail_num": 0,
"avail_expected": 0
},
"descriptors": [
{
"tag": 0,
"descriptor_length": 8,
"name": "Avail Descriptor",
"identifier": "FUEI", # <-- Changed
"provider_avail_id": 0
}
],
"packet_data": {
"pid": "0x40b",
"program": 1030,
"pcr": 23677.003267,
"pts": 23677.030189
}
}
# The Changed cues will be in myvideoedited.ts,
# Check it like this:
from threefive import decode
decode('myvideoedited.ts')
"""
_PAD = b"\xff"
@staticmethod
def edit_cue(cue):
"""
edit_cue sets cue.info_section.pts_adjustment
and then re-encodes the SCTE35 Cue.
"""
print("BEFORE:", file=sys.stderr)
cue.show()
cue.info_section.pts_adjustment = 109.55 # Changed
cue.command.unique_program_id = 999 # Changed
if cue.descriptors:
cue.descriptors[0].identifier = "FUEI" # Changed
cue.encode()
print("AFTER:\n", file=sys.stderr)
cue.show()
def repack_pkt(self, pkt, cue):
"""
repack_pkt recreates the SCTE35 packet
with the edited SCTE35 Cue as the payload
adds padding as needed and then
writes the new_packet to stdout
"""
pay_idx = pkt.index(cue.bites)
header = pkt[:pay_idx]
self.edit_cue(cue)
new_payload = cue.bites
new_pkt = header + new_payload
new_pkt += (self._PACKET_SIZE - len(new_pkt)) * self._PAD
return new_pkt
def edit_scte35(self):
"""
edit_scte35 applies Stream2.edit_cue(cue)
on SCTE35 data before writing all packets
to stdout.
"""
if self._find_start():
for pkt in iter(partial(self._tsdata.read, self._PACKET_SIZE), b""):
cue = self._parse(pkt)
if cue:
pkt = self.repack_pkt(pkt, cue)
sys.stdout.buffer.write(pkt)
self._tsdata.close()
if __name__ == "__main__":
args = sys.argv[1:]
for arg in args:
strm = Stream2(arg)
strm.edit_scte35()