-
Notifications
You must be signed in to change notification settings - Fork 6
/
ts_metadata_reader.py
373 lines (344 loc) · 13.5 KB
/
ts_metadata_reader.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""
TS Metadata reader and parser. It iterates through each TS packet and decodes the header. It also provides a
way to skip to any packet non-linearly, and supports iterating backwards.
It is useful for basic TS metadata analysis scripting, or could be used as the basis of a more complex TS metadata
analysis tool.
Note: Does not decode or de-multiplex a/v.
Also has command line interface to measure the duration of ts files based on pts or dts. Run as main for cli.
Simple Example Usage:
file = "C:/videofiles/tsfile.ts"
ts = TSRead(file)
for x in ts:
print x.decodedpts, x.decodeddts
Note 2: The code is not beautiful, but it works. I plan to pretty it up in the future.
Copyright Joshua Banton
"""
import argparse
import bitstring
import sys
import argparse
import os
class TSRead():
"""
Class to read TS files.
"""
def __init__(self, tsfile):
self.tsfile = tsfile
self.tsopen = open(tsfile, 'rb')
self.pos = -188
self.bytes = self.tsopen.read(188)
self.bits = bitstring.ConstBitStream(bytes=self.bytes, length=1504)
self.packetnum = 0
self.filesize = os.path.getsize(tsfile)
self.totalpackets = self.filesize/188
if not (self.filesize % 188) == 0:
self.complete = False
else:
self.complete = True
def next(self):
"""reads the ts header and returns all the header values"""
self.pos += 188
if self.pos == self.filesize:
raise StopIteration
if not self.pos == self.filesize:
self.tsopen.seek(self.pos)
self.bytes = self.tsopen.read(188)
self.bits = bitstring.ConstBitStream(bytes=self.bytes, length=1504)
self.packetnum += 1
tsdata = self.decodets()
return tsdata
def back(self):
"""reads the ts header and returns all the header values"""
self.pos -= 188
if self.pos == 0:
raise StopIteration
if not self.pos == self.filesize:
self.tsopen.seek(self.pos)
self.bytes = self.tsopen.read(188)
self.bits = bitstring.ConstBitStream(bytes=self.bytes, length=1504)
self.packetnum -= 1
tsdata = self.decodets()
return tsdata
def __iter__(self):
return self
def last(self):
self.pos = self.filesize - 188
self.tsopen.seek(self.pos)
self.bytes = self.tsopen.read(188)
self.bits = bitstring.ConstBitStream(bytes=self.bytes, length=1504)
self.packetnum = self.totalpackets
tsdata = self.decodets()
return tsdata
def first(self):
self.pos = -188
self.packetnum = 0
tsdata = self.next()
return tsdata
def goto(self, packetnum):
self.pos = (packetnum * 188) - 376
self.packetnum = packetnum - 1
tsdata = self.next()
return tsdata
def decodets(self):
adaptation_size = False
av = False
adapt = False
pestype = False
ptspresent = False
dtspresent = False
decodedpts = False
decodeddts = False
pcr = False
opcr = False
discontinuity = False
random = False
espriority = False
pcrpresent = False
opcrpresent = False
splicingpoint = False
transportprivate = False
adaptation_ext = False
packsize = 188
sync = self.bits.read(8).hex
if sync == '0x47':
tei = self.bits.read(1).uint
pusi = self.bits.read(1).uint
transportpri = self.bits.read(1).uint
pid = self.bits.read(13).uint
packet = self.bits.read((packsize-3)*8)
scramblecontrol = packet.read(2).uint
adapt = packet.read(2).uint
concounter = packet.read(4).uint
if adapt == 3:
adaptation_size = packet.read(8).uint
discontinuity = packet.read(1).uint
random = packet.read(1).uint
espriority = packet.read(1).uint
pcrpresent = packet.read(1).uint
opcrpresent = packet.read(1).uint
splicingpoint = packet.read(1).uint
transportprivate = packet.read(1).uint
adaptation_ext = packet.read(1).uint
restofadapt = (adaptation_size+3) - 1
if pcrpresent == 1:
pcr = packet.read(48)
restofadapt -= 6
if opcrpresent == 1:
opcr = packet.read(48)
restofadapt -= 6
packet.pos += (restofadapt-3) * 8
if ((packet.len - packet.pos)/8) > 5:
pesync = packet.read(24).hex
if pesync == ('0x000001'):
pestype = packet.read(8).uint
if pestype > 223 and pestype < 240:
av = 'video'
if pestype < 223 and pestype > 191:
av = 'audio'
packet.pos += (3*8)
ptspresent = packet.read(1).uint
dtspresent = packet.read(1).uint
if ptspresent:
packet.pos += (14)
pts = packet.read(40)
pts.pos = 4
firstpartpts = pts.read(3)
pts.pos += 1
secondpartpts = pts.read(15)
pts.pos += 1
thirdpartpts = pts.read(15)
decodedpts = bitstring.ConstBitArray().join([firstpartpts.bin, secondpartpts.bin, thirdpartpts.bin]).uint
if dtspresent:
dts = packet.read(40)
dts.pos = 4
firstpartdts = dts.read(3)
dts.pos += 1
secondpartdts = dts.read(15)
dts.pos += 1
thirdpartdts = dts.read(15)
decodeddts = bitstring.ConstBitArray().join([firstpartdts.bin, secondpartdts.bin, thirdpartdts.bin]).uint
elif adapt == 2:
#if adapt is 2 the packet is only an adaptation field
adaptation_size = packet.read(8).uint
discontinuity = packet.read(1).uint
random = packet.read(1).uint
espriority = packet.read(1).uint
pcrpresent = packet.read(1).uint
opcrpresent = packet.read(1).uint
splicingpoint = packet.read(1).uint
transportprivate = packet.read(1).uint
adaptation_ext = packet.read(1).uint
restofadapt = (adaptation_size+3) - 1
if pcrpresent == 1:
pcr = packet.read(48)
restofadapt -= 6
if opcrpresent == 1:
opcr = packet.read(48)
restofadapt -= 6
elif adapt == 1:
pesync = packet.read(24).hex
if pesync == ('0x000001'):
pestype = packet.read(8).uint
if pestype > 223 and pestype < 240:
av = 'video'
if pestype < 223 and pestype > 191:
av = 'audio'
packet.pos += 24
ptspresent = packet.read(1).uint
dtspresent = packet.read(1).uint
if ptspresent:
packet.pos += 22
pts = packet.read(32)
firstpart = pts.read(15)
pts.pos += 1
secondpart = pts.read(15)
decodedpts = bitstring.ConstBitArray().join([firstpart.bin, secondpart.bin]).uint
if dtspresent:
dts = packet.read(40)
dts.pos = 4
firstpartdts = dts.read(3)
dts.pos += 1
secondpartdts = dts.read(15)
dts.pos += 1
thirdpartdts = dts.read(15)
decodeddts = bitstring.ConstBitArray().join([firstpartdts.bin, secondpartdts.bin, thirdpartdts.bin]).uint
else:
return 'found badness?', sync.hex
tsobj = TSPacket(sync, tei, transportpri, pusi, pid, scramblecontrol, adapt, concounter, adaptation_size, discontinuity, \
random, espriority, pcrpresent, opcrpresent, splicingpoint, transportprivate, adaptation_ext, pcr, opcr, pestype, \
ptspresent, dtspresent, decodedpts, decodeddts, av)
return tsobj
class TSPacket():
"""
Class to read TS packets.
"""
def __init__(self, sync, tei, transportpri, pusi, pid, scramblecontrol, adapt, concounter, adaptation_size, discontinuity, \
random, espriority, pcrpresent, opcrpresent, splicingpoint, transportprivate, adaptation_ext, pcr, opcr, pestype, \
ptspresent, dtspresent, decodedpts, decodeddts, av):
self.sync = sync
self.transportpri = transportpri
self.pusi = pusi
self.pid = pid
self.scramblecontrol = scramblecontrol
self.adapt = adapt
self.concounter = concounter
self.adaptation_size = adaptation_size
self.discontinuity = discontinuity
self.random = random
self.espriority = espriority
self.pcrpresent = pcrpresent
self.opcrpresent = opcrpresent
self.splicingpoint = splicingpoint
self.transportprivate = transportprivate
self.adaptation_ext = adaptation_ext
self.pcr = pcr
self.opcr = opcr
self.pestype = pestype
self.ptspresent = ptspresent
self.dtspresent = dtspresent
self.decodedpts = decodedpts
self.decodeddts = decodeddts
self.av = av
def get_duration_format(file, format, type):
run = True
ts = TSRead(file)
for x in ts:
if type == 'pts':
data = x
first = data.decodedpts
if x.ptspresent and x.av == format:
break
else:
data = x
first = data.decodeddts
if x.dtspresent and x.av == format:
break
firstdata = ts.last()
if type == 'pts':
if firstdata.ptspresent and firstdata.av == format:
run = False
else:
if firstdata.dtspresent and firstdata.av == format:
run = False
while run:
if type == 'pts':
data = ts.back()
last = data.decodedpts
if data.ptspresent and data.av == format:
break
else:
data = ts.back()
last = data.decodeddts
if data.dtspresent and data.av == format:
break
duration = (last-first)/90000.0
return duration
def get_duration_pid(file, pid, type):
run = True
ts = TSRead(file)
for x in ts:
if type == 'pts':
data = x
first = data.decodedpts
if x.ptspresent and x.pid == int(pid):
break
else:
data = x
first = data.decodeddts
if x.dtspresent and x.pid == int(pid):
break
firstdata = ts.last()
if type == 'pts':
if firstdata.ptspresent and firstdata.pid == int(pid):
run = False
else:
if firstdata.dtspresent and firstdata.pid == int(pid):
run = False
while run:
if type == 'pts':
data = ts.back()
last = data.decodedpts
if data.ptspresent and data.pid == int(pid):
break
else:
data = ts.back()
last = data.decodeddts
if data.dtspresent and data.pid == int(pid):
break
duration = (last-first)/90000.0
return duration
def batchfolder(dir):
os.chdir(dir)
files = os.listdir(os.path.abspath(dir))
ts_files = []
for file in files:
if '.ts' in file:
ts_files.append(os.path.abspath(file))
return ts_files
if __name__ == "__main__":
"""Command line interface to measure the duration of ts files based on pts or dts"""
parser = argparse.ArgumentParser(description='Measures ts durations by pts or dts of the audio or video')
parser.add_argument('-i', '--input', nargs='?', default=None, help=\
"input ts file path to calculate duration of")
parser.add_argument('-b', '--batch', nargs='?', const=True, default=False, help=\
"folder to batch process")
parser.add_argument('-f', '--format', nargs='?', default='video', help=\
"measure video or audio | Note: Stream type must be labeled correctly")
parser.add_argument('-p', '--pid', nargs='?', default=False, help=\
"measure pid")
parser.add_argument('-t', '--type', nargs='?', default='pts', help=\
"measure duration by pts or dts")
args = parser.parse_args()
file = args.input
batch = args.batch
if file:
if not args.pid:
print get_duration_format(file, args.format, args.type)
else:
print get_duration_pid(file, args.pid, args.type)
elif batch:
files = batchfolder(batch)
for x in files:
print get_duration(file, format, type)
else:
print 'need an input file'