2015-04-07 3 views
3

Я хотел бы объединить видео (файл mp4) и аудио (mp3-файл) с помощью AVAssetExportSession с разными томами. Например, громкость звука от видео может быть 1, а громкость другого аудио может быть 0,5 в качестве фоновой музыки. Однако, похоже, [AVMutableAudioMixInputParameters setVolume] или [AVMutableAudioMixInputParameters setVolumeRampFromStartVolume] не работают. Объем музыки в выходном файле кажется совсем не ниже.Аудиовизуальные аудио с различными томами не работают

код показан в следующей

AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; 
// add a video track 
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
NSMutableArray *audioMixParams = [[NSMutableArray alloc]init]; 

NSError *error; 

if (musicPath && mVolume > 0) { 
    // add a music track 
    AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

    AVURLAsset *musicAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:musicPath] options:nil]; 
    if ([[musicAsset tracksWithMediaType:AVMediaTypeAudio] count] <= 0) { 
     [self.logger error:@"music file error : ", musicPath]; 
     handler(NO); 
     return; 
    } 
    AVAssetTrack *musicAudioTrack = [[musicAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    AVMutableAudioMixInputParameters *musicParam = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    musicParam.trackID = musicAudioTrack.trackID; 
    [self.logger debug:@"merge video and music : set music source volume -> %f", mVolume]; 
    // set music's volume 
    [musicParam setVolumeRampFromStartVolume:mVolume toEndVolume:mVolume timeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)]; 
    // add param to mix 
    [audioMixParams addObject:musicParam]; 

    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:musicAudioTrack atTime:kCMTimeZero error:&error]; 
    if (error) { 
     [self.logger error:@"Error when insert audio track of music file : %@", error.description]; 
    } 
} 

if ([[videoAsset tracksWithMediaType:AVMediaTypeAudio] count] && vVolume > 0) { 
    // add video sound track 
    AVMutableCompositionTrack *videoAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    AVAssetTrack * sourceAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; 
    AVMutableAudioMixInputParameters *videoParam = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    videoParam.trackID = sourceAudioTrack.trackID; 
    [self.logger debug:@"merge video and music : set video source volume -> %f", vVolume]; 
    // set video's audio volume 
    [videoParam setVolumeRampFromStartVolume:vVolume toEndVolume:vVolume timeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)]; 
    // add param to mix 
    [audioMixParams addObject:videoParam]; 

    [videoAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:sourceAudioTrack atTime:kCMTimeZero error:&error]; 
    if (error) { 
     [self.logger error:@"Error when insert audio track of video file : %@", error.description]; 
    } 
} 

// merge 
audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams]; 

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 
exporter.outputURL= [NSURL fileURLWithPath:outputPath]; 
exporter.outputFileType = AVFileTypeMPEG4; 
exporter.shouldOptimizeForNetworkUse = YES; 
exporter.audioMix = audioMix; 
exporter.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration); 
[exporter exportAsynchronouslyWithCompletionHandler:^{ 
}]; 

Может кто-нибудь помочь? Спасибо

+0

Вы пробовали - setVolume: atTime:? (обязательно установите CMTime правильно) – krafter

+0

Да, я использовал 'setVolume: 0.5 atTime: CMTimeZero' сначала, но он не работает ... – David

+0

Вы когда-нибудь это выясняли? –

ответ

0

Я нашел решение. AudioMixInputParameters должны взять идентификатор дорожки CompositionTrack, а не AVAssetTrack

Смежные вопросы