swiftrotationavfoundationcgaffinetransformcgaffinetransformscale

Video Merging Orientation in Swift


I am using video merging tutorial in that in ray wenderlich. I always get an 90 degree rotated video.When I add some different caffeine rotation codes my videos can t seen. Can you advise me I code that include CGAffineTransformMakeRotation.

My code is below

func orientationFromTransform(transform: CGAffineTransform) -> (orientation: UIImageOrientation, isPortrait: Bool) {
    var assetOrientation = UIImageOrientation.Up
    var isPortrait = false
    if transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0 {
        assetOrientation = .Right
        isPortrait = true

    } else if transform.a == 0 && transform.b == -1.0 && transform.c == 1.0 && transform.d == 0 {
        assetOrientation = .Left
        isPortrait = true
    } else if transform.a == 1.0 && transform.b == 0 && transform.c == 0 && transform.d == 1.0 {
        assetOrientation = .Up

    } else if transform.a == -1.0 && transform.b == 0 && transform.c == 0 && transform.d == -1.0 {
        assetOrientation = .Down

    }
    return (assetOrientation, isPortrait)
}


func videoCompositionInstructionForTrack(track: AVCompositionTrack, asset: AVAsset) -> AVMutableVideoCompositionLayerInstruction {
    // 1
    let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: track)
    // 2
    let assetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack

    // 3
    var transform = assetTrack.preferredTransform
    let assetInfo = orientationFromTransform(transform)
    var scaleToFitRatio = UIScreen.mainScreen().bounds.width / assetTrack.naturalSize.width

    if assetInfo.isPortrait {
        // 4
        print("ppppp")
        scaleToFitRatio = UIScreen.mainScreen().bounds.width / assetTrack.naturalSize.height
        let scaleFactor = CGAffineTransformMakeScale(scaleToFitRatio, scaleToFitRatio)

        //instruction.setTransform(t2, atTime: kCMTimeZero)


        instruction.setTransform(CGAffineTransformConcat(assetTrack.preferredTransform, scaleFactor),
            atTime: kCMTimeZero)




    } else {
        // 5
        let scaleFactor = CGAffineTransformMakeScale(scaleToFitRatio, scaleToFitRatio)
        var concat = CGAffineTransformConcat(CGAffineTransformConcat(assetTrack.preferredTransform, scaleFactor), CGAffineTransformMakeTranslation(0, UIScreen.mainScreen().bounds.width / 2))
        if assetInfo.orientation == .Down {
            print("down")
            let fixUpsideDown = CGAffineTransformMakeRotation(CGFloat(M_PI))
            let windowBounds = UIScreen.mainScreen().bounds
            let yFix = assetTrack.naturalSize.height + windowBounds.height
            let centerFix = CGAffineTransformMakeTranslation(assetTrack.naturalSize.width, yFix)
            concat = CGAffineTransformConcat(CGAffineTransformConcat(fixUpsideDown, centerFix), scaleFactor)
        }
        instruction.setTransform(concat, atTime: kCMTimeZero)
    }

    return instruction
}

Solution

  • A CGAffineTransformRotate operates around a point.

    When you rotate the video, it "moves" around that point, including sometimes off the screen.

     x---    rotate 90 degrees clockwise around X becomes  -----x
     |   |                                                |     |
     |   |                                                 -----
     |   |
      ---  
    

    Not a great picture but you can see that if x is a fixed point, the video has actually moved.

    Anyway, it's easy to fix - you're missing a Translate (literally moving the position of the video within the output frame).

    Experiment with CGAffineTranslate (by the scale of the video) and you'll see the frame move around.

    Good luck!