ios – 使用AVMutableVideoComposition导出时的视频方向问题

ios – 使用AVMutableVideoComposition导出时的视频方向问题,第1张

概述这是我用来导出视频的功能: - (void) videoOutput{//1 - Early exit if there's no video file selectedif (!self.videoAsset) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Lo 这是我用来导出视频的功能:

- (voID) vIDeoOutput{//1 - Early exit if there's no vIDeo file selectedif (!self.vIDeoAsset) {    UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"Error" message:@"Please Load a VIDeo Asset First"                                                   delegate:nil cancelbuttonTitle:@"OK" otherbuttonTitles:nil];    [alert show];    return;}// 2 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];// 3 - VIDeo trackAVMutableCompositionTrack *vIDeoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVIDeo                                                                    preferredTrackID:kCMPersistentTrackID_InvalID];[vIDeoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,self.vIDeoAsset.duration)                    ofTrack:[[self.vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0]                     atTime:kCMTimeZero error:nil];// 3.1 - Create AVMutableVIDeoCompositionInstructionAVMutableVIDeoCompositionInstruction *mainInstruction = [AVMutableVIDeoCompositionInstruction vIDeoCompositionInstruction];mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,self.vIDeoAsset.duration);// 3.2 - Create an AVMutableVIDeoCompositionLayerInstruction for the vIDeo track and fix the orIEntation.AVMutableVIDeoCompositionLayerInstruction *vIDeolayerInstruction = [AVMutableVIDeoCompositionLayerInstruction vIDeoCompositionLayerInstructionWithAssetTrack:vIDeoTrack];AVAssetTrack *vIDeoAssetTrack = [[self.vIDeoAsset tracksWithMediaType:AVMediaTypeVIDeo] objectAtIndex:0];BOol isVIDeoAssetPortrait_  = NO;CGAffinetransform vIDeotransform = vIDeoAssetTrack.preferredtransform;if (vIDeotransform.a == 0 && vIDeotransform.b == 1.0 && vIDeotransform.c == -1.0 && vIDeotransform.d == 0) {    isVIDeoAssetPortrait_ = YES;}if (vIDeotransform.a == 0 && vIDeotransform.b == -1.0 && vIDeotransform.c == 1.0 && vIDeotransform.d == 0) {    isVIDeoAssetPortrait_ = YES;}if (vIDeotransform.a == 1.0 && vIDeotransform.b == 0 && vIDeotransform.c == 0 && vIDeotransform.d == 1.0) {    isVIDeoAssetPortrait_  = NO;}if (vIDeotransform.a == -1.0 && vIDeotransform.b == 0 && vIDeotransform.c == 0 && vIDeotransform.d == -1.0) {    isVIDeoAssetPortrait_  = NO;}[vIDeolayerInstruction settransform:vIDeoAssetTrack.preferredtransform atTime:kCMTimeZero];[vIDeolayerInstruction setopacity:0.0 atTime:self.vIDeoAsset.duration];// 3.3 - Add instructionsmainInstruction.layerInstructions = [NSArray arrayWithObjects:vIDeolayerInstruction,nil];AVMutableVIDeoComposition *mainCompositionInst = [AVMutableVIDeoComposition vIDeoComposition];CGSize naturalSize;if(isVIDeoAssetPortrait_){    naturalSize = CGSizeMake(vIDeoAssetTrack.naturalSize.height,vIDeoAssetTrack.naturalSize.wIDth);} else {    naturalSize = vIDeoAssetTrack.naturalSize;}mainCompositionInst.renderSize = naturalSize;mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];mainCompositionInst.frameDuration = CMTimeMake(1,30);// 4 - Get pathNSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES);Nsstring *documentsDirectory = [paths objectAtIndex:0];Nsstring *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:                         [Nsstring stringWithFormat:@"FinalVIDeo-%d.mov",arc4random() % 1000]];NSURL *url = [NSURL fileURLWithPath:myPathDocs];// 5 - Create exporterAVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition                                                                  presetname:AVAssetExportPresetHighestQuality];exporter.outputURL=url;exporter.outputfileType = AVfileTypeQuickTimeMovIE;exporter.shouldOptimizeforNetworkUse = YES;exporter.vIDeoComposition = mainCompositionInst;[exporter exportAsynchronouslyWithCompletionHandler:^{    dispatch_async(dispatch_get_main_queue(),^{        [self exportDIDFinish:exporter];    });}];}

问题是我第一次使用此功能导出肖像视频时,变量vIDeotransform(vIDeoAssetTrack.preferredtransform)是:

vIDeotransform.a == 0 && vIDeotransform.b == 1.0 && vIDeotransform.c == -1.0 && vIDeotransform.d == 0

变量isVIDeoAssetPortrait_等于YES.一切都是对的.但是,导出完成并保存到Camera Roll后,我使用此功能重新加载结果视频.这次,vIDeotransform发生了变化:

vIDeotransform.a == 1.0 && vIDeotransform.b == 0 && vIDeotransform.c == 0 && vIDeotransform.d == 1.0

并且isVIDeoAssetPortrait_等于NO.这意味着一次导出后,vIDeotransform已更改其值(从纵向方向 – >横向)

我搜索了很多关于AV基金会视频定位的问题,但还没有找到解决方案.

感谢您阅读我的长篇解释.如果您有任何疑问,请告诉我.

解决方法 关于视频轨道没有关于肖像/风景的概念.它只是它的尺寸和应用的转换才能正确呈现它.默认情况下,“肖像”视频在摄像机生成时进行编码(比如横向渲染),90度旋转用于正确显示.

当您导出它时,它的方向不会改变,它只是在物理上旋转重新编码,因此不需要旋转来正确显示它.
这就是你第二次获得单位矩阵的原因(这并不意味着它已经从纵向更改为横向),但这次它的自然大小也会被交换,所有内容都应该基于您的代码.

请在后面的案例中说明错误.

总结

以上是内存溢出为你收集整理的ios – 使用AVMutableVideoComposition导出时的视频方向问题全部内容,希望文章能够帮你解决ios – 使用AVMutableVideoComposition导出时的视频方向问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存