Skip to content

Commit

Permalink
Parse Bitrate value as integer not string
Browse files Browse the repository at this point in the history
  • Loading branch information
davemevans committed Aug 7, 2024
1 parent f9360bd commit f261ae4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def dumps(self, last_segment, timespec="milliseconds", infspec="auto"):
output.append("#EXT-X-BYTERANGE:%s\n" % self.byterange)

if self.bitrate:
output.append("#EXT-X-BITRATE:%s\n" % self.bitrate)
output.append("#EXT-X-BITRATE:%d\n" % self.bitrate)

if self.gap_tag:
output.append("#EXT-X-GAP\n")
Expand Down
6 changes: 4 additions & 2 deletions m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,12 @@ def _parse_variant_playlist(line, data, state, **kwargs):
state["expect_playlist"] = False


def _parse_bitrate(line, state, **kwargs):
def _parse_bitrate(state, **kwargs):
if "segment" not in state:
state["segment"] = {}
state["segment"]["bitrate"] = line.replace(protocol.ext_x_bitrate + ":", "")
state["segment"]["bitrate"] = _parse_simple_parameter(
cast_to=int, **kwargs
)


def _parse_byterange(line, state, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,13 @@ def test_low_latency_output():
assert actual == expected


def test_bitrate_settable_as_int():
obj = m3u8.loads(playlists.BITRATE_PLAYLIST)
obj.segments[0].bitrate = 9876

assert "#EXT-X-BITRATE:9876" in obj.dumps().strip()


# custom asserts


Expand Down
4 changes: 2 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ def test_delta_playlist_daterange_skipping():

def test_bitrate():
data = m3u8.parse(playlists.BITRATE_PLAYLIST)
assert data["segments"][0]["bitrate"] == "1674"
assert data["segments"][1]["bitrate"] == "1625"
assert data["segments"][0]["bitrate"] == 1674
assert data["segments"][1]["bitrate"] == 1625


def test_content_steering():
Expand Down

0 comments on commit f261ae4

Please sign in to comment.