How to fix my orientation issue with merging videos from front and back camera

后端 未结 1 390
栀梦
栀梦 2020-12-16 07:11

I am merging multiply videos (implantation of pause button) and everything working fine except when merging video from the back camera with video from front camera then one

相关标签:
1条回答
  • 2020-12-16 08:09

    You need to be careful with the renderSize of your AVMutableVideoComposition and the transform of the AVMutableVideoCompositionLayerInstruction.

    To properly align them, you need to both translate and rotate the video with the correct degrees. To flip it upside down you need to rotate it by 180 degrees and translate it to the proper coordinates:

    ...
    videoComposition.renderSize = CGSizeMake(X, Y)
    ...
    let translate = CGAffineTransformMakeTranslation(X, Y);
    let rotate = CGAffineTransformRotate(translate, CGFloat(ANGLE_IN_RADIANS))
    ...    
    

    In my case, the renderSize and the translation are set to 1280 and 720 and the 180 degree rotation is basically M_PI in radians:

    let videoComposition = AVMutableVideoComposition()
    videoComposition.renderSize = CGSizeMake(1280, 720)
    videoComposition.frameDuration = CMTimeMake(1, 30)
    
    let videoInstruction = AVMutableVideoCompositionInstruction()
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))
    
    let transformInstruction:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
    let translate = CGAffineTransformMakeTranslation(1280, 720);
    let rotate = CGAffineTransformRotate(translate, CGFloat(M_PI))
    
    transformInstruction.setTransform(rotate, atTime: kCMTimeZero)
    videoInstruction.layerInstructions = [transformInstruction]
    videoComposition.instructions = [videoInstruction]
    

    In the end, you will still need to add this videoComposition to your AVAssetExportSession for the transformations to take effect.

    0 讨论(0)
提交回复
热议问题