How to export a video object through AVAssetExportSession in portrait mode

when I export a video object through AVAssetExportSession, the result file is in landscape mode. (the file is captured via itune-> apps-> file sharing-> my application). How can I export a video object in portrait mode (rotate it)?

+5
iphone video avfoundation assets avassetexportsession
source share
2 answers

The video coming from the iPhone capture device is always landscape-oriented regardless of the orientation of the device when capturing.

If you want to rotate your video, the β€œsimple” solution is to assign the conversion of the video track of the exported session.

Create 2 mutable tracks in the AVComposition object:

AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

Add your media track to your composition:

 ... BOOL videoResult = [videoTrack insertTimeRange:sourceCMTime ofTrack:[tracks objectAtIndex:0] atTime:currentTime error:&error]; BOOL audioResult = [audioTrack insertTimeRange:sourceCMTime ofTrack:[tracks objectAtIndex:0] atTime:currentTime error:&error]; ... 

After you have added all your tracks, apply your conversion to the video track of your composition:

  CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2); // CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,360,0); videoTrack.preferredTransform = rotationTransform; 

(Be careful that the transformation has the upper left corner as the origin, so translation was necessary after rotation, but tested on iPhone 4S, iOS 5.1, it seems that rotation is now done around the center.)

+20
source share

When U converts the track, meanwhile, it must set the renderSize composition, since it can exit the frame or appear with a black block:

 self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width); 
0
source share

All Articles