gpt4 book ai didi

ios - AVMutableCompositionTrack - 使用 insertEmptyTimeRange 在两个 WAV 文件之间插入静音

转载 作者:行者123 更新时间:2023-12-02 23:50:28 28 4
gpt4 key购买 nike

我遇到的问题是在两个 wav 文件之间放置可变数量的静音。

到目前为止,我的方法如下:

首先我创建了一个 AVMutableComposition和一个 AVMutableCompositionTrack

AVMutableComposition* composition = [AVMutableComposition composition];
AVMutableCompositionTrack* appendedAudioTrack =
[composition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];

然后使用 AVURLAsset我分配了我方便命名的 first.wav 文件。
AVURLAsset* firstComponent = [[AVURLAsset alloc]
initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"first" ofType: @"wav"]] options:nil];

然后我将其插入到我命名为 appendedAudioTrack 的可变组合轨道中早些时候
NSArray *firstTrack = [firstComponent tracksWithMediaType:AVMediaTypeAudio];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, firstComponent.duration);
[appendedAudioTrack insertTimeRange:timeRange
ofTrack:[firstTrack objectAtIndex:0]
atTime:kCMTimeZero
error:&error];

second.wav 文件以完全相同的方式插入:
AVURLAsset* secondComponent = [[AVURLAsset alloc]
initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"second" ofType: @"wav"]] options:nil];
NSArray *secondTrack = [secondComponent tracksWithMediaType:AVMediaTypeAudio];
CMTimeRange timeRange2 = CMTimeRangeMake(kCMTimeZero, secondComponent.duration);
[appendedAudioTrack insertTimeRange:timeRange2
ofTrack:[secondTrack objectAtIndex:0]
atTime:kCMTimeZero
error:&error];

到目前为止,这已成功地将两个 wav 文件首尾相连。

然后我尝试使用 insertEmptyTimeRange 插入可变数量的静音像这样:
    CMTimeRange timeRange3 = CMTimeRangeFromTimeToTime(firstComponent.duration,CMTimeAdd(firstComponent.duration,CMTimeMake(interval,1)));
[appendedAudioTrack insertEmptyTimeRange:timeRange3];

静音持续时间是 float出于测试目的,目前为 0.49。它的变量名是 interval它以秒为单位表示所需的静音。

我使用 CMTimeRange 所做的假设是 AVURLAsset的持续时间属性可以认为是结束 CMTime对于第一个音轨。

当我从 Xcode 中的组织者下载文档目录,并在 audacity 中查看生成的 m4a 文件时,沉默在文件中,但它在开始时,而不是根据需要位于两个 .wav 文件的中间。

错误的是:SILENCE, first.wav, second.wav

我想知道如何正确使用 insertEmptyTimeRange 来生成
第一个.wav,沉默,第二个.wav。

注:我见过 this问题(和其他人)提出了一个非常相似的问题,但是他们采用的方法是使用恒定的静音文件。我的沉默是可变的,并在运行时确定。而且我也知道另一个答案提供了他们所说的解决方案,但它对我不起作用。我已经尝试了我在互联网上找到的所有不同方法,但似乎我误解了一些东西,因为它对我来说永远不会正常工作。

以防万一,我像这样导出文件:
 // Create a new audio file using the appendedAudioTrack
AVAssetExportSession* exportSession = [AVAssetExportSession
exportSessionWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];

if (!exportSession)
{
// do something
return nil;
}



//This gives me an output URL to a file name that doesn't yet exist
NSString *path2;
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path2 = [paths2 objectAtIndex:0];
path2 = [path2 stringByAppendingPathComponent:[string stringByAppendingString: @".m4a"]];
NSString* appendedAudioPath= path2;



exportSession.outputURL = [NSURL fileURLWithPath:appendedAudioPath];
exportSession.outputFileType = AVFileTypeAppleM4A;



[exportSession exportAsynchronouslyWithCompletionHandler:^{


switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
NSLog(@"%@",exportSession.error);
break;
case AVAssetExportSessionStatusCompleted:

break;
case AVAssetExportSessionStatusWaiting:
break;
default:
break;
}

谢谢

最佳答案

事实证明,间隔的持续时间不正确,当检查 audacity 时,它实际上是 0.049,而不是 0.49。

我遇到的问题是这个

CMTimeMake(interval,1)

我认为这会产生 interval/1秒,但我错了。

相反,我使用了 CMTimeMakeWithSeconds连同 NSEC_PER_SEC像这样
    CMTimeRange timeRange3 = CMTimeRangeFromTimeToTime(firstComponent.duration,CMTimeAdd(firstComponent.duration,CMTimeMakeWithSeconds(interval ,NSEC_PER_SEC)));
[appendedAudioTrack insertEmptyTimeRange:timeRange3];

然后这为我提供了所需的沉默 interval秒长,并且在正确的位置。

关于ios - AVMutableCompositionTrack - 使用 insertEmptyTimeRange 在两个 WAV 文件之间插入静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099671/

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