gpt4 book ai didi

ios - AVAssetExportSession 视频未保存为纵向

转载 作者:行者123 更新时间:2023-11-29 10:29:50 26 4
gpt4 key购买 nike

我想在自定义 View 中录制视频,所以我按照以下代码使用 AVFoundation。

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;
});
}];
}

但是视频只能以横向模式保存。我引用了this并添加以下代码

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

但是在添加这个之后没有得到正确的结果。所以请帮我解决这个问题...

最佳答案

你应该使用 AVMutableVideoCompositionInstruction 和 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:^{

}];

关于ios - AVAssetExportSession 视频未保存为纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30262905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com