7

when i export a video asset via AVAssetExportSession the result file is in landspace mode. (file grabbed via itune->apps->file sharing->my app). how can i export the video asset in portrait mode (rotate it)?

amit
  • 2,171
  • 4
  • 31
  • 50

2 Answers2

22

The video coming from the iPhone capture device are always landscape orientated whatever the device orientation is when capturing.

If you want to rotate your video, the 'simple' solution is to assign a transform to the video track of the exported session.

Create 2 mutable tracks in your AVComposition object :

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

Add your medias tracks to your composition's tracks :

...        
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 added all your tracks, apply your transform to the video track of your composition :

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

(be carful that the transform had the upper left corner as origin, so the translation was needed after rotation, but tested on iPhone 4S, iOS 5.1, it seems that the rotation is now made around the center.)

Steven Schobert
  • 4,201
  • 3
  • 25
  • 34
Nicolas Buquet
  • 3,880
  • 28
  • 28
  • I try it. This is to change the rotation degree of video, if your player (ex. QuickTime) support the metadata of rotation, you can get portrait video. But the raw pixel buffer is still landscape. – CloudTuan Oct 08 '18 at 03:33
  • 1
    The raw pixel buffer will always be landscape only on iPhone/iPad (and even Android). Only the metadata can tell a player to display it rotated. If you want a movie file or raw pixels buffer to be portrait, I think you will have to write your own encoder, and it will be a lot of work. – Nicolas Buquet Oct 09 '18 at 04:31
  • Hi @NicolasBuquet, Can you help me in my Question, please, https://stackoverflow.com/questions/55771275/duet-merge-2-videos-side-by-side – Ahtazaz Apr 22 '19 at 12:51
0

When U transform the track meanwhile should set the composition renderSize since it may out of frame or appear with black block:

self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);
JonphyChen
  • 129
  • 1
  • 11