Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #373: Import EXT-X-CUE-OUT and EXT-X-CUE-OUT-CONT handling #374

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 33 additions & 55 deletions m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,14 @@ def _parse_attribute_list(prefix, line, attribute_parser, default_parser=None):

attributes = {}
for param in params:
name, value = param.split("=", 1)
name = normalize_attribute(name)
param_parts = param.split("=", 1)
if len(param_parts) == 1:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change affects all tags - if there's some parameter without a key=value pair, it will get stuck into the empty string key.

name = ""
value = param_parts[0]
else:
name, value = param_parts

name = normalize_attribute(name)
if name in attribute_parser:
value = attribute_parser[name](value)
elif default_parser is not None:
Expand Down Expand Up @@ -548,23 +553,23 @@ def _parse_cueout_cont(line, state, **kwargs):
if len(elements) != 2:
return

# EXT-X-CUE-OUT-CONT:2.436/120 style
res = re.match(
r"^[-+]?([0-9]+(\.[0-9]+)?|\.[0-9]+)/[-+]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$",
elements[1],
)
if res:
state["current_cue_out_elapsedtime"] = res.group(1)
state["current_cue_out_duration"] = res.group(3)
return

# EXT-X-CUE-OUT-CONT:ElapsedTime=10,Duration=60,SCTE35=... style
cue_info = _parse_attribute_list(
protocol.ext_x_cue_out_cont,
line,
remove_quotes_parser("duration", "elapsedtime", "scte35"),
)

# EXT-X-CUE-OUT-CONT:2.436/120 style
progress = cue_info.get("")
if progress:
progress_parts = progress.split("/", 1)
if len(progress_parts) == 1:
state["current_cue_out_duration"] = progress_parts[0]
else:
state["current_cue_out_elapsedtime"] = progress_parts[0]
state["current_cue_out_duration"] = progress_parts[1]

duration = cue_info.get("duration")
if duration:
state["current_cue_out_duration"] = duration
Expand All @@ -578,55 +583,28 @@ def _parse_cueout_cont(line, state, **kwargs):
state["current_cue_out_elapsedtime"] = elapsedtime


def _cueout_no_duration(line):
# this needs to be called first since line.split in all other
# parsers will throw a ValueError if passed just this tag
if line == protocol.ext_x_cue_out:
return (None, None)


def _cueout_envivio(line, state):
param, value = line.split(":", 1)
res = re.match('.*DURATION=(.*),.*,CUE="(.*)"', value)
if res:
return (res.group(2), res.group(1))
else:
return None


def _cueout_duration(line):
# This was added separately rather than modifying "simple"
param, value = line.split(":", 1)
res = re.match(r"DURATION=(.*)", value)
if res:
return (None, res.group(1))


def _cueout_simple(line):
param, value = line.split(":", 1)
res = re.match(r"^(\d+(?:\.\d)?\d*)$", value)
if res:
return (None, res.group(1))


def _parse_cueout(line, state, **kwargs):
_cueout_state = (
_cueout_no_duration(line)
or _cueout_envivio(line, state)
or _cueout_duration(line)
or _cueout_simple(line)
)
if _cueout_state:
cue_out_scte35, cue_out_duration = _cueout_state
current_cue_out_scte35 = state.get("current_cue_out_scte35")
state["current_cue_out_scte35"] = cue_out_scte35 or current_cue_out_scte35
state["current_cue_out_duration"] = cue_out_duration

state["cue_out_start"] = True
state["cue_out"] = True
if "DURATION" in line.upper():
state["cue_out_explicitly_duration"] = True

elements = line.split(":", 1)
if len(elements) != 2:
return

cue_info = _parse_attribute_list(
protocol.ext_x_cue_out,
line,
remove_quotes_parser("cue"),
)
cue_out_scte35 = cue_info.get("cue")
cue_out_duration = cue_info.get("duration") or cue_info.get("")

current_cue_out_scte35 = state.get("current_cue_out_scte35")
state["current_cue_out_scte35"] = cue_out_scte35 or current_cue_out_scte35
state["current_cue_out_duration"] = cue_out_duration


def _parse_server_control(line, data, **kwargs):
attribute_parser = {
Expand Down
25 changes: 25 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,31 @@
segment_19980229.ts
"""

CUE_OUT_MEDIACONVERT_PLAYLIST = """\
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:10,
segment_00001.ts
#EXT-X-CUE-OUT:4,SpliceType=VOD_DAI,Action=REPLACE, PAID=example.com/2024073010700,Acds=BA
#EXTINF:10,
segment_00002.ts
#EXT-X-CUE-OUT-CONT:10/4, SpliceType=VOD_DAI,Action=REPLACE,PAID=example.com/2024073010700,Acds=BA
#EXTINF:10,
segment_00003.ts
#EXTINF:10,
segment_00004.ts
#EXT-X-CUE-IN:4,SpliceType=VOD_DAI
#EXTINF:0,
segment_00005.ts
#EXTINF:10,
segment_00006.ts
#EXT-X-ENDLIST
"""


CUE_OUT_ENVIVIO_PLAYLIST = """
#EXTM3U
#EXT-X-VERSION:3
Expand Down
10 changes: 9 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ def test_segment_cue_out_cont_alt():
assert segments[3].scte35_duration == "120.0"


def test_segment_cue_out_cont_mediaconvert():
obj = m3u8.M3U8(playlists.CUE_OUT_MEDIACONVERT_PLAYLIST)
segments = obj.segments

assert segments[2].scte35_elapsedtime == "10"
assert segments[2].scte35_duration == "4"


def test_segment_envivio_scte35_attribute():
obj = m3u8.M3U8(playlists.CUE_OUT_ENVIVIO_PLAYLIST)
segments = obj.segments
Expand All @@ -273,7 +281,7 @@ def test_segment_envivio_scte35_attribute():
def test_segment_unknown_scte35_attribute():
obj = m3u8.M3U8(playlists.CUE_OUT_INVALID_PLAYLIST)
assert obj.segments[0].scte35 is None
assert obj.segments[0].scte35_duration is None
assert obj.segments[0].scte35_duration == "INVALID"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now no longer assume a particular format for duration, so weird fraction stuff can flow through.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great! Thank you



def test_segment_cue_out_no_duration():
Expand Down
Loading