开发者

AVMutableAudioMix and AVAssetExportSession

I want to make fade in/out effect for export session. But it doesn't work. Please help me!

Trim works perfect! But fade in/out doesn't :(((

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];  
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:songAsset
                                               presetName:AVAssetExportPresetPassthrough];

        NSArray *tracks = [songAsset tracksWithMediaType:AVMediaTypeAudio];
        AVAssetTrack *track = [tracks objectAtIndex:0];
        id desc = [track.formatDescriptions objectAtIndex:0];
        const AudioStreamBasicDescription *audioDesc = CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)desc);
        FourCharCode formatID = audioDesc->mFormatID;


    // trim track
        CMTime startTrimTime = CMTimeMakeWithSeconds(startTime, 1);
        CMTime endTrimTime = CMTimeMakeWithSeconds(endTime, 1);
        CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTrimTime, endTrimTime);
        exportSession.timeRange = exportTimeRange;

    //add fade in, fade out     
            CMTime startFadeInTime = startTrimTime;
            CMTime endFadeInTime = CMTimeMakeWithSeconds(startTime + 4.0, 1);
            CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, endFadeInTime);

            CMTime startFadeOutTime = CMTimeMakeWithSeconds(endTime - 4.0, 1);
            CMTime endFadeOutTime = endTrimTime;
            CMTimeRange fadeOutTimeRange = CMTimeRangeFromTimeToTime(startFadeOutTime, endFadeOutTime);

            AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];

            //AVMutableAudioMixInputPara开发者_如何学Pythonmeters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParameters];
            AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

            [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
            [exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

            exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters];       
            exportSession.audioMix = exportAudioMix;
...

    myDeleteFile(filePath);
    exportSession.outputURL = [NSURL fileURLWithPath:filePath];


    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"export session completed");
            //
            //return YES;
        } else {
            NSLog(@"export session error");
            //return NO;
        }

        [exportSession release];
    }];


You can try to add this line:

[exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-0.01, 1)];

Before:

[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];

Sample Code:

//trim
CMTime startTrimTime = CMTimeMakeWithSeconds(start, 1);
CMTime endTrimTime = CMTimeMakeWithSeconds(start+length, 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTrimTime, endTrimTime);
exportSession.timeRange = exportTimeRange;


//add fade in, fade out
AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];

NSMutableArray* inputParameters = [NSMutableArray arrayWithCapacity:1];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
for(AVAssetTrack *track in tracks) {
    //NSLog(@"track:%@", track);
    CMTime startFadeInTime = startTrimTime;
    CMTime endFadeInTime = CMTimeMakeWithSeconds(start + 4.0, 1);
    CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, endFadeInTime);

    CMTime startFadeOutTime = CMTimeMakeWithSeconds(start+length - 4.0, 1);
    CMTime endFadeOutTime = endTrimTime;
    CMTimeRange fadeOutTimeRange = CMTimeRangeFromTimeToTime(startFadeOutTime, endFadeOutTime);

    //AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParameters];
    AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

    [exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-0.01, 1)];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

    [inputParameters addObject:exportAudioMixInputParameters];
}

exportAudioMix.inputParameters = inputParameters;
exportSession.audioMix = exportAudioMix;


Here is the solution.

setVolumeRampFromStartVolume doesn't work.

AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

//fade in

[exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-1, 1)];
[exportAudioMixInputParameters setVolume:0.1 atTime:CMTimeMakeWithSeconds(start, 1)];
[exportAudioMixInputParameters setVolume:0.5 atTime:CMTimeMakeWithSeconds(start+1, 1)];
[exportAudioMixInputParameters setVolume:1.0 atTime:CMTimeMakeWithSeconds(start+2, 1)];

//fade out

[exportAudioMixInputParameters setVolume:1.0 atTime:CMTimeMakeWithSeconds((start+length-2), 1)];
[exportAudioMixInputParameters setVolume:0.5 atTime:CMTimeMakeWithSeconds((start+length-1), 1)];
[exportAudioMixInputParameters setVolume:0.1 atTime:CMTimeMakeWithSeconds((start+length), 1)];

exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters];


// configure export session  output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:filePath]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type
exportSession.timeRange = exportTimeRange; // trim time ranges
exportSession.audioMix = exportAudioMix; // fade in audio mix
// perform the export
[exportSession exportAsynchronouslyWithCompletionHandler:^{

    if (AVAssetExportSessionStatusCompleted == exportSession.status) {
        NSLog(@"AVAssetExportSessionStatusCompleted");

    } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
        NSLog(@"AVAssetExportSessionStatusFailed");

    } else {
        NSLog(@"Export Session Status: %d", exportSession.status);
    }
}];


you are adding the audio mix parameters to an AVAssetTrack.

My working version sets the audio mix params to a AVMutableCompostionTrack:

AVMutableCompositionTrack *compositionMainAudioTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

then

     bResult= [compositionMainAudioTrack insertTimeRange:CMTimeRangeFromTimeToTime(kCMTimeZero,compDuration) ofTrack:assetMainAudioTrack atTime:kCMTimeZero error:&error];

 AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:compositionMainAudioTrack];

        [audioInputParams setVolume:.85 atTime:kCMTimeZero];

Not sure but you may want to use the exporter with an AVMutableComposition

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜