1

After picking a video from gallery, I am trying to rotate it and save it in the gallery. When I pick a video which is in landscape mode, my rotation is working perfectly. But if I select a video which is in portrait mode, and try to rotate it on a given angle. The Video gets additional rotation.

here is my code:

        let currentAsset = AVAsset( url: outputFileURL)
        let composition = AVMutableComposition.init()

        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTimeMake(1, 30)
        videoComposition.renderScale  = 1.0

        let compositionCommentaryTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)

        let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
        let videoDuration: CMTime = currentAsset.duration

        let clipVideoTrack:AVAssetTrack =  currentAsset.tracks(withMediaType: AVMediaType.video)[0]
        let audioTrack: AVAssetTrack = currentAsset.tracks(withMediaType: AVMediaType.audio)[0]

        try? compositionCommentaryTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration), of: audioTrack, at: kCMTimeZero)

        try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration ), of: clipVideoTrack, at: kCMTimeZero)
        var naturalSize = clipVideoTrack.naturalSize

        naturalSize = CGSize.init(width: naturalSize.width, height: naturalSize.height)
        videoComposition.renderSize = naturalSize
        let x = naturalSize.width
        let y = naturalSize.height

        let scale = CGFloat(1.0)
        let frontLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack!)
        var rotationTransform = CGAffineTransform(translationX: x/2, y: y/2)
        rotationTransform = rotationTransform.rotated(by: angle)
        rotationTransform = rotationTransform.translatedBy(x: -x/2, y: -y/2)

        frontLayerInstruction.setTransform(rotationTransform, at: kCMTimeZero)

// saving video in gallery

        let MainInstruction = AVMutableVideoCompositionInstruction()
        MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
        MainInstruction.layerInstructions = [frontLayerInstruction]
        videoComposition.instructions = [MainInstruction]
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let videoPath = documentsPath+"/cropEditVideo.mov"

        let fileManager = FileManager.default

        if fileManager.fileExists(atPath: videoPath) {
            try! fileManager.removeItem(atPath: videoPath)
        }

        var exportSession = AVAssetExportSession.init(asset: composition, presetName: AVAssetExportPresetHighestQuality)
        exportSession?.outputFileType = AVFileType.mov
        exportSession?.videoComposition = videoComposition
        exportSession?.outputURL = URL.init(fileURLWithPath: videoPath)
        .
        .
        .
        .

I have not posted the full code of saving video using AVAssetExportSession, as it is not needed I believe.

So if I apply a rotation of 10 degree on the video, its being saved with a rotation of 280 degree. So basically an additonal And its only happening with portrait videos.

Any help would be much appreciated.

Umair Afzal
  • 4,947
  • 5
  • 25
  • 50

1 Answers1

0

Let's consider this:

You have 2 cases:

  1. You rotate a landscape video and it works perfectly;

  2. You rotate a portrait video and it works perfectly;

Thus, the issue isn't with the rotation; there is already an initial rotation on the portrait videos you are attempting to use! No worries, this happens all the time and is generally the default behavior (I'm not sure why, I just have dealt with this many many times)

Some Examples:

AR Problem

AVAssetExportSession Problem

No need in rewriting code that has already been produced; your problem has a solution here, however, if you cannot find an answer, comment below and we can work it out!

impression7vx
  • 1,728
  • 1
  • 20
  • 50