Iphone 使用AVAssetExportSession旋转AVAsset

Iphone 使用AVAssetExportSession旋转AVAsset,iphone,ipad,core-graphics,core-video,Iphone,Ipad,Core Graphics,Core Video,我正在尝试使用AVAssetExportSession将视频旋转到正确的方向,但始终出现以下错误: Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)" 这就转化为平均无效视频合成,但我看不出我的视频合成有任何错误。代码如下: AVAssetTrack *sourceVideo = [[avAs

我正在尝试使用
AVAssetExportSession
将视频旋转到正确的方向,但始终出现以下错误:

Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)"
这就转化为
平均无效视频合成
,但我看不出我的视频合成有任何错误。代码如下:

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:^{ }];

这篇作文有什么问题?我已经阅读了文档,到目前为止没有发现任何错误。

这可能与您的帧持续时间有关。你正在使用
CMTimeMake(1,自然多尺度)


您应该检查自然多尺度,因为它并不总是等于fps。有关更多信息,请参阅“时间表示法”。

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

    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width);
难道不是吗

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

相反?

错误源于您如何设置
指令

... videoCompositionLayerInstructionWithAssetTrack:sourceVideo]

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

你有没有发现你为什么会犯这样的错误?好吧,看来一半的赏金都归姜饼了,因为答案是无效的。StackOverflow上似乎没有真正的iOS开发者社区。很遗憾,在iOS 7.0.3上遇到了类似的问题。。。当我使用刚录制的视频时,我的代码与之类似,但当我从库中选择视频时失败。这对我来说没什么意义。我找到解决方案后会通知你的。可能不需要使用AVExportSession。您知道如何获取视频资源的FPS吗?