首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AVAssetExportSession旋转AVAsset

使用AVAssetExportSession旋转AVAsset
EN

Stack Overflow用户
提问于 2012-09-13 23:48:27
回答 3查看 4.4K关注 0票数 8

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

代码语言:javascript
复制
Error Domain=AVFoundationErrorDomain Code=-11841 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11841.)"

翻译过来就是AVErrorInvalidVideoComposition,但我看不出我的视频构图有什么问题。代码如下:

代码语言:javascript
复制
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:^{ }];

构图可能有什么问题?我已经看过文档了,到目前为止还看不出有什么问题。

EN

回答 3

Stack Overflow用户

发布于 2012-10-26 03:02:41

这可能与您的帧持续时间有关。您正在使用CMTimeMake(1, naturalTimeScale)

您应该检查naturalTimeScale,因为它并不总是等于fps。有关更多信息,请参阅AVFoundation Programming Guide的“时间表示法”。

票数 0
EN

Stack Overflow用户

发布于 2014-07-02 23:33:13

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

代码语言:javascript
复制
    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].height, [avAsset naturalSize].width);

难道不应该是

代码语言:javascript
复制
    videoComposition.renderSize = CGSizeMake([avAsset naturalSize].width, [avAsset naturalSize].height);

而不是?

票数 0
EN

Stack Overflow用户

发布于 2015-08-15 13:58:39

该错误与您设置instruction的方式有关。

代码语言:javascript
复制
... videoCompositionLayerInstructionWithAssetTrack:sourceVideo]

sourceVideo不是该组合的成员。在您的示例中,它应该是compositionVideoTrack

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12410130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档