how to correct orientation of video in objective c

Viewed 4681

I am using UIImagePickerController to capture video in iPhone but video is showing rotated 90 digree to right.

How to fix this issue

Is there any way by which we can correct the orientation.

2 Answers

[EDIT] Edited to add description how video re-encoding can be done on device.

When video is recorded with UIImagePickerController it should have orientation embedded in video file as exif flag. Video itself is in portrait orientation. When such video is played on iPhone (or in other way where exif rotation is honoured) the video should be oriented correctly. For example way to play a video explained http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/ .

If video is played in a custom way then orientation should be extracted from video (for example as described here https://stackoverflow.com/a/9195350/2546685) and applied during playback.

It is also possible to re-encode video using AVExportSession to rotate video to correct orientation and avoid using exif flag during playback. Example how re-encoding can be done: Combining this answer https://stackoverflow.com/a/16314552/2546685 with this one https://stackoverflow.com/a/9195350/2546685 (I did not tried to compile, syntax errors may exists) ->

AVURLAsset *footageVideo = [AVURLAsset URLAssetWithURL:assetURL options:nil];
AVAssetTrack *footageVideoTrack = [footageVideo tracksWithMediaType:AVMediaTypeVideo][0];

CGAffineTransform t = footageVideoTrack.preferredTransform;

AVMutableComposition *composition = [AVMutableComposition composition];

AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[videoCompositionTrack insertTimeRange:footageVideoTrack.timeRange ofTrack: footageVideoTrack atTime:CMTimeMakeWithSeconds(0, NSEC_PER_SEC) error:NULL];

NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {

self.exportSession = [[AVAssetExportSession alloc]
                      initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
// Implementation continues.

NSURL *furl = [NSURL fileURLWithPath:self.tmpVideoPath];

self.exportSession.outputURL = furl;
//provide outputFileType acording to video format extension
self.exportSession.outputFileType = AVFileTypeQuickTimeMovie;
self.exportSession.timeRange = footageVideoTrack.timeRange;

self.self.btnTrim.hidden = YES;
self.myActivityIndicator.hidden = NO;
[self.myActivityIndicator startAnimating];
[self.exportSession exportAsynchronouslyWithCompletionHandler:^{

    switch ([self.exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
        default:
            NSLog(@"Triming Completed");
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myActivityIndicator stopAnimating];
                self.myActivityIndicator.hidden = YES;
            });

            break;
    }
}];

}
Related