Skip to content

Instantly share code, notes, and snippets.

@nileshpunjabi
Last active October 16, 2018 12:41
Show Gist options
  • Save nileshpunjabi/c026f688da61e0e62794 to your computer and use it in GitHub Desktop.
Save nileshpunjabi/c026f688da61e0e62794 to your computer and use it in GitHub Desktop.
Rotate Landscape video
//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