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
19 changes: 19 additions & 0 deletions m3u8/version_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ def get_version(file_lines: list[str]):

return None

def validate_multiple_version_decl(file_lines: list[str]):
version_number_line_ctr = 0
errors = []

for number, line in enumerate(file_lines):
if line.startswith(protocol.ext_x_version):
version_number_line_ctr = version_number_line_ctr + 1

if version_number_line_ctr > 1:
errors.append(VersionMatchingError(
line_number=number,
line=line,
description="There are multiple version declarations in the file.",
how_to_fix="Remove all extra version declarations."
))
return errors

def valid_in_all_rules(
line_number: int, line: str, version: float
Expand All @@ -30,8 +46,11 @@ def validate(file_lines: list[str]) -> list[VersionMatchingError]:
return []

errors = []

for number, line in enumerate(file_lines):
errors_in_line = valid_in_all_rules(number, line, found_version)
errors.extend(errors_in_line)

errors.extend(validate_multiple_version_decl(file_lines))

return errors
11 changes: 11 additions & 0 deletions tests/invalid_versioned_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@
#EXTINF: 10.0,
https://example.com/segment1.ts
"""

# Should not have multiple version tags
M3U8_RULE_MULTIPLE_VERSIONS = """
#EXTM3U
#EXT-X-VERSION: 3
#EXT-X-BYTERANGE: 200000@1000
#EXT-X-TARGETDURATION: 10
#EXT-X-VERSION: 4
#EXTINF: 10.0,
https://example.com/segment1.ts
"""
5 changes: 5 additions & 0 deletions tests/test_invalid_versioned_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ def test_should_fail_if_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY_and_version_less_
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_BYTE_RANGE, strict=True)

assert "Change the protocol version to 4 or higher." in str(exc_info.value)

def test_should_fail_if_multiple_EXT_X_VERSION_lines_are_found():
with pytest.raises(Exception) as exc_info:
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_MULTIPLE_VERSIONS, strict=True)
assert "There are multiple version declarations in the file." in str(exc_info.value)