Live m3u8播放3个文件自动停止问题
1.问题描述
最近做一个转码切片播放测试,使用HLS(HTTP Live Streaming)
来做直播, 每个TS分片时间为10s,根据TS分片文件生成以下live m3u8
文件
#EXTM3U #EXT-X-MEDIA-SEQUENCE:1 #EXT-X-TARGETDURATION:10 #EXTINF:10 hls/1.ts #EXTINF:10 hls/2.ts #EXTINF:10 hls/3.ts #EXTINF:10 hls/4.ts #EXTINF:10 hls/5.ts #EXTINF:10 hls/6.ts #EXTINF:10 hls/7.ts #EXTINF:10 hls/8.ts #EXTINF:10 hls/9.ts #EXTINF:10 hls/10.ts
将上述m3u8
文件保存为live.m3u8
,放到Apache
文档目录下,用VLC
播放以下网址:
http://localhost/live.m3u8
测试发现,开始播放的第一个文件不是1.ts,多次测试后发现:
只要列表中的文件超过三个,播放的总是列表中的最后三个文件
2.问题解决
很悲惨的是,上网搜索后,没有找到任何有效的信息,有个哥们遇到同样的情况,解决后有没有将经验分享出来。
向一位同事请教后,同事说,这是有可能的,因为live m3u8
文件列表是需要实时更新的,我们做测试的话,可以先在最后面加上#EXT-X-ENDLIST
,这个方法经测试有效,但这样已经不是live m3u8
模式
再次上网搜索后,确认,终于找到一篇live m3u8
说明
Live Playlist (Sliding Window)
For live sessions, the index file is updated by removing media URIs from the file as new media files are created and made available.
Important: The
EXT-X-ENDLIST
tag is not present in the Live playlist, indicating that new media files will be added to the index file as they become available.See Listing 3 for an example live playlist as it would appear at the beginning of a session.
Listing 3 Live Playlist at the beginning of a session.
#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:1 #EXTINF:10, fileSequence1.ts #EXTINF:10, fileSequence2.ts #EXTINF:10, fileSequence3.ts #EXTINF:10, fileSequence4.ts #EXTINF:10, fileSequence5.ts
The
EXT-X-MEDIA-SEQUENCE
tag value MUST be incremented by 1 for every media URI that is removed from the playlist file. Media URIs must be removed from the playlist file in the order that they appear in the playlist. The updated index file presents a moving window into a continuous stream. This type of session is suitable for continuous broadcasts.Here's the same playlist after it has been updated with new media URIs:
Listing 4 Live Playlist after updating the media URIs.
#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:2 #EXTINF:10, fileSequence2.ts #EXTINF:10, fileSequence3.ts #EXTINF:10, fileSequence4.ts #EXTINF:10, fileSequence5.ts #EXTINF:10, fileSequence6.ts
3.live m3u8
更新规则
live m3u8
文件列表需要不断更新,更新规则:
- 移除一个文件播放列表中靠前的(认为已播放的)文件
- 不断更新
EXT-X-MEDIA-SEQUENCE
标签,以步长为1进行递增
4.实验
写了一个生成live m3u8的小程序,进行测试
Usage:
m3u8_gen.exe start_num list_count duration filename.m3u8 [prefix]
使用示例:
m3u8_gen.exe 1 3 10 live.m3u8 hls/
生成live.m3u8
文件为
#EXTM3U
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-TARGETDURATION:10
#EXTINF:10
hls/1.ts
#EXTINF:10
hls/2.ts
#EXTINF:10
hls/3.ts
写一个BAT脚本,每10s
循环更新live.m3u8
文件
@echo off for /l %%i in (1,1,420) do ( echo m3u8_gen.exe %%i 3 10 live.m3u8 hls/ m3u8_gen.exe %%i 3 10 live.m3u8 hls/ call :SLEEP 10000 ) ::延时函数实现 :SLEEP ping 192.168.11.1 -n 1 -w %1 > nul
经VLC播放测试正常!!