-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcue2vtt.py
More file actions
89 lines (74 loc) · 2.16 KB
/
cue2vtt.py
File metadata and controls
89 lines (74 loc) · 2.16 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
"""
cue2vtt.py
uses webvtt subtitles to display splice insert cue out and cue In.
Usage:
pypy3 cue2vtt.py thevideo.ts | mplayer thevideo.ts -sub -
"""
import sys
import time
from iframes import IFramer
from threefive import Stream
def first():
global start_pts
iframer = IFramer(shush=True)
start_pts = iframer.first(sys.argv[1])
def ts_to_vtt(timestamp):
"""
ts_to_vtt converts timestamp into webvtt times
"""
timestamp -= start_pts
hours, seconds = divmod(timestamp, 3600)
mins, seconds = divmod(seconds, 60)
seconds = round(seconds, 3)
return f"{int(hours):02}:{int(mins):02}:{seconds:02}"
def scte35_to_vtt(cue):
"""
scte35_to_vtt prints splice insert cue out and cue in via webvtt
"""
cue_start = 0
cue_end = 0
duration = None
pts_time = None
upid = None
seg_mesg = None
now = cue.packet_data.pts
if cue.command.has("pts_time"):
cue_start = cue.command.pts_time + cue.info_section.pts_adjustment
else:
cue_start = now
if cue_start > now:
time.sleep(cue_start - now)
if cue.command.has("break_duration"):
duration = cue.command.break_duration
for d in cue.descriptors:
if d.has("segmentation_duration"):
duration = d.segmentation_duration
if d.has("segmentation_upid"):
upid = d.segmentation_upid
if d.has("segmentation_message"):
seg_mesg = d.segmentation_message
if cue_start:
cue_end = cue_start + 2
## if cue_end == 0:
## end = start + 4
print(f"{ts_to_vtt(cue_start)} --> {ts_to_vtt(cue_end)} ")
print(f"Cmd: {cue.command.name} ")
pts_time = None
if cue.command.pts_time:
pts_time = cue.command.pts_time + cue.info_section.pts_adjustment
else:
pts_time = now
print(f"PTS: {pts_time} ")
if duration:
print(f"Duration: {round(duration,3)} ")
if seg_mesg:
print(seg_mesg)
if upid:
print(f"Upid: {upid}")
print()
if __name__ == "__main__":
arg = sys.argv[1]
first()
print("WEBVTT\n\n\n")
strm = Stream(arg)
strm.decode(func=scte35_to_vtt)