gpt4 book ai didi

iphone - 使用 AVAssetExportSession 旋转 AVAsset

转载 作者:行者123 更新时间:2023-12-03 18:36:51 25 4
gpt4 key购买 nike

我尝试使用 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:^{ }];

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

最佳答案

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

关于iphone - 使用 AVAssetExportSession 旋转 AVAsset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12410130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com