如何更改 AVMutableComposition 中曲目的音量

发布于 2025-01-07 23:55:21 字数 2233 浏览 4 评论 0原文

我一直在尝试不同的方法来解决这个问题,但它们需要很长时间才能处理(更改不同卷的 MP3 文件)。

我有一个 AVMutableComposition,其中填充了多个用于音频和视频的 AVMutableCompositionTrack。混合工作正常,但音轨音量调整不起作用,并且在导出时失败。

这是我使用的代码:

AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *soundTrackAsset = [[AVURLAsset alloc]initWithURL:trackTempProcessedURL options:nil];

//ADDING AUDIO
AVMutableCompositionTrack *compositionAudioSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackIDSoundTrack];
[compositionAudioSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
                                    ofTrack:[[soundTrackAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
                                     atTime:CMTimeAdd(cmTimeDifference,startTime) error:nil];

NSArray *tracksToDuck = [mixComposition tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
    AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
    [trackMix setVolume:volume atTime:kCMTimeZero];   
    [trackMixArray addObject:trackMix];
 }
 audioMix = [AVMutableAudioMix audioMix];
 audioMix.inputParameters = trackMixArray;

//ADDING VIDEO
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:frontAssetURL options:nil];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                               ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                atTime:startTime error:nil];

//EXPORTING
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName: AVAssetExportPresetPassthrough];

_assetExport.outputFileType = AVFileTypeQuickTimeMovie; 
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.audioMix = audioMix;  

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) { 
 ...

没有音频混合器,一切都混合得很好,但是当我尝试改变音量时,导出会出现错误:

AVFoundationErrorDomain Error: 11822

I have been trying different approaches to this problem, but they take too long to process (alter the MP3 file for different volumes).

I have an AVMutableComposition that gets filled with several AVMutableCompositionTrack for audio and video. The mixing works fine but the adjustment in the volume for the audio track is not working and fails when exporting.

Here is the code I use:

AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *soundTrackAsset = [[AVURLAsset alloc]initWithURL:trackTempProcessedURL options:nil];

//ADDING AUDIO
AVMutableCompositionTrack *compositionAudioSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackIDSoundTrack];
[compositionAudioSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
                                    ofTrack:[[soundTrackAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
                                     atTime:CMTimeAdd(cmTimeDifference,startTime) error:nil];

NSArray *tracksToDuck = [mixComposition tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
    AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
    [trackMix setVolume:volume atTime:kCMTimeZero];   
    [trackMixArray addObject:trackMix];
 }
 audioMix = [AVMutableAudioMix audioMix];
 audioMix.inputParameters = trackMixArray;

//ADDING VIDEO
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:frontAssetURL options:nil];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                               ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                atTime:startTime error:nil];

//EXPORTING
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName: AVAssetExportPresetPassthrough];

_assetExport.outputFileType = AVFileTypeQuickTimeMovie; 
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.audioMix = audioMix;  

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) { 
 ...

Everything mixes fine without the audio mixer but when I try to alter the volume the export gives me an error:

AVFoundationErrorDomain Error: 11822

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2025-01-14 23:55:21

AVMutableAudioMixInputParameters 需要设置“trackID”来指示哪个音轨应应用该参数。

for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
    AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
    [trackMix setVolume:volume atTime:kCMTimeZero];   

//+++++code
    AVMutableCompositionTrack * track = [tracksToDuck objectAtIndex:i]
    [trackMix setTrackID:[track trackID]];

    [trackMixArray addObject:trackMix];
}

AVMutableAudioMixInputParameters need set the "trackID" to indicates which audio track should applied the parameters.

for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
    AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
    [trackMix setVolume:volume atTime:kCMTimeZero];   

//+++++code
    AVMutableCompositionTrack * track = [tracksToDuck objectAtIndex:i]
    [trackMix setTrackID:[track trackID]];

    [trackMixArray addObject:trackMix];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文