Last active
October 16, 2018 12:41
-
-
Save nileshpunjabi/c026f688da61e0e62794 to your computer and use it in GitHub Desktop.
Rotate Landscape video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//setting up the first video based on previous recording | |
CMTimeRange videoDuration = CMTimeRangeMake(kCMTimeZero, [self.previousRecording duration]); | |
AVAssetTrack *clipVideoTrack = [[self.previousRecording tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
AVAssetTrack *clipAudioTrack = [[self.previousRecording tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; | |
[compositionVideoTrack insertTimeRange:videoDuration ofTrack:clipVideoTrack atTime:nextClipStartTime error:nil]; | |
[compositionAudioTrack insertTimeRange:videoDuration ofTrack:clipAudioTrack atTime:nextClipStartTime error:nil]; | |
//our first track instruction - set up the instruction layer, then check the orientation of the track | |
//if the track is in landscape-left mode, it needs to be rotated 180 degrees (PI) | |
AVMutableVideoCompositionLayerInstruction *firstTrackInstruction = | |
[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack]; | |
if([self orientationForTrack:clipVideoTrack] == UIDeviceOrientationLandscapeLeft) { | |
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI); | |
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(640, 480); | |
CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter); | |
[firstTrackInstruction setTransform:mixedTransform atTime:kCMTimeZero]; | |
} | |
// Checking orientation for track .... | |
- (UIInterfaceOrientation)orientationForTrack:(AVAsset *)asset | |
{ | |
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; | |
CGSize size = [videoTrack naturalSize]; | |
CGAffineTransform txf = [videoTrack preferredTransform]; | |
if (size.width == txf.tx && size.height == txf.ty) | |
return UIInterfaceOrientationLandscapeRight; | |
else if (txf.tx == 0 && txf.ty == 0) | |
return UIInterfaceOrientationLandscapeLeft; | |
else if (txf.tx == 0 && txf.ty == size.width) | |
return UIInterfaceOrientationPortraitUpsideDown; | |
else | |
return UIInterfaceOrientationPortrait; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment