Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

iphone - how to export video asset via AVAssetExportSession in portrait mode

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...