Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ playlist.dump('playlist.m3u8')
- [\#EXT-X-DATERANGE](https://tools.ietf.org/html/rfc8216#section-4.3.2.7)
- [\#EXT-X-GAP](https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-05#section-4.4.2.7)
- [\#EXT-X-CONTENT-STEERING](https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-10#section-4.4.6.64)
- [\#EXT-X-DEFINE](https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis#section-4.4.2.3)

# Frequently Asked Questions

Expand Down
11 changes: 11 additions & 0 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,17 @@ def dumps(self, timespec="milliseconds", infspec="auto"):
You could also use unicode(<this obj>) or str(<this obj>)
"""
output = ["#EXTM3U"]
if hasattr(self, "variables_defined"):
for name, value in self.variables_defined.items():
output.append(f'#EXT-X-DEFINE:NAME="{name}",VALUE="{value}"')

if hasattr(self, "variables_imported"):
for name, import_value in self.variables_imported.items():
result = f'#EXT-X-DEFINE: IMPORT="{import_value}"'
if name:
result += f',NAME="{name}"'
output.append(result)

if self.content_steering:
output.append(str(self.content_steering))
if self.media_sequence:
Expand Down
30 changes: 30 additions & 0 deletions m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def parse(content, strict=False, custom_tags_parser=None):
"session_data": [],
"session_keys": [],
"segment_map": [],
"variables_defined": [],
"variables_imported": [],
}

state = {
Expand Down Expand Up @@ -170,6 +172,9 @@ def parse(content, strict=False, custom_tags_parser=None):
elif line.startswith(protocol.ext_is_independent_segments):
_parse_is_independent_segments(**parse_kwargs)

elif line.startswith(protocol.ext_x_define):
_parse_ext_x_define(**parse_kwargs)

elif line.startswith(protocol.ext_x_endlist):
_parse_endlist(**parse_kwargs)

Expand Down Expand Up @@ -247,6 +252,31 @@ def parse(content, strict=False, custom_tags_parser=None):
return data


def _parse_ext_x_define(line, data, **kwargs):
attribute_parser = remove_quotes_parser(
"name",
"value",
"import",
)
attributes = _parse_attribute_list(protocol.ext_x_define, line, attribute_parser)
name = attributes.get("name")
value = attributes.get("value")
import_attr = attributes.get("import")

if name and not value and not import_attr:
raise ParseError(kwargs["lineno"], "EXT-X-DEFINE tag must have VALUE if NAME is specified")

if data["is_variant"] and import_attr:
raise ParseError(kwargs["lineno"], "EXT-X-DEFINE tag with IMPORT attribute is not allowed in variant playlists")

if value and import_attr:
raise ParseError(kwargs["lineno"], "EXT-X-DEFINE tag cannot have both VALUE and IMPORT attributes")

if import_attr:
data["variables_imported"].append(import_attr)
else:
data["variables_defined"].append({"name": name, "value": value})

def _parse_key(line, data, state, **kwargs):
params = ATTRIBUTELISTPATTERN.split(line.replace(protocol.ext_x_key + ":", ""))[
1::2
Expand Down
1 change: 1 addition & 0 deletions m3u8/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ext_x_cue_span = "#EXT-X-CUE-SPAN"
ext_oatcls_scte35 = "#EXT-OATCLS-SCTE35"
ext_is_independent_segments = "#EXT-X-INDEPENDENT-SEGMENTS"
ext_x_define = "#EXT-X-DEFINE"
ext_x_map = "#EXT-X-MAP"
ext_x_start = "#EXT-X-START"
ext_x_server_control = "#EXT-X-SERVER-CONTROL"
Expand Down
10 changes: 10 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,16 @@
#EXT-X-SESSION-DATA:DATA-ID="com.example.title",URI="title.json"
"""

MULTIPLE_DEFINE_NAME_AND_VALUE = """#EXTM3U
#EXT-X-DEFINE:NAME="VAR1",VALUE="Value1"
#EXT-X-DEFINE:NAME="VAR2",VALUE="Value2"
"""

MULTIPLE_DEFINE_IMPORT = """#EXTM3U
#EXT-X-DEFINE:IMPORT="token"
#EXT-X-DEFINE:IMPORT="key"
"""

VERSION_PLAYLIST = """#EXTM3U
#EXT-X-VERSION:4
"""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,23 @@ def test_should_parse_multiple_session_data():
assert data["session_data"][3]["data_id"] == "com.example.title"
assert data["session_data"][3]["uri"] == "title.json"

def test_should_parse_multiple_define_name_and_value():

data = m3u8.parse(playlists.MULTIPLE_DEFINE_NAME_AND_VALUE)

assert data["variables_defined"][0]["name"] == "VAR1"
assert data["variables_defined"][0]["value"] == "Value1"

assert data["variables_defined"][1]["name"] == "VAR2"
assert data["variables_defined"][1]["value"] == "Value2"

def test_should_parse_multiple_define_import():

data = m3u8.parse(playlists.MULTIPLE_DEFINE_IMPORT)

assert data["variables_imported"][0] == "token"
assert data["variables_imported"][1] == "key"


def test_simple_playlist_with_discontinuity_sequence():
data = m3u8.parse(playlists.SIMPLE_PLAYLIST_WITH_DISCONTINUITY_SEQUENCE)
Expand Down