Skip to content

Commit

Permalink
Update cue2vtt.py
Browse files Browse the repository at this point in the history
  • Loading branch information
futzu authored Nov 25, 2024
1 parent 7c9fa8b commit 7d7ed75
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions examples/stream/cue2vtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@
"""

import sys
from threefive import Stream
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)
Expand All @@ -28,17 +36,19 @@ def scte35_to_vtt(cue):
"""
scte35_to_vtt prints splice insert cue out and cue in via webvtt
"""
start = 0
end = 0
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"):
start = pts_time = cue.command.pts_time + cue.info_section.pts_adjustment
if pts_time > now:
time.sleep(pts_time - now)
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:
Expand All @@ -48,16 +58,20 @@ def scte35_to_vtt(cue):
upid = d.segmentation_upid
if d.has("segmentation_message"):
seg_mesg = d.segmentation_message
if start and duration:
end = start + duration
if end == 0:
end = start + 4
if cue_start:
cue_end = cue_start + 2
## if cue_end == 0:
## end = start + 4

print(f"{ts_to_vtt(start)} --> {ts_to_vtt(end)} ")
print(f"{ts_to_vtt(cue_start)} --> {ts_to_vtt(cue_end)} ")

print(f"Cmd: {cue.command.name} ")
if pts_time:
print(f"PTS: {pts_time} ")
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:
Expand All @@ -69,6 +83,7 @@ def scte35_to_vtt(cue):

if __name__ == "__main__":
arg = sys.argv[1]
first()
print("WEBVTT\n\n\n")
strm = Stream(arg)
strm.decode(func=scte35_to_vtt)

0 comments on commit 7d7ed75

Please sign in to comment.