6

I'm doing some video editing and I need to put AVMutableVideoComposition I'm manipulation back into a player item. To go into a player item it needs to be an AVAsset. How to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Conor Quinn
  • 529
  • 4
  • 13
  • Take a look at this answer: [Exporting AVComposition and getting local url to play and upload the video](https://stackoverflow.com/a/46559459/2276920) – sahiljain Oct 04 '17 at 07:48
  • Take a look at [AVAssetExportSession](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAssetExportSession_Class/%20). – rkyr Aug 20 '15 at 13:38

2 Answers2

16

You can use an AVMutableComposition as an AVPlayerItem's asset since AVMutableComposition is a subclass of AVAsset.

An AVMutableVideoComposition is not a subclass of AVAsset, but rather a way to display the video that you've inserted into AVAssetTracks in an AVMutableComposition.

(If all of your videos are positioned the way you'd like without an AVMutableVideoComposition, then you may not need to set the player item's videoComposition property)

ObjC:

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

Swift:

let composition = AVMutableComposition(...
let videoComposition = AVMutableVideoComposition(...
let item = AVPlayerItem(asset: composition)
item.videoComposition = videoComposition
jlw
  • 3,166
  • 1
  • 19
  • 24
3

Check this- exportPath can be any temporary path for keeping the video.

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

                }
            })
        }) }
Aneesh G
  • 46
  • 5
  • 12
Thomas Paul
  • 355
  • 5
  • 13