forked from Cleric-K/BlackboxToGPMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps.py
62 lines (47 loc) · 1.61 KB
/
fps.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
import mp4
import struct
def get_fps(f):
f.seek(0)
mp4.find_atom(f, b'moov')
moov = mp4.parse_atom(f)
for trak in moov.find(b'trak'):
if trak.find(b'vmhd'):
video_trak = trak
break
else:
raise Exception('FPS: No video track found')
stts = video_trak.find(b'stts')[0]
mdhd = video_trak.find(b'mdhd')[0]
stts_entries = struct.unpack('>I', stts.data[4:4+4])[0]
sum_samples = 0
sum_delta = 0
for i in range(stts_entries):
offset = 8 + i*8
num_samples = struct.unpack('>I', stts.data[offset:offset+4])[0]
sum_samples += num_samples
sum_delta += num_samples * struct.unpack('>I', stts.data[offset+4:offset+8])[0]
denom = round(sum_delta / sum_samples)
numer = struct.unpack('>I', mdhd.data[12:12+4])[0]
return numer, denom
def set_fps(f, numer, denom):
f.seek(0)
mp4.find_atom(f, b'moov')
moov_offset = f.tell()
moov = mp4.parse_atom(f)
for trak in moov.find(b'trak'):
if trak.find(b'vmhd'):
video_trak = trak
break
else:
raise Exception('FPS: No video track found')
stts = video_trak.find(b'stts')[0]
mdhd = video_trak.find(b'mdhd')[0]
stts_entries = struct.unpack('>I', stts.data[4:4+4])[0]
num_samples = 0
for i in range(stts_entries):
offset = 8 + i*8
num_samples += struct.unpack('>I', stts.data[offset:offset+4])[0]
stts.data[offset+4:offset+8] = struct.pack('>I', denom)
mdhd.data[12:12+8] = struct.pack('>II', numer, num_samples*denom)
f.seek(moov_offset)
f.write(moov.flatten())