Search code examples
iphonemedia-playerplaylistm3u

Playlist file(m3u8) give another playlist file infinitely not giving any media file


I have a playlist file which give me another playlist file which also give me another playlist file, continously.
How can I play this playlist file? and Where can I found the source of video?

For example, I have a playlist file. That is

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=355670
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950

If I access to http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950, it give me other playlist files.

#EXTM3U
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:894
#EXTINF:11,
media_894.ts?wowzasessionid=195968950
#EXTINF:10,
media_895.ts?wowzasessionid=195968950
#EXTINF:11,
media_896.ts?wowzasessionid=195968950

If I access to the results, it also give me playlist files.

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=373764
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=1093961187

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=382539
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=1566364859

etc...

Although I can play the url http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950 in safari in my iphone, I want to play this url in my own iphone app.
How can I play m3u8 file extension by myself?


Solution

  • EDIT - May be a duplicate of this SO question but if you want to persist the file, you'll need to go the hard way listed below.

    Easy way = Embed UIWebView in your app and initialize it with the url of the .m3u8 file. This will open up quicktime which understands m3u8 and it will play it as it downloads.

    Hard way = manually create the request/responses for each part of the m3u8 session and then download each .ts file in turn from the playlist file. As you get each "chunk" of the video, write it to a file or memory, then append the next "chunk" after it. I've done this for a h.264 encoded .m4v file served as a m3u8 and it worked (my code is too ugly to paste) but the pseudocode is:

    1. Fetch the m3u8, saving any cookies sent and any headers that may be important*
    2. Parse the .m3u8 (the first file, with the #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=373764) and pull our the urls.
    3. Fetch the playlist from one of the URLs in the .m3u8 file (being sure to keep any headers/cookies in the request that may be necessary)
    4. Parse the playlist file, saving as much metadata as you need from the top part of the file, construct an absolute URL for each .ts path, then stuff it into an array.
    5. Iterate over the array (again, being mindful of cookies/headers) and fetch the content of each .ts URL into a file.
    6. Play the downloaded file with whichever Media Framework you choose (search SO for how to do this)

    If you just need to play the files, go the easy route. If you need to persist them, you need to do the m3u8 dance.

    *HTTP Scoop is invaluable for seeing how the m3u8 "protocol" works and for making sure you're request/response headers are accurate - http://tuffcode.com/

    *I used vanilla NSURLConnection synchronous calls as my code was just a proof of concept, other network frameworks like AFNetworking will make this a lot easier.