8000 Add Segment.media_Sequence by bbayles · Pull Request #310 · globocom/m3u8 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add Segment.media_Sequence #310

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

Merged
merged 3 commits into from
Dec 27, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# You can use PyPy versions in python-version.
# For example, pypy2 and pypy3
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
6 changes: 5 additions & 1 deletion m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def _initialize_attributes(self):
for attr, param in self.simple_attributes:
setattr(self, attr, self.data.get(param))

for i, segment in enumerate(self.segments, self.media_sequence or 0):
Copy link
Contributor

Choose a reason for hiding this comment

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

I learned something new here :) I didn't know enumerate would work with two sources :)

Just out of curiosity, why does it return first the number iterator and secondly the list iterator? (since they were passed in other order)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two tricks here.

First, enumerate has a two argument form: iterable, start_number. For us, self.segments is the iterable, since the SegmentList type can be iterated. We want to start counting from the media sequence number, so we use it as the second argument.

Second, the self.media_sequence or 0 trick. I want to handle the case where media_sequence is None and treat that ias 0, as per the HLS spec. The or operator takes the left argument if it's truthy (None isn't) and the right operator if it's not.

None or 0  # Picks 0
0 or 0  # Picks the second 0
1 or 0  # Picks 1
2 or 0  # Picks 2
# etc

Copy link
Contributor

Choose a reason for hiding this comment

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

cool ty

segment.media_sequence = i

self.files = []
for key in self.keys:
# Avoid None key, it could be the first one, don't repeat them
Expand Down Expand Up @@ -448,7 +451,8 @@ def __init__(self, uri=None, base_uri=None, program_date_time=None, current_prog
cue_out_start=False, cue_in=False, discontinuity=False, key=None, scte35=None,
oatcls_scte35=None, scte35_duration=None, scte35_elapsedtime=None, asset_metadata=None,
keyobject=None, parts=None, init_section=None, dateranges=None, gap_tag=None,
custom_parser_values=None):
media_sequence=None, custom_parser_values=None):
self.media_sequence = media_sequence
self.uri = uri
self.duration = duration
self.title = title
Expand Down
9 changes: 7 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def test_target_duration_attribute():

def test_media_sequence_attribute():
obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST)
mock_parser_data(obj, {'media_sequence': '1234567'})
mock_parser_data(obj, {'media_sequence': 1234567})

assert '1234567' == obj.media_sequence
assert 1234567 == obj.media_sequence


def test_program_date_time_attribute():
Expand Down Expand Up @@ -1430,6 +1430,11 @@ def test_add_content_steering_base_uri_update():
obj.base_uri = "https://yet-another.example.com/"

assert obj.content_steering.absolute_uri == "https://yet-another.example.com/steering?video=00012"

def test_segment_media_sequence():
obj = m3u8.M3U8(playlists.SLIDING_WINDOW_PLAYLIST)
assert [s.media_sequence for s in obj.segments] == [2680, 2681, 2682]

# custom asserts


Expand Down
0