import subprocess
import json
import time

# HLS input settings
input_playlist = 'live.m3u8'

# HLS output settings
output_playlist = 'live_marked.m3u8'

# SCTE-35 marker settings
marker_time = '00:01:00' # in the format of [HH:MM:SS]
marker_duration = 5 # in seconds

# create command list 
cmd = ['ffmpeg', '-i', input_playlist, '-c:v', 'copy', '-c:a', 'copy', '-f', 'hls', '-hls_time', '10', '-hls_playlist_type', 'event', '-hls_segment_filename', 'live_%03d.ts', '-scte35_format', 'json', '-scte35_time', marker_time, '-scte35_duration', str(marker_duration), output_playlist]

# start ffmpeg process
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# output can be read from process
stdout, stderr = proc.communicate()

# parse json result
scte35_markers = json.loads(stderr.decode())

# Iterate through the list of markers
for marker in scte35_markers:
    # print marker
    print(marker)
