Closed Trungdo253 closed 2 months ago
These playlists are similar enough that I think you could just combine the text contents, but strip out the first three tags from the second one to avoid duplicating them.
Here is some code to do that; it doesn't assume the number of tags to skip is 3.
import m3u8
# Read in the playlists
# playlist_1_text = ...
# playlist_2_text = ...
# Split them into lines
playlist_1_lines = playlist_1_text.splitlines()
playlist_2_lines = playlist_2_text.splitlines()
# Take the lines from playlist 1, then add in any non-duplicates from playlist 2
line_set = set(playlist_1_lines)
combined_lines = playlist_1_lines + [x for x in playlist_2_lines if x not in line_set]
playlist_combined_text = '\n'.join(combined_lines)
# Parse the combined lines to put the media tags back at the top
combined_playlist = m3u8.loads(playlist_combined_text)
combined_playlist_text = combined_playlist.dumps()
print(combined_playlist_text)
Thank you @bbayles
@Trungdo253 does the bbayle's snippet solve your problem?
Closing.
Moved @bbayles snippet to FAQ https://github.com/globocom/m3u8/wiki/FAQ#how-to-merge-multiple-playlists
file 1.m3u8
EXTM3U
EXT-X-VERSION:7
EXT-X-INDEPENDENT-SEGMENTS
EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="1",NAME="none",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="chunklist_5_audio_LQjVYCeD_llhls.m3u8"
EXT-X-STREAM-INF:BANDWIDTH=3628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="1"
chunklist_2_video_LQjVYCeD_llhls.m3u8
EXT-X-STREAM-INF:BANDWIDTH=628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="1"
chunklist_1_video_LQjVYCeD_llhls.m3u8
EXT-X-STREAM-INF:BANDWIDTH=1628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="1"
chunklist_0_video_LQjVYCeD_llhls.m3u8 file 2.m3u8
EXTM3U
EXT-X-VERSION:7
EXT-X-INDEPENDENT-SEGMENTS
EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="5",NAME="none",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="chunklist_5_audio_lHv4VsCh_llhls.m3u8"
EXT-X-STREAM-INF:BANDWIDTH=3628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="5"
chunklist_2_video_lHv4VsCh_llhls.m3u8
EXT-X-STREAM-INF:BANDWIDTH=628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="5"
chunklist_1_video_lHv4VsCh_llhls.m3u8
EXT-X-STREAM-INF:BANDWIDTH=1628000,RESOLUTION=1280x720,FRAME-RATE=30.0,CODECS="avc1.42c01f,mp4a.40.2",AUDIO="5"
chunklist_0_video_lHv4VsCh_llhls.m3u8
i want append mutiple m3u8 to one file m3u8 thank you very much! sory my english so bad