Can AVFoundation be coerced into playing a local .

2019-03-10 03:19发布

问题:

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!

回答1:

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



回答2:

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.