AVMutableComposition resizing issue

后端 未结 2 711
时光说笑
时光说笑 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.

提交回复
热议问题