8

我正在尝试使用 an 将视频旋转到正确的方向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:^{ }];

组合可能有什么问题?我已经阅读了文档,到目前为止看不到任何问题。

4

3 回答 3

0

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

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

难道不应该

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

反而?

于 2014-07-02T15:33:12.753 回答
0

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

于 2012-10-25T19:02:41.243 回答
0

错误来自您的设置方式instruction

... videoCompositionLayerInstructionWithAssetTrack:sourceVideo]

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

于 2015-08-15T05:58:38.667 回答