【问题标题】:How to convert an AVMutableVideoComposition to an AVAsset如何将 AVMutableVideoComposition 转换为 AVAsset
【发布时间】:2015-08-18 19:27:39
【问题描述】:

我正在做一些视频编辑,我需要将我正在操作的 AVMutableVideoComposition 放回播放器项目中。要进入播放器项目,它需要是 AVAsset。如何做到这一点?

【问题讨论】:

标签: ios swift avfoundation


【解决方案1】:

您可以将AVMutableComposition 用作AVPlayerItem 的资产,因为AVMutableCompositionAVAsset 的子类。

AVMutableVideoComposition 不是AVAsset 的子类,而是一种在AVMutableComposition 中显示您插入AVAssetTracks 的视频的方式。

(如果没有AVMutableVideoComposition,您的所有视频都按照您想要的方式定位,那么您可能不需要设置播放器项目的videoComposition 属性)

对象:

AVMutableComposition *composition = ...
AVMutableVideoComposition *videoComposition = ...
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:composition];
item.videoComposition = videoComposition;

斯威夫特:

let composition = AVMutableComposition(...
let videoComposition = AVMutableVideoComposition(...
let item = AVPlayerItem(asset: composition)
item.videoComposition = videoComposition

【讨论】:

    【解决方案2】:

    勾选此项 - exportPath 可以是任何用于保存视频的临时路径。

        func ConvertAvcompositionToAvasset(avComp: AVComposition, completion:@escaping (_ avasset: AVAsset) -> Void){
            let exporter = AVAssetExportSession(asset: avComp, presetName: AVAssetExportPresetHighestQuality)
            let randNum:Int = Int(arc4random())
            //Generating Export Path
            let exportPath: NSString = NSTemporaryDirectory().appendingFormat("\(randNum)"+"video.mov") as NSString
            let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL
            //SettingUp Export Path as URL
            exporter?.outputURL = exportUrl as URL
            exporter?.outputFileType = AVFileTypeQuickTimeMovie
            exporter?.shouldOptimizeForNetworkUse = true
            exporter?.exportAsynchronously(completionHandler: {() -> Void in
                DispatchQueue.main.async(execute: {() -> Void in
                    if exporter?.status == .completed {
                        let URL: URL? = exporter?.outputURL
                        let Avasset:AVAsset = AVAsset(url: URL!)
                        completion(Avasset)
                    }
                    else if exporter?.status == .failed{
                        print("Failed")
    
                    }
                })
            }) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多