Live m3u8播放3個文件自動停止問題


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文件列表需要不斷更新,更新規則:

  1. 移除一個文件播放列表中靠前的(認為已播放的)文件
  2. 不斷更新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播放測試正常!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM