ios – AVAssetExportSession忽略videoComposition轮换和剥离元数据

ios – AVAssetExportSession忽略videoComposition轮换和剥离元数据,第1张

概述我试图在我的iOS设备上传之前旋转视频,因为其他平台(如 Android)不能正确解释iOS记录视频中的旋转信息,因此播放它们不正确地旋转. 我看过下面的堆栈帖子,但没有成功应用任何一个我的情况: > iOS rotate every frame of video > Rotating Video w/ AVMutableVideoCompositionLayerInstruction > AVM 我试图在我的iOS设备上传之前旋转视频,因为其他平台(如 Android)不能正确解释iOS记录视频中的旋转信息,因此播放它们不正确地旋转.

我看过下面的堆栈帖子,但没有成功应用任何一个我的情况:

> iOS rotate every frame of video
> Rotating Video w/ AVMutableVideoCompositionLayerInstruction
> AVMutableVideoComposition rotated video captured in portrait mode
> iOS AVFoundation: Setting Orientation of Video

我处理了Apple AVSimpleEditor项目示例,但不幸的是,在创建AVAssetExportSession并调用exportAsynchronouslyWithCompletionHandler时,不会执行任何旋转,更糟糕的是,旋转元数据从生成的文件中被删除.

以下是运行导出的代码:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:[_mutableComposition copy] presetname:AVAssetExportPresetPassthrough];exportSession.outputURL = outputURL;exportSession.outputfileType = AVfileType3GPP;exportSession.shouldOptimizeforNetworkUse = YES;exportSession.vIDeoComposition = _mutableVIDeoComposition;[exportSession exportAsynchronouslyWithCompletionHandler:^(voID) {     NSLog(@"Status is %d %@",exportSession.status,exportSession.error);     handler(exportSession);     [exportSession release]; }];

_mutableComposition和_mutableVIDeoComposition的值由此方法初始化:

- (voID) getVIDeoComposition:(AVAsset*)asset{    AVMutableComposition *mutableComposition = nil;    AVMutableVIDeoComposition *mutableVIDeoComposition = nil;    AVMutableVIDeoCompositionInstruction *instruction = nil;    AVMutableVIDeoCompositionLayerInstruction *layerInstruction = nil;    CGAffinetransform t1;    CGAffinetransform t2;    AVAssetTrack *assetVIDeoTrack = nil;    AVAssetTrack *assetAudioTrack = nil;    // Check if the asset contains vIDeo and audio tracks    if ([[asset tracksWithMediaType:AVMediaTypeVIDeo] count] != 0) {        assetVIDeoTrack = [asset tracksWithMediaType:AVMediaTypeVIDeo][0];    }    if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] != 0) {        assetAudioTrack = [asset tracksWithMediaType:AVMediaTypeAudio][0];    }    CMTime insertionPoint = kCMTimeZero;    NSError *error = nil;    // Step 1    // Create a composition with the given asset and insert audio and vIDeo tracks into it from the asset    // Check whether a composition has already been created,i.e,some other tool has already been applIEd    // Create a new composition    mutableComposition = [AVMutableComposition composition];    // Insert the vIDeo and audio tracks from AVAsset    if (assetVIDeoTrack != nil) {        AVMutableCompositionTrack *compositionVIDeoTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];        [compositionVIDeoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,[asset duration]) ofTrack:assetVIDeoTrack atTime:insertionPoint error:&error];    }    if (assetAudioTrack != nil) {        AVMutableCompositionTrack *compositionAudioTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID];        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,[asset duration]) ofTrack:assetAudioTrack atTime:insertionPoint error:&error];    }    // Step 2    // Translate the composition to compensate the movement caused by rotation (since rotation would cause it to move out of frame)    t1 = CGAffinetransformMakeTranslation(assetVIDeoTrack.naturalSize.height,0.0);    // Rotate transformation    t2 = CGAffinetransformRotate(t1,degreesToradians(90.0));    // Step 3    // Set the appropriate render sizes and rotational transforms    // Create a new vIDeo composition    mutableVIDeoComposition = [AVMutableVIDeoComposition vIDeoComposition];    mutableVIDeoComposition.renderSize = CGSizeMake(assetVIDeoTrack.naturalSize.height,assetVIDeoTrack.naturalSize.wIDth);    mutableVIDeoComposition.frameDuration = CMTimeMake(1,30);    // The rotate transform is set on a layer instruction    instruction = [AVMutableVIDeoCompositionInstruction vIDeoCompositionInstruction];    instruction.timeRange = CMTimeRangeMake(kCMTimeZero,[mutableComposition duration]);    layerInstruction = [AVMutableVIDeoCompositionLayerInstruction vIDeoCompositionLayerInstructionWithAssetTrack:(mutableComposition.tracks)[0]];    [layerInstruction settransform:t2 atTime:kCMTimeZero];    // Step 4    // Add the transform instructions to the vIDeo composition    instruction.layerInstructions = @[layerInstruction];    mutableVIDeoComposition.instructions = @[instruction];    TT_RELEASE_SAFELY(_mutableComposition);    _mutableComposition = [mutableComposition retain];    TT_RELEASE_SAFELY(_mutableVIDeoComposition);    _mutableVIDeoComposition = [mutableVIDeoComposition retain];}

我从AVSERotateCommand from here中提取了这种方法.任何人都可以提出为什么这种方法不能将视频成功旋转90度?

解决方法 因为您正在使用AVAssetExportPresetPassthrough,AVAssetExportSession将忽略vIDeoComposition,请使用任何其他预设. 总结

以上是内存溢出为你收集整理的ios – AVAssetExportSession忽略videoComposition轮换和剥离元数据全部内容,希望文章能够帮你解决ios – AVAssetExportSession忽略videoComposition轮换和剥离元数据所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1097207.html

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

发表评论

登录后才能评论

评论列表(0条)

保存