MTTransitions
MTTransitions copied to clipboard
Need to change orientation of video
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
Cloud you provide some example of the wrong orientations of videos ?
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.
Emmm.. It seems a bug. But you can change the orientation before merging. I will fix the bug when I have time.
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.
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
}
==========
Hello There, Any update as I am trying before and after change orientation . Can you please help me for this.
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 Any update on this orientation issue?
How can i set transform to MTVideoCompositionInstruction or please suggest anything to resolve this orientation issue?
Thanks
Any updates on this issue or anyone resolved it?? Please Help...