m3u8
m3u8 copied to clipboard
Dump and dumps functions do not show subtitles and audio fields added to stream_info
I want to inject audios and subtitles in m3u8 file. The first step is to add new audios and subtitles as media. The second step would be to add subtitle and audio field in the renditions list.
The initial file looked like this:
#EXTM3U
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-VERSION:5
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=2516370,AVERAGE-BANDWIDTH=2516370,RESOLUTION=1280x720,CODECS="mp4a.40.2,avc1.640020"
video_720p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=1640580,AVERAGE-BANDWIDTH=1640580,RESOLUTION=960x540,CODECS="mp4a.40.2,avc1.640020"
video_540p.m3u8
The modified file should look like this:
#EXTM3U
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-VERSION:5
#EXT-X-MEDIA:URI="audios/Nepali/master.m3u8",TYPE=AUDIO,GROUP-ID="Audio",LANGUAGE="np",NAME="Nepali",DEFAULT=YES,AUTOSELECT=YES
#EXT-X-MEDIA:URI="audios/German/master.m3u8",TYPE=AUDIO,GROUP-ID="Audio",LANGUAGE="de",NAME="German",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-MEDIA:URI="subtitles/German/master.m3u8",TYPE=SUBTITLES,GROUP-ID="Subtitle",LANGUAGE="de",NAME="German",AUTOSELECT=NO,FORCED=NO
#EXT-X-MEDIA:URI="subtitles/Nepali/master.m3u8",TYPE=SUBTITLES,GROUP-ID="Subtitle",LANGUAGE="np",NAME="Nepali",AUTOSELECT=NO,FORCED=NO
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=2516370,AVERAGE-BANDWIDTH=2516370,RESOLUTION=1280x720,CODECS="mp4a.40.2,avc1.640020",AUDIO="Audio",SUBTITLES="Subtitle"
607588853682e1001241042c_720p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=1640580,AVERAGE-BANDWIDTH=1640580,RESOLUTION=960x540,CODECS="mp4a.40.2,avc1.640020",AUDIO="Audio",SUBTITLES="Subtitle"
607588853682e1001241042c_540p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=4755240,AVERAGE-BANDWIDTH=4755240,RESOLUTION=1920x1080,CODECS="mp4a.40.2,avc1.64002a",AUDIO="Audio",SUBTITLES="Subtitle"
607588853682e1001241042c_1080p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=994068,AVERAGE-BANDWIDTH=994068,RESOLUTION=640x360,CODECS="mp4a.40.2,avc1.64001f",AUDIO="Audio",SUBTITLES="Subtitle"
607588853682e1001241042c_360p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=697078,AVERAGE-BANDWIDTH=697078,RESOLUTION=480x270,CODECS="mp4a.40.2,avc1.64001e",AUDIO="Audio",SUBTITLES="Subtitle"
607588853682e1001241042c_270p.m3u8
With the code below the medias get added successfully but the AUDIO and SUBTILES fields do not get added in the lines starting with #EXT-X-STREAM-INF
. The problem lies in the dump
function.'
import m3u8
manifest file = './tmp/manifest.m3u8'
m3u8_obj = None
with open(manifest_file, 'r') as manifest:
content = manifest.read()
m3u8_obj = m3u8.loads(content)
# Add audios media
new_audio = m3u8.Media(uri='audios/Nepali/master.m3u8', type='AUDIO', group_id='Audio', language='np', name='Nepali', default='YES', autoselect='YES')
m3u8_obj.add_media(new_audio)
new_audio = m3u8.Media(uri='audios/German/master.m3u8', type='AUDIO', group_id='Audio', language='de', name='German', default='NO', autoselect='NO')
m3u8_obj.add_media(new_audio)
# Add subtitle media
new_subtitle = m3u8.Media(uri=f'subtitles/Nepali/master.m3u8', type='SUBTITLES', group_id='Subtitle', language='np', name='Nepali', forced='NO', autoselect='NO')
m3u8_obj.add_media(new_subtitle)
new_subtitle = m3u8.Media(uri=f'subtitles/German/master.m3u8', type='SUBTITLES', group_id='Subtitle', language='de', name='German', forced='NO', autoselect='NO')
m3u8_obj.add_media(new_subtitle)
# Trying to add audio and subtitles field in renditions
for playlist in m3u8_obj.playlists:
stream_info = playlist.stream_info
stream_info.audio = 'Audio'
stream_info.subtitles = 'Subtitle'
print( m3u8_obj.dumps())
I request you to fix the dump and the dumps function to add the above fields.
Try this code:
import m3u8
m3u8_text = """
#EXTM3U
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-VERSION:5
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=2516370,AVERAGE-BANDWIDTH=2516370,RESOLUTION=1280x720,CODECS="mp4a.40.2,avc1.640020"
video_720p.m3u8
#EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,BANDWIDTH=1640580,AVERAGE-BANDWIDTH=1640580,RESOLUTION=960x540,CODECS="mp4a.40.2,avc1.640020"
video_540p.m3u8
"""
m3u8_obj = m3u8.loads(m3u8_text)
nepali_audio = m3u8.Media(uri='audios/Nepali/master.m3u8', type='AUDIO', group_id='Audio', language='np', name='Nepali', default='YES', autoselect='YES')
m3u8_obj.add_media(nepali_audio)
german_audio = m3u8.Media(uri='audios/German/master.m3u8', type='AUDIO', group_id='Audio', language='de', name='German', default='NO', autoselect='NO')
m3u8_obj.add_media(german_audio)
nepali_subs = m3u8.Media(uri=f'subtitles/Nepali/master.m3u8', type='SUBTITLES', group_id='Subtitle', language='np', name='Nepali', forced='NO', autoselect='NO')
m3u8_obj.add_media(nepali_subs)
german_subs = m3u8.Media(uri=f'subtitles/German/master.m3u8', type='SUBTITLES', group_id='Subtitle', language='de', name='German', forced='NO', autoselect='NO')
m3u8_obj.add_media(german_subs)
for playlist in m3u8_obj.playlists:
playlist.media += [nepali_audio, german_audio, nepali_subs, german_subs]
print(m3u8_obj.dumps())
I don't think this will do the job. The issue is the missing of the TAGs: AUDIO="Audio",SUBTITLES="Subtitle" inside the EXT-X-STREAM-INF.
#302 should do the job.