11

Clearly, AVFoundation (and Quicktime X) can demux and play properly encoded .ts containers, because .ts containers underly HTTPS live streaming.

Short of setting up a local web service to serve the .m3u8 and associated .ts files, I'd really like to be able to either: convince AVURLAsset and/or URLAssetWithURL to accept a local file .m3u8 URI as if it were an HTTP URI, or better yet, be able to use AVQueuePlayer to load and play a sequence of .ts files without jumping through the live streaming hoops.

The reason I'm wanting to do this is that I need to locally generate movie assets on-the-fly in a somewhat piecemeal fashion - the entire asset won't be available at once but will be generated as time goes by. Obviously this lends itself to an AVQueuePlayer but for various reasons my asset fragments are packaged in .ts containers. All this sounds like it's perfect for "local" live streaming.

I suspect that URLAssetWithURL does some qualification of the string passed to it and then sets some properties to signal that it's looking at a live streaming source which in turn tell AVPlayer/AVQueuePlayer to expect tracks in .ts form. It probably sees the HTTP and decides that this is live streaming.

So my question is: how would one go about "fooling" AVFoundation into handling a local .m3u8 file exactly as it does a remote one?

And the bonus question is: Has anyone (and if so how) been able to make an AVAsset from a .ts file so that the asset will return the status of the asset's tracks (prepare for playback)?

TIA!

The Student
  • 27,520
  • 68
  • 161
  • 264
Tawpie
  • 271
  • 3
  • 11
  • 1
    Same exact problem... I haven't found a solution. Sure would be nice since you can feed this to a MPMoviePlayerController and have Airplay. – Gabe Apr 21 '11 at 21:12
  • Actually, have you found a way to play .ts files without wrapping them in a HTTP live stream? AVAsset and MPMoviePlayerController both fail when I give them local or network .ts files. I can however use local .m3u8s, even .m3u8s that reference other local .m3u8s. – Gabe Apr 21 '11 at 21:31
  • 1
    No joy -- I ended up putting in a tiny HTTP server and that worked fine but haven't found a way to make an asset directly from a .m3u8 or .ts file. We also took a different approach for experimentation and wrote a proprietary (sorry, can't share) demuxer that reduces the .ts to raw h.264 frames which we then decode and present. It works but is fairly resource costly. Next investigation will be to repackage the raw h.264 as a .mp4 and see how expensive that step is. – Tawpie May 05 '11 at 15:27
  • I did get this working with local files without an internet connection. You can spin up a HTTP server on the iPhone and then serve the files to the iPhone via localhost. I slightly modified the iPhone HTTP-server project at http://cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html to serve the ts and playlist files. – Gabe Jul 07 '11 at 18:09
  • "TIA" in the google dictionary is "Transient ischemic attack", what do you mean with this? – The Student Apr 26 '12 at 12:39
  • @Gabe Can you give me a hint how you changed the code to deliver the files. I am trying to build something similar. – dasdom Nov 07 '12 at 09:25
  • You should be able to use CocoaHttpServer without any modification to serve via localhost. – George Newton Nov 09 '12 at 11:36
  • 1
    @TomBrito TIA expands to Thanks in Advance. – vipw Nov 29 '12 at 13:40
  • @Gabe Can you write up an answer on how you used a local HTTP server? I think that is the best answer for this question. – vipw Nov 29 '12 at 13:41
  • @Tawpie what did you use to decode raw h264 frames after demuxing? – jAckOdE Dec 11 '12 at 01:25
  • (very late comment, lots has happened). I did mean thanks in advance, but transient ischemic attack was equally accurate in this situation! @jAckOdE: I would like to, but that company went under and took their code with them. it's been so long I don't even remember what we did... – Tawpie Jun 23 '14 at 15:06

2 Answers2

9

This problem was annoying for us too for a long time. We have finally decided to write a tool to convert a list of ts to an mp4 file. It consists in using TSDemux to demux and concatenate videos/audios and then generate a mp4 file with GPAC.

It does really answer your question, but it may be a way to do what you want to do. This tool is on Github, feel free to try: https://github.com/Keemotion/TS2MP4

smad
  • 1,071
  • 14
  • 29
  • 1
    I want to use the library you wrote but the output mp4 file does not have any sound. There is an open github issue (https://github.com/Keemotion/TS2MP4/issues/6) which describes the problem. Would you care to have a look at it? Much appreciated. – Seyeong Jeong Sep 09 '15 at 03:15
  • No we haven't fixed it yet and do not have any plan to work on it for now. Feel free to work on it and submit a pull request. – smad Sep 09 '15 at 09:48
  • Does this Library still work properly, I cloned the repo and even the podfile needs to be updated to have a proper workspace? Even, I am getting multiple file errors that doesn't exist like " #include – mAc Apr 07 '20 at 07:12
  • It was in 2014, so 6 years ago. This lib is not maintained anymore. – smad Apr 08 '20 at 16:18
0

You can actually create AVURLAssets directly from the underlying ts files, and play these directly, very similarly to the way you would play a mov or mp4 file.

There is some overhead for each ts file, so your best bet is to simply cat the files into one large ts file (based on your m3u8's contents), and play that large file.

There are a couple of gotcha's: AVCompositions constructed using ts file based AVAssets are memory hungry, so avoid AVCompositions apart from small files. You cannot use ts file based AVAssets in a reference movie.

  • My hevc/ac3 ts `AVURLAssets` don't play. I can't wrap them in `AVCompositions` as they have no `tracks`. Maybe it doesn't like ac3. – Rhythmic Fistman Dec 27 '18 at 23:53
  • It doesn't work, tested with h264+aac ts file on iOS 13. The error: "Cannot Open" UserInfo={NSLocalizedFailureReason=This media format is not supported. ... – Troy Mar 23 '20 at 09:13