2015-08-18 65 views
0

我正在做一些視頻編輯,我需要將AVMutableVideoComposition放到播放器項目中。要進入玩家項目,它需要成爲AVAsset。有誰知道他們的頭如何做到這一點?我無法找到如何在我查看的文檔中執行此操作。謝謝如何將AVMutableVideoComposition轉換爲AVAsset

+0

請看這個答案:[導出AVComposition並獲取本地url播放和上傳視頻](https://stackoverflow.com/a/46559459/2276920) – coder123

回答

5

您可以使用AVMutableComposition作爲AVPlayerItem的資產,因爲AVMutableCompositionAVAsset的子類。

AVMutableVideoComposition不是AVAsset的子類,而是一種在AVMutableComposition中顯示您插入到AVAssetTracks中的視頻的方法。

(如果您的所有影片被定位你想沒有AVMutableVideoComposition的方式,那麼你可能並不需要設置播放器項目的videoComposition屬性)

ObjC:

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 
0

檢查這 - exportPath可以保持視頻

012的任何臨時路徑
func ConvertAvcompositionToAvasset(avComp: AVComposition, completion:@escaping (_ avasset: AVAsset) -> Void){ 
    let exporter = AVAssetExportSession(asset: avComp, presetName: AVAssetExportPresetHighestQuality) 
    let randNum:Int = Int(arc4random()) 
    let exportPath: NSString = NSTemporaryDirectory().appendingFormat("\(randNum)"+"video.mov") as NSString 
    let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL 

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

      } 
     }) 
    }) 
} 
相關問題