MTTransitions icon indicating copy to clipboard operation
MTTransitions copied to clipboard

Need to change orientation of video

Open Deepak-kumar-varshney opened this issue 4 years ago • 9 comments

Hello Team,

you made great example of transition between videos but when I use your library its always gives me wrong orientation video. can you please help me for the same.

Thanks in advance

Deepak-kumar-varshney avatar May 25 '20 12:05 Deepak-kumar-varshney

Cloud you provide some example of the wrong orientations of videos ?

alexiscn avatar May 25 '20 14:05 alexiscn

Please find video . I am choosing 2 video from gallery which have portrait orientation. but after using transition (any from transition list) it gives me wrong orientation video.

wrong-orientation_IFYz4Dfj.compressed.mov.zip

Deepak-kumar-varshney avatar May 26 '20 05:05 Deepak-kumar-varshney

Emmm.. It seems a bug. But you can change the orientation before merging. I will fix the bug when I have time.

alexiscn avatar May 27 '20 02:05 alexiscn

I changed orientation before merging by your library but it gives same result. I will try some other methods and inform you if it is done from my side.

Deepak-kumar-varshney avatar May 27 '20 04:05 Deepak-kumar-varshney

I change video orientation before merging and try also after merging but nothing will happen. please look into this As soon as possible .

please find code which used for same

========== func changeVideoOrientation(videoUrl: URL) -> AVMutableComposition {

    let mixComposition = AVMutableComposition()
    var mutableCompositionVideoTrack = [AVMutableCompositionTrack]()
    var mutableCompositionAudioOfVideoTrack = [AVMutableCompositionTrack]()

    //start merge

    let aVideoAsset = AVAsset(url: videoUrl)

    let compositionAddVideo = mixComposition.addMutableTrack(withMediaType: .video,
                                                             preferredTrackID: kCMPersistentTrackID_Invalid)
    let compositionAddAudio = mixComposition.addMutableTrack(withMediaType: .audio,
                                                             preferredTrackID: kCMPersistentTrackID_Invalid)
    let compositionAddAudioOfVideo = mixComposition.addMutableTrack(withMediaType: .audio,
                                                                    preferredTrackID: kCMPersistentTrackID_Invalid)

    let aVideoAssetTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
    let aAudioOfVideoAssetTrack: AVAssetTrack? = aVideoAsset.tracks(withMediaType: AVMediaType.audio).first

    // Default must have tranformation

    compositionAddVideo?.preferredTransform = aVideoAssetTrack.preferredTransform

    var transforms = aVideoAssetTrack.preferredTransform
    let angle = (atan2(transforms.a, transforms.b) * 180) / .pi
    Debug.log("angle ==== \(angle)")
    if angle == 180 {
        
        transforms = transforms.concatenating(CGAffineTransform(rotationAngle: -90))
    }
    else if angle == 0 {
        transforms = transforms.concatenating(CGAffineTransform(rotationAngle: 90))
    }
    else if angle == -90 {
        transforms = transforms.concatenating(CGAffineTransform(rotationAngle: 180))
    }

    compositionAddVideo?.preferredTransform = transforms

    mutableCompositionVideoTrack.append(compositionAddVideo!)
    mutableCompositionAudioOfVideoTrack.append(compositionAddAudioOfVideo!)

    do {

        try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero,
                                                                            duration: aVideoAssetTrack.timeRange.duration),
                                                            of: aVideoAssetTrack,
                                                            at: CMTime.zero)


        // adding audio (of the video if exists) asset to the final composition
        if let aAudioOfVideoAssetTrack = aAudioOfVideoAssetTrack {
            try mutableCompositionAudioOfVideoTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero,
                                                                                       duration: aVideoAssetTrack.timeRange.duration),
                                                                       of: aAudioOfVideoAssetTrack,
                                                                       at: CMTime.zero)
        }
    } catch {
        print(error.localizedDescription)
    }

    return mixComposition

}

==========

Deepak-kumar-varshney avatar May 28 '20 07:05 Deepak-kumar-varshney

Hello There, Any update as I am trying before and after change orientation . Can you please help me for this.

Deepak-kumar-varshney avatar Jun 01 '20 12:06 Deepak-kumar-varshney

There are two things to be done to handle the orientation of the input videos.

First, we should set preferredTransform to input video's preferredTransform according to Apple Reference

This can be done by add following code to MTVideoTransition at line 132


buildTransitionComposition(composition, videoComposition: videoComposition)

// to fix the orientation of passthrough videos
let videos = composition.tracks(withMediaType: .video)
for video in videos {
    video.preferredTransform = videoTrack.preferredTransform
}

Second, we should handle the transition frame's orientation. We should pass the preferredTranform information to the MTVideoCompositionInstruction and handle the pixelBuffer.

You can have a look at DVGStackableVideoCompositor

I will fix the bug when I have spare time but you can try to fix it and make a PR. :D

alexiscn avatar Jun 02 '20 07:06 alexiscn

@alexiscn Any update on this orientation issue?

How can i set transform to MTVideoCompositionInstruction or please suggest anything to resolve this orientation issue?

Thanks

Maulik699 avatar Jun 17 '23 19:06 Maulik699

Any updates on this issue or anyone resolved it?? Please Help...

sabaanwar2024 avatar Oct 13 '23 13:10 sabaanwar2024