How to play ts file using the cache in the AVPlayer? - ios

I want to cache the HLS in the ts file.
Apple demo site use custom schema for this site.
https://developer.apple.com/library/ios/samplecode/sc1791/Introduction/Intro.html
This sample use custom schema. But it's use redirect for ts file.
I searched "How to play ts file using the cache in the AVPlayer". I found use redirect local proxy. But it method is prohibited to Apple that the move background.
https://developer.apple.com/library/ios/technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841-CH1-SUBSECTION2
Is it possible to start the ts file cached locally on the AVPlayer ?

As far as I know, Its not possible to play ts files directly using iOS Player (AVPlayer).
But there is a way to play it.
Download the .ts file and save it in app document directory.
Use this library Keemotion/TS2MP4 to
convert ts file to mp4 which can be played using AVPlayer.
Now you can either do it discreetly downloading -> convert -> play for each single file.
or You can download multiple -> convert them to one large mp4 file and play it.

Related

AVAggregateAssetDownloadTask seems to load m3u8 file twice

I am using AssetPersistenceManager from this Apple Sample : Using AVFoundation to play and persist http live streams
I create an AVURLAsset with a playlist master url, then call downloadStream(for:) with this asset, and the AVAggregateAssetDownloadTask complete download successfully !
It saves a folder named 286943_chapter_01_C955A55455AC0C4E.movpkg which contains the downloaded segments, my .m3u8 file and a StreamInfoBoot.xml file.
The StreamInfoBook.xml file contains references between local segment (or .frag) file and m3u8 segment URL.
However, those urls are signed and generated by Cloudfront. And some times, some URLs inside the .m3u8 file doesn't match URLs in StreamInfoBook.xml because of Expires parameter of cloudfrount.
So I think maybe AVAggregateAssetDownloadTask is calling the .m3u8 file twice : Once to save the .m3u8 and once to download segments and write StreamInfoBook.xml.
Does anyone already faced this issue ? What could I do to check if my Asset is playable offline or not ?
Do you have any suggestion ?
Here is an exemple of not matching segment URLs
In .m3u8 downloaded file :
https: //ezvoiznbov32f.cloudfront.net/file/265579/audio/chapter_08_10.mp3?Expires=1576159816&Signature=vfAYZ8ivP7yKeiranpba3R24gTSxexX4Nb1VjbDrR917pyFQqi2DMMMf8gi3LcueKXxv0FITprXzLXV62Py0Ph4nMxp8ixf5qG5sYnW1JcO74BxNfJE~DEurN~yQgczNBOK4b~bEXrDDZId1AweYJrgjZtjSoyUwAGXf~LA_&Key-Pair-Id=APKAAFEFNOSQQ34Q
In StreamInfoBook.xml file :
https: //ezvoiznbov32f.cloudfront.net/file/265579/audio/chapter_08_10.mp3?Expires=1576159817&Signature=PZ~tqOeIa3Vm26qKTFYU1LCGfvsWNpkUDMi3eFr~EhyRTLj66CDInSwfpoorYjl4Kl4B9eQ1W8ZzTlIMDGvs36wJ235JOszeov23IuIOzAJTlNw7zQYEd4Y1tq~nyZinpFoyDRGk0CelKOhH91Itb3rpQUeMN6lDhO-cq1X1OM_&Key-Pair-Id=APKAAFEFNOSQQ34Q
I'm testing with iOS 13.2
Well, answering myself :
After a very long time trying to make AVAggregateAssetDownloadTask works in background while the application is killed by the system, I finally switched to AVAssetDownloadTask.
Now everything is working perfectly ! It's like magic !
The difference between aggregate and normal tasks is in media selections. But I don't need it for now....

How to: Playm3u8 files offline in VLC

I'm using an offline terminal to create an .m3u8 file but I would like to play it using VLC. Every example so far has shown examples of using m3u8 online but this isn't an option for me. If you can play it offline using VLC how do you do so?
You have to download the playlist from m3u8 to watch it offline and to do that you have to either use youtube-dl or ffmpeg
i would recommend ffmpeg , and the command for downloading the m3u8 file goes like this :-
ffmpeg -i "here your m3u8 link" -c copy Output.mp4
This will download the highest quality video available in that m3u8 file.
To change quality you can use map command or simply download the m3u8 file and open it with notepad and there you will find links for other resolution.
m3u8 files are just playlists without actual contents, but just URL pointers. Those files are mostly being used these days for HLS.
While you can play the files while offline, you won't see anything unless you are also hosting the referenced Contents.

play audio and video files in Cordova app that reside in iOS Documents directory

I have a Cordova application that I use to be able to link to mp3 and mp4 files relatively using a src like so:
../../Documents/videos/video.mp4
I just now updated my Cordova application to the latest version and these relative URLs don't work so I've been expermenting with other solutions.
It looks like if I use the cordova.file.documentsDirectory (iOS only) variable I can link to them that way but when I save references to these files in the database the GUID of the application changes and the URL is no longer valid when the app is rebuilt and relaunched.
I tried using cdvfile://localhost/persistent/ but this seems to only work for images and not video or audio files using HTML5 audio and video tags for playback.
Ultimately I could save the files with a variable that gets replaced at run-time but this is obviously not the preferred "solution." Something like [documentsfolder]/videos/video.mp4
How can I link to a persistent file location and have it work with images, audio files, and video files?
I would love to use the cdvfile url but have it work with mp3 and mp4 files.
Thank you.
You should be able to access any resource [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:#"www/[your path]"]

Streaming Technique from pocketcast in xcode

I've been asked by my client whether it is possible to download a video and stream it once a bit has downloaded, just like pocketcasts does. His reasoning is this will allow him to store his video files on a site such as godaddy and bypass the need to stream the file to the phone which normally requires a dedicated server.
Is this even possible? if so do you know anywhere I can look to find out how pocketcasts does it? At the moment my app just streams an mp4.
Thanks for looking,
Matt
Since you're targetting iOS, HLS (HTTP Live Streaming) is your friend: https://developer.apple.com/streaming/
Please see my answer here for how you can use it: Simultaneously downloading and playing a song that is pieced together from multiple URLs
It's very easy to run a long movie through the mediafilesegmenter tool from Apple (or FFMPEG) which spits out a number of small .ts files (MPEG 2 Transport Stream). Then you create a manifest (a .m3u8 file) which describes how these files fit together (which mediafilesegment will create for you too!). Then you just put the manifest file and the .ts files on a hosting provider (like GoDaddy) and you're all set.
For example, given a file called test.mp4, first turn it into a .ts file with ffmpeg:
ffmpeg -i test.mp4 -acodec copy -vcodec copy -bsf h264_mp4toannexb test.ts
Then turn it into a series of HLS segments with mediafilesegmenter (the same can be done using the ffmpeg segment muxer, but mediafilesegmenter seems to be more robust):
mediafilesegmenter -t 3 test.ts
The result is a bunch of 3 second clips (that's what -t 3 means) and an manifest file called prog_index.m3u8. The contents of that look like:
#EXTM3U
#EXT-X-TARGETDURATION:3
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:2.99520,
fileSequence0.ts
#EXTINF:2.99520,
fileSequence1.ts
#EXTINF:2.99520,
fileSequence2.ts
#EXTINF:2.99520,
fileSequence3.ts
...
#EXTINF:0.37440,
fileSequence75.ts
#EXT-X-ENDLIST
Simply putting all of the .ts files and the .m3u8 file on a web server and pointing your AVPlayer or MPMoviePlayerController in iOS at the URL for the .m3u8 will get you an excellent streaming performance.

Intro MP3 file with SHOUTcast

I have a PHP MP3 file which changes every time it is loaded, and plays a different advert, what I would like is away or is there away to play that before the SHOUTcast stream as the SHOUTcast intro file.
I personally dont really want to put it into flash or create custom playlist files but more would like SHOUTcast to fetch an intro file from a URL address.
Unfortunately, the SHOUTcast configuration doesn't allow this directly.
However, you can easily accomplish what you need by having a script pick a new intro file every few seconds or so. Then you can either write that MP3 to the file configured in SHOUTcast, or create a symlink and update it.

Resources