1

この問題に対してさまざまなアプローチを試みてきましたが、処理に時間がかかりすぎます (異なるボリュームの MP3 ファイルを変更します)。

オーディオとビデオ用のいくつかの AVMutableCompositionTrack で満たされる AVMutableComposition があります。ミキシングは正常に機能しますが、オーディオ トラックの音量調整が機能せず、エクスポート時に失敗します。

私が使用するコードは次のとおりです。

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
4

1 に答える 1

2

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];
}
于 2014-07-01T09:30:27.723 に答える