Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to change orientation of video #5

Open
Deepak-kumar-varshney opened this issue May 25, 2020 · 9 comments
Open

Need to change orientation of video #5

Deepak-kumar-varshney opened this issue May 25, 2020 · 9 comments

Comments

@Deepak-kumar-varshney
Copy link

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

@alexiscn
Copy link
Owner

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

@Deepak-kumar-varshney
Copy link
Author

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

@alexiscn
Copy link
Owner

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

@Deepak-kumar-varshney
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@alexiscn
Copy link
Owner

alexiscn commented Jun 2, 2020

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

@Maulik699
Copy link

@alexiscn Any update on this orientation issue?

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

Thanks

@sabaanwar2024
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants