gpt4 book ai didi

ios - AVAssetExportSession 创建电影成功,但时长关闭

转载 作者:行者123 更新时间:2023-11-29 13:36:43 25 4
gpt4 key购买 nike

我一直在尝试将视频与背景音频结合起来。我能够创建一个包含 1 个视频和 1 个音频文件的视频(谢天谢地,我在 SO 上找到了它),但我很难将其扩展为使用 2 个或更多音频文件。背景音频可以来自多个文件,我想将它们按顺序添加到视频中。

例。 audio-1.caf 长 2 秒,audio-2.caf 长 2 秒,video-1.mov 长 4 秒。

我希望 audio-1 的时间间隔为 0-2 秒,audio-2 的时间间隔为 2-4 秒。不幸的是,我使用的代码创建了一个 2 秒的电影,其中第一个音频为 1,其余的电影和音频不包括在内。

我简化了问题,这样我就可以在这里寻求帮助。下面的代码尝试创建一个包含 2 个音频源的空白视频。 (我以前的实现是动态的,循环遍历所有音频源)

-(void)writeAudio
{
AVMutableComposition* mixComposition = [AVMutableComposition composition];

NSDictionary * assetOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

NSString * outputFilePath = NSHomeDirectory();
outputFilePath = [outputFilePath stringByAppendingPathComponent:@"Library"];
outputFilePath = [outputFilePath stringByAppendingPathComponent:@"temp.mov"];
NSURL * outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
[[NSFileManager defaultManager] removeItemAtURL:outputFileUrl error:nil];

CMTime nextClipStartTime = kCMTimeZero;
NSURL * audioURL /* = kSomeValidURL */;
NSURL * audio2URL /* = kSomeOtherValidURL */;

AVURLAsset* audioAsset = [[AVURLAsset alloc] initWithURL:audioURL options:assetOptions];
CMTimeRange audio_timeRange = CMTimeRangeMake(nextClipStartTime, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
nextClipStartTime = CMTimeAdd(nextClipStartTime, audio_timeRange.duration);

AVURLAsset* audio2Asset = [[AVURLAsset alloc] initWithURL:audio2URL options:assetOptions];
CMTimeRange audio2_timeRange = CMTimeRangeMake(nextClipStartTime, audio2Asset.duration);
AVMutableCompositionTrack *b_compositionAudio2Track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudio2Track insertTimeRange:audio2_timeRange ofTrack:[[audio2Asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
nextClipStartTime = CMTimeAdd(nextClipStartTime, audio2_timeRange.duration);

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
NSLog(@"audio was completed???");
}
];


}

问题仍然存在,新视频由第一个 audioURL 的内容组成,并且是第一个 audioURL 内容的持续时间。

最佳答案

我的问题源于没有完全理解 AVFoundation(我仍然不理解)。

我的时间间隔,仍然需要从 0 开始并持续音频 Assets 的持续时间,然后在我希望它们开始的时间插入。

我更改了初始化 audio_timeRanges 的代码。

来自

CMTimeRange audio_timeRange = CMTimeRangeMake(nextClipStartTime, audioAsset.duration);

CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

CMTimeRange audio2_timeRange = CMTimeRangeMake(nextClipStartTime, audio2Asset.duration);

CMTimeRange audio2_timeRange = CMTimeRangeMake(kCMTimeZero, audio2Asset.duration);

关于ios - AVAssetExportSession 创建电影成功,但时长关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403334/

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