AVAssetExportSession视频未保存为纵向 [英] AVAssetExportSession video not saving as portrait orientation
本文介绍了AVAssetExportSession视频未保存为纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在自定义视图中录制视频,因此我按照以下代码使用AVFoundation.
I want to record on video in custom view so I'm using AVFoundation as per below code.
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
{
/** Below code works fine (save in portrait orientation)
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
if (!error)
{
self.doneButton.userInteractionEnabled = YES;
[videoAddr addObject:assetURL];
videoURL = outputFileURL;
}
}];*/
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAsset *asset = [AVAsset assetWithURL:outputFileURL];
[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@%d.mov",NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"],++videoCounter]];
[[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil];
NSLog(@"movie added to path:%@",myPathDocs);
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
self.doneButton.userInteractionEnabled = YES;
[videoAddr addObject:exporter.outputURL];
videoURL = outputFileURL;
flagAutorotate = NO;
});
}];
}
但是视频将仅以横向模式保存. 我已引用此并在下面添加代码
But video is going to save in landscape mode only. I've refer this and added below code
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
// CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotationTransform,360,0);
track.preferredTransform = rotationTransform;
但是在添加之后,无法得到正确的结果. 所以请帮我解决这个问题...
But after adding this not getting proper result. So please help me to solve this issue...
推荐答案
您应使用AVMutableVideoCompositionInstruction和layerInstruction
you should use AVMutableVideoCompositionInstruction and layerInstruction
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
[layerInstruction setTransform:rotationTransform atTime:kCMTimeZero];
instruction.layerInstructions = @[layerInstruction];
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(videoWidth, videoHeight);
videoComposition.instructions = @[instruction];
videoComposition.frameDuration = CMTimeMake(1, 30);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = exportURL;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.videoComposition = videoComposition;
[exporter exportAsynchronouslyWithCompletionHandler:^{
}];
这篇关于AVAssetExportSession视频未保存为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文