diff --git a/examples/stream/cue2vtt.py b/examples/stream/cue2vtt.py index da232fbf..ab109805 100644 --- a/examples/stream/cue2vtt.py +++ b/examples/stream/cue2vtt.py @@ -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) @@ -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: @@ -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: @@ -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)