-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetTimeStamps.py
More file actions
executable file
·89 lines (78 loc) · 2.73 KB
/
Copy pathgetTimeStamps.py
File metadata and controls
executable file
·89 lines (78 loc) · 2.73 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
#!/usr/bin/env python
"""
This script allows to get the frametimes from a m3u8 file.
It substracts 1/4 from the framerate. If you insert a keyframe
using the forcekeyframe options ffmpeg sets it to the next frame.
Substracting an offset should help that ffpmeg proccesses this correctly.
"""
import os, sys, re, time, json
import subprocess
import numpy as np
import math
def get_vid_stream_stats(file_name):
command = 'ffprobe -show_entries frame -show_streams ' \
'-print_format json -show_entries frame ' \
'-i {file_name}'.format(file_name=file_name)
#print(command)
proc = subprocess.Popen(command.split(), \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr = proc.communicate()
json_dump = json.loads(stderr[0].decode('utf-8'))
times = []
print(len(json_dump['frames']))
frames = json_dump['frames']
for frame in frames:
frame_type = frame['pict_type']
if frame_type == 'I':
times.append(float(frame['best_effort_timestamp_time']))
return times, frames
def get_timeline(input_file):
timeline_I,frames = get_vid_stream_stats(input_file)
timeline = [0]
count = 1
with open(input_file,'r') as m3u8:
for line in m3u8:
if line.startswith('#EXTINF:'):
duration = float(line.replace('#EXTINF:','').split(',')[0])
stamp = timeline[count-1] + duration
idx = (np.abs(np.array(timeline_I) - stamp)).argmin()
timeline.append(timeline_I[idx])
count += 1
return timeline[:-1] # remove last element
def process_timeline(timeline, frame_sub_offset):
out = ''
i = 0
for t in timeline:
if (t-frame_sub_offset) < 0:
stamp = 0
else:
stamp = t-frame_sub_offset
if i == 0:
out += str(truncate(stamp,5))
else:
out += ',' + str(truncate(stamp,5))
i += 1
return out
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
if __name__== "__main__":
if len(sys.argv) < 2:
print('Usage: python getTimeStamps.py [input m3u8] [framerate] [SUB]')
exit()
input_file=str(sys.argv[1])
framerate=float(sys.argv[2])
sub_frame_t = (1/(framerate*4))
timeline = get_timeline(input_file)
if len(sys.argv) > 3:
sub_frame = str2bool(sys.argv[3])
if sub_frame == True:
print(process_timeline(timeline, sub_frame_t))
else:
print(process_timeline(timeline, 0.0))
else:
# just both
print(process_timeline(timeline, sub_frame_t))
print()
print(process_timeline(timeline, 0))