11

Some days ago I was asked to check how difficult is to play a video while downloading it from Internet. I know it's an easy task because someone told me a while ago. So, I checked and it was super easy.

The problem was that I wanted to save to disk the video to do not force the user to download it again and again.

The problem was to access the buffer and store it to disk.

Many answers in Stackoverflow says it is nor possible. Specially with videos.

My original code to play the video:

import AVFoundation

....

//MARK: - Accessors

lazy var player: AVPlayer = {

    var player: AVPlayer = AVPlayer(playerItem: self.playerItem)

    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None

    return player
}()

lazy var playerItem: AVPlayerItem = {

    var playerItem: AVPlayerItem = AVPlayerItem(asset: self.asset)

    return playerItem
}()

lazy var asset: AVURLAsset = {

    var asset: AVURLAsset = AVURLAsset(URL: self.url)

    return asset
}()

lazy var playerLayer: AVPlayerLayer = {

    var playerLayer: AVPlayerLayer = AVPlayerLayer(player: self.player)

    playerLayer.frame = UIScreen.mainScreen().bounds
    playerLayer.backgroundColor = UIColor.clearColor().CGColor

    return playerLayer
}()

var url: NSURL = {

    var url = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

    return url!
}()

//MARK: - ViewLifeCycle

override func viewDidLoad() {

    super.viewDidLoad()

    view.layer.addSublayer(playerLayer)

    player.play()
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81

2 Answers2

25

The solution for this problem is to use AVAssetExportSession and AVAssetResourceLoaderDelegate:

First step is to add a notification to know when the video finish. Then we can start saving it to disk.

override func viewDidLoad() {

    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(playerItemDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)

    ...
}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

The implementation of our function:

func playerItemDidReachEnd(notification: NSNotification) {

    if notification.object as? AVPlayerItem  == player.currentItem {

        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

        let filename = "filename.mp4"

        let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!

        let outputURL = documentsDirectory.URLByAppendingPathComponent(filename)

        exporter?.outputURL = outputURL
        exporter?.outputFileType = AVFileTypeMPEG4

        exporter?.exportAsynchronouslyWithCompletionHandler({

            print(exporter?.status.rawValue)
            print(exporter?.error)
        })
    }
}

Finally we need to make our AVURLAsset delegate of AVAssetResourceLoaderDelegate:

lazy var asset: AVURLAsset = {

    var asset: AVURLAsset = AVURLAsset(URL: self.url)

    asset.resourceLoader.setDelegate(self, queue: dispatch_get_main_queue())

    return asset
}()

And:

extension ViewController : AVAssetResourceLoaderDelegate {

}

I created a small demo with this code in GitHub.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • 2
    Why is AVAssetResourceLoaderDelegate necessary here? I don't see any of these being used: https://developer.apple.com/reference/avfoundation/avassetresourceloaderdelegate – Lane Rettig Mar 04 '17 at 21:16
  • 5
    I get the following error when I try to run this code:`Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSUnderlyingError=0x61800024ebe0 {Error Domain=NSOSStatusErrorDomain Code=-12109 "(null)"}, NSLocalizedFailureReason=The operation is not supported for this media., NSLocalizedDescription=Operation Stopped}`. I made sure that the `outputURL` and `outputFileType` are correct. – Lane Rettig Mar 04 '17 at 21:36
  • Worked for me. What are the precautions we need to take when we play one url at a time ? – KavyaKavita Apr 24 '17 at 12:32
  • This look great, but I tried the demo project and it didn't save it, giving me the error. Optional(Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save" – user3069232 May 05 '17 at 18:34
  • Hello, is this demo implementable in a collectionView of videos without making the scroll laggy ? – Farid Al Haddad Jun 06 '17 at 23:48
  • 1
    Could you please let me know if this will be exporting the video file from the buffered cache, was just curious since we are downloading through the new asset and was not able to understand as to how it will get the current player item buffer – GoodSp33d Dec 05 '17 at 13:21
  • If you have the `The operation is not supported for this media.` error - add the delegate! – kelin Mar 22 '19 at 18:53
  • Is this actually working for anyone? I am getting the error as @LaneRettig – Legolas Jun 05 '19 at 21:01
  • What happens if I start watching a longer video and then scrub to the end? Will it export something? If yes, does it just export the watched sequences? – heyfrank Jan 05 '21 at 19:19
  • In my case it's always failing on simulator for some reason, but on a real device it's working. – heyfrank Jan 05 '21 at 22:27
3

The team at Calm has open-sourced our implementation to this. It's available as a CocoaPod. It's called PersistentStreamPlayer.

Features include:

  • streaming of audio file, starting playback as soon as first data is available
  • also saves streamed data to a file URL as soon as the buffer completes exposes timeBuffered, helpful for displaying buffer progress bars in the UI
  • handles re-starting the audio file after the buffer stream stalls (e.g. slow network)
  • simple play, pause and destroy methods (destroy clears all memory resources)
  • does not keep audio file data in memory, so that it supports large files that don't fit in RAM

You can find it here: https://github.com/calmcom/PersistentStreamPlayer

Tyler Sheaffer
  • 1,953
  • 1
  • 16
  • 16
  • 1
    It looks quite similar than this code: https://gist.github.com/anonymous/83a93746d1ea52e9d23f An issue I'm having is that when using the `AVAssetResourceLoaderDelegate` the `readyToPlay` events happens only once the entire file is downloaded, while it used to happen as soon as enough data was received to play the beginning of the video – Guig Dec 31 '16 at 13:43
  • Yes it is very similar to that code, as I started from there as a branching off point. I've added several nice features on top of that, however. – Tyler Sheaffer Jan 05 '17 at 16:20
  • As for the `AVAssetResourceLoaderDelegate`, @Guig notice that your instance of `PersistentStreamPlayer` is implementing that protocol, so I'm not sure how you're hooking in to listen for that. If you'd like to open an issue on the repo I'll be happy to work through the issue with you, or feel free to open a Pull Request there – Tyler Sheaffer Jan 05 '17 at 16:22
  • done, thanks. I can try to add more details / repro if you need and if I've not lost my toy project already – Guig Jan 05 '17 at 19:08
  • Thanks! does it also support video? – Frade Feb 03 '17 at 23:42
  • @Frade It should in theory, but I've only tested it in production with audio. If it doesn't quite fit your use case it should be much easier to fork and PR to get video support working than to start from scratch. Cheers. – Tyler Sheaffer Feb 05 '17 at 05:06
  • @Tyler, Thanks, will try it to load video – Frade Feb 06 '17 at 14:47
  • Hi Tyler! apparently cocoapods are fetching Installing version 0.1.0: "Persistent StreamPlayer (0.1.0)" – Frade Feb 07 '17 at 14:21
  • Thanks @Frade It should be close to working with video. Pull requests welcome. In any case, let's more this discussion to https://github.com/calm/PersistentStreamPlayer/issues/3 – Tyler Sheaffer Feb 08 '17 at 19:58
  • FYI: This code is unfinished. It does not allow for proper saving of file if seeking while the file is still being downloaded. – Greg Ellis Nov 25 '19 at 23:40