globocom / m3u8

Python m3u8 Parser for HTTP Live Streaming (HLS) Transmissions
Other
1.98k stars 464 forks source link

Playlist objects can't be used with add_playlist() #361

Closed Baa14453 closed 2 months ago

Baa14453 commented 2 months ago

Essentially I am building a variant playlist by combining normal playlists:

files = listdir(input_playlists_directory)
m3u8_files = []

# Get a list of playlist files and their full path.
for file in files:
    if file.endswith(".m3u8"):
        m3u8_files.append(input_playlists_directory.rstrip("/\\") + "/" + file)

# Create a new Playlist object
master_playlist = m3u8.model.M3U8()

# Append each playlist to the master playlist
for playlist in m3u8_files:
    # Load the playlist path into a Playlist object.
    playlist = m3u8.load(playlist)

    # Append to the master playlist
    master_playlist.add_playlist(playlist)

This seemed logical to me, but the result produced a text file with object references in it:

#EXTM3U
<m3u8.model.M3U8 object at 0x7a2f5452aaa0>
<m3u8.model.M3U8 object at 0x7a2f5452ab60>
<m3u8.model.M3U8 object at 0x7a2f5452abc0>
<m3u8.model.M3U8 object at 0x7a2f5452ab30>
davemevans commented 2 months ago

load returns M3U8 objects but add_playlist expects Playlist objects.

You could argue this could fail more gracefully, but what you are trying to do isn't correct.

bbayles commented 2 months ago

I don't think this is something you can really do, because the media playlists don't contain the information you need to assemble a multivariant playlist.

Let's take this stream for example.

Its media playlists look like this (shortened for clarity):

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:5849701
#EXTINF:6.000000,
segment_4_20240417_1713385074.ts
#EXTINF:6.000000,
segment_4_20240417_1713385081.ts
#EXTINF:6.000000,
segment_4_20240417_1713385086.ts
#EXTINF:6.000000,
segment_4_20240417_1713385092.ts
#EXTINF:6.000000,
segment_4_20240417_1713385099.ts
#EXTINF:6.000000,
segment_4_20240417_1713385104.ts

Its multivariant playlist looks like this (shortened for clarity):

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=550172,RESOLUTION=256x106
level_0.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=1650064,RESOLUTION=640x266
level_1.m3u8

There's not any information about bandwidth or resolution in the media playlists that would allow you to construct the multivariant playlist. That would need to come from another source.

Baa14453 commented 2 months ago

Ah that's a pain, oh well thank you both :heart: I'll construct the rest of the info manually and call add_playlist based on that.