AVMutableComposition resizing issue

后端 未结 2 665
时光说笑
时光说笑 2021-02-19 23:10

I\'m trying to render an image into a video captured with the front camera using AVMutableComposition. The size of the resulting video (including the image) is

相关标签:
2条回答
  • 2021-02-19 23:26

    This is really tricky: You need to check the preferredTransform of the video track to determine wether it is a portrait video or not.

        var videoAssetOrientation = UIImageOrientation.up
        var isVideoAssetPortrait = false
        var videoTransform = videoAssetTrack.preferredTransform
        if needsMirroring == true  {
            isVideoAssetPortrait = true
        }else if videoTransform.a == 0 && videoTransform.b == 1.0 && videoTransform.c == -1.0 && videoTransform.d == 0 {
            videoAssetOrientation = .right
            isVideoAssetPortrait = true
        }else if videoTransform.a == 0 && videoTransform.b == -1.0 && videoTransform.c == 1.0 && videoTransform.d == 0 {
            videoAssetOrientation = .left
            isVideoAssetPortrait = true
        }else if videoTransform.a == 1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == 1.0 {
            videoAssetOrientation = .up
        }else if videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0 {
            videoAssetOrientation = .down
        }
    
        //Add instructions
        mainInstruction.layerInstructions = [videoLayerInstruction]
        let mainCompositionInst = AVMutableVideoComposition()
        let naturalSize : CGSize!
        if isVideoAssetPortrait {
            naturalSize = CGSize(width: videoAssetTrack.naturalSize.height, height: videoAssetTrack.naturalSize.width)
        } else {
            naturalSize = videoAssetTrack.naturalSize
        }
    

    Hope that helps.

    0 讨论(0)
  • 2021-02-19 23:26

    Try applying a negative scale transform to flip the video when mirrored:

    // Create an AvmutableVideoCompositionLayerInstruction
    let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction.init(assetTrack: videoTrack!)
    let flipped = videoAssetTrack.preferredTransform.scaledBy(x: -1.0, y: 1.0)
    videoLayerInstruction.setTransform(flipped, at: kCMTimeZero)
    compositionInstructions.layerInstructions = [videoLayerInstruction]    
    
    0 讨论(0)
提交回复
热议问题