一尘不染

如何使用AVURLAsset流式传输视频并将缓存的数据保存到磁盘

swift

几天前,我被要求检查从互联网下载视频时播放视频的难度。我知道这很容易,因为有人在不久前告诉我。所以,我检查了一下,这非常容易。

问题是我想将视频保存到磁盘上,而不是强迫用户一次又一次地下载它。

问题是访问缓冲区并将其存储到磁盘。

Stackoverflow中的许多答案都说不可能。特别是视频。

我播放视频的原始代码:

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()
}

阅读 398

收藏
2020-07-07

共1个答案

一尘不染

解决此问题的方法是使用AVAssetExportSessionAVAssetResourceLoaderDelegate

第一步是添加一条通知,以了解视频何时结束。然后,我们可以开始将其保存到磁盘。

override func viewDidLoad() {

    super.viewDidLoad()

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

    ...
}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

执行我们的功能:

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)
        })
    }
}

最后,我们需要使我们AVURLAsset的代理AVAssetResourceLoaderDelegate:

lazy var asset: AVURLAsset = {

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

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

    return asset
}()

和:

extension ViewController : AVAssetResourceLoaderDelegate {

}

我在GitHub中使用此代码创建了一个小型演示。

2020-07-07