Skip to content

Commit

Permalink
fix: fix m3u8 format generated by HLS proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Aug 23, 2023
1 parent 90c3adc commit b12e370
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions gridplayer/utils/stream_proxy/m3u8.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@
def m3u8_to_str(hls_playlist: M3U8):
res = ["#EXTM3U"]
res += [f"#EXT-X-VERSION:{hls_playlist.version}"]
res += [f"#EXT-X-TARGETDURATION:{hls_playlist.target_duration}"]
if hls_playlist.playlist_type:
res += [f"#EXT-X-PLAYLIST-TYPE:{hls_playlist.playlist_type}"]
res += [f"#EXT-X-MEDIA-SEQUENCE:{hls_playlist.media_sequence}"]
if hls_playlist.media_sequence:
res += [f"#EXT-X-MEDIA-SEQUENCE:{hls_playlist.media_sequence}"]
res += [f"#EXT-X-TARGETDURATION:{hls_playlist.targetduration}"]

# grab only the edge if it's a livestream
if hls_playlist.media_sequence:
segments = hls_playlist.segments[-LIVESTREAM_EDGE:]
else:
segments = hls_playlist.segments

res += sum([_segment_to_str(s) for s in segments], [])
s_map = None
for s in segments:
if s.map is not None and s.map != s_map:
s_map = s.map

res += _segment_to_str(s, add_map=True)
else:
res += _segment_to_str(s)

if hls_playlist.is_endlist:
res += ["#EXT-X-ENDLIST"]

return "\n".join(res)


def _segment_to_str(segment: Segment) -> List[str]:
def _segment_to_str(segment: Segment, add_map=False) -> List[str]:
res = []

if segment.date:
Expand All @@ -38,7 +46,7 @@ def _segment_to_str(segment: Segment) -> List[str]:
res += ["#EXT-X-DISCONTINUITY"]
if segment.byterange:
res += ["#EXT-X-BYTERANGE:{0}".format(_byterange_to_str(segment.byterange))]
if segment.map:
if segment.map and add_map:
res += [
'#EXT-X-MAP:URI="{0}"{1}'.format(
segment.map.uri,
Expand Down

0 comments on commit b12e370

Please sign in to comment.