Video not rotating using AVMutableVideoCompositionLayerInstruction

后端 未结 3 1979
粉色の甜心
粉色の甜心 2021-02-06 12:42

I\'m trying to merge two videos I get after recording using the camera as a UIImagePickerController. I\'ve succeeded with combining the videos into one but I have some problems

3条回答
  •  情歌与酒
    2021-02-06 13:03

    Try using trackVideo in the initializer for your layer Instruction so it will use the AVMutableCompositionTrack's trackID rather than the source asset's trackID

    Update:

    You only need one AVMutableVideoCompositionLayerInstruction, so declare it before the loop with the AVMutableCompositionTrack as the parameter. Then on each iteration of the loop, set the necessary properties of the layer instruction (transform, crop rect) for the current video asset you're working with. You're controlling how the video content in the composition track should be displayed at each insert time.

    At the end, place the single layerInstruction in the instructions array, and use that in the video composition.

    var layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: trackVideo)
    for i in 0...(videos.count-1){
       //...your other code here
    
       layerInstruction.setTransform(assetTrack.preferredTransform, atTime: insertTime)
       layerInstruction.setCropRectangle(CGRectMake(0, 0, 300, 300), atTime: insertTime)
    }
    
    var instruction = AVMutableVideoCompositionInstruction();
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, insertTime);
    
    instruction.layerInstructions = NSArray(object: layerInstruction);
    var mainCompositionInst = AVMutableVideoComposition()
    mainCompositionInst.instructions = NSArray(object: instruction)
    

提交回复
热议问题