【问题标题】:Rotate an AVAsset with AVAssetExportSession使用 AVAssetExportSession 旋转 AVAsset
【发布时间】:2012-09-13 15:48:26
【问题描述】:

我正在尝试使用 AVAssetExportSession 将视频旋转到正确的方向,但我总是收到以下错误:

Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)"

转换为AVErrorInvalidVideoComposition,但我看不出我的视频合成有什么问题。代码如下:

AVAssetTrack *sourceVideo = [[avAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
AVAssetTrack *sourceAudio = [[avAsset tracksWithMediaType:AVMediaTypeAudio] lastObject];
CGAffineTransform preferredTransform = [sourceVideo preferredTransform];

AVMutableComposition *composition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality] autorelease];

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration)
                               ofTrack:sourceVideo
                                atTime:kCMTimeZero
                                 error:nil];

if( !CGAffineTransformIsIdentity(preferredTransform) ) {

    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width);
    videoComposition.frameDuration = CMTimeMake(1, compositionVideoTrack.naturalTimeScale);

    AVMutableVideoCompositionLayerInstruction *instruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:sourceVideo];
    [instruction setTransform:preferredTransform atTime:kCMTimeZero];

    AVMutableVideoCompositionInstruction *videoTrackInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoTrackInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, avAsset.duration);
    videoTrackInstruction.layerInstructions = [NSArray arrayWithObject:instruction];

    [videoComposition setInstructions:[NSArray arrayWithObject:videoTrackInstruction]];

    exporter.videoComposition = videoComposition;
}

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];

[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, avAsset.duration)
                               ofTrack:sourceAudio
                                atTime:kCMTimeZero
                                 error:nil];

exporter.outputURL = tempPathUrl;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
[exporter exportAsynchronouslyWithCompletionHandler:^{ }];

构图可能有什么问题?我已经浏览了文档,到目前为止还没有发现任何问题。

【问题讨论】:

  • 你有没有发现为什么会出现这个错误?
  • 好的,所以似乎有一半的赏金流向了 GingerBreadMane,以寻求一个不起作用的答案。 StackOverflow 上似乎没有真正的 iOS 开发者社区。真可惜。
  • 在 iOS 7.0.3 上遇到类似问题...当我使用刚录制的视频时,我的代码类似,但当我从库中选择视频时失败。这对我来说意义不大。当我找到解决方案时会通知您。可能不必使用 AVExportSession。

标签: iphone ipad core-graphics core-video


【解决方案1】:

这可能与您的帧持续时间有关。你正在使用 CMTimeMake(1, naturalTimeScale)
您应该检查 naturalTimeScale 因为它并不总是等于 fps。有关详细信息,请参阅AVFoundation Programming Guide“时间表示”。

【讨论】:

  • 您知道如何获取视频资产的 FPS 吗?
【解决方案2】:

我认为这是因为宽度和高度参数的顺序不正确。 你有:

    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width);

不应该

    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].width, [avAsset naturalSize].height);

改为?

【讨论】:

    【解决方案3】:

    错误来自您如何设置instruction

    ... videoCompositionLayerInstructionWithAssetTrack:sourceVideo]
    

    sourceVideo 不是该组合的成员。在您的情况下,这应该是compositionVideoTrack

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多