简体   繁体   English

使用AVAssetExportSession导出Wav文件

[英]Exporting Wav Files Using AVAssetExportSession

Im trying to add a fade in to a wav file and then exporting a new file with the added fade using AVAssetExportSession . 我试图将淡入添加到wav文件,然后使用AVAssetExportSession导出添加了淡入的新文件。 All the examples I have seen have seen are exporting as m4u Is it even possible to do this with wav or aif? 我所看到的所有示例都将导出为m4u甚至可以使用wav或aif进行此操作吗?

The error I get is: 我得到的错误是:

AVAssetExportSessionStatusFailed Error Domain=AVFoundationErrorDomain Code=-11822 "Cannot Open" UserInfo=0x1f01c9f0 {NSLocalizedDescription=Cannot Open, NSLocalizedFailureReason=This media format is not supported.} 

My code looks like below 我的代码如下所示

 NSString *inpath = [path stringByAppendingFormat:@"/%@",file];

    NSString *ename = [file stringByDeletingPathExtension];
    NSString *incname = [ename stringByAppendingString:@"1t"];
    NSString *outname = [incname stringByAppendingPathExtension:@"wav"];
    NSString *outpath = [path stringByAppendingFormat:@"/%@",outname];

    NSURL *urlpath = [NSURL fileURLWithPath:inpath];
    NSURL *urlout = [NSURL fileURLWithPath:outpath];



    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                                        forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:urlpath options:options];


    //check the soundfile is greater than 50seconds
    CMTime assetTime = [anAsset duration];
    Float64 duration = CMTimeGetSeconds(assetTime);
    if (duration < 50.0) return NO;

    // get the first audio track
    NSArray *tracks = [anAsset tracksWithMediaType:AVMediaTypeAudio];
    if ([tracks count] == 0) return NO;

    AVAssetTrack *track = [tracks objectAtIndex:0];

    // create trim time range - 20 seconds starting from 30 seconds into the asset
    CMTime startTime = CMTimeMake(30, 1);
    CMTime stopTime = CMTimeMake(50, 1);
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    // create fade in time range - 10 seconds starting at the beginning of trimmed asset
    CMTime startFadeInTime = startTime;
    CMTime endFadeInTime = CMTimeMake(40, 1);
    CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime,
                                                            endFadeInTime);

    // setup audio mix
    AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
    AVMutableAudioMixInputParameters *exportAudioMixInputParameters =
    [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];

    exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters];

    AVAssetExportSession *exportSession = [AVAssetExportSession
                                           exportSessionWithAsset:anAsset presetName:AVAssetExportPresetPassthrough];


    //NSArray *listof = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
    //NSLog(@"LISTOF %@",listof);

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

    NSString *fileType = nil;
    NSString *ex = nil;

    switch (formatID) {

        case kAudioFormatLinearPCM:
        {
            UInt32 flags = audioDesc->mFormatFlags;
            if (flags & kAudioFormatFlagIsBigEndian) {
                fileType = @"public.aiff-audio";
                ex = @"aif";
            } else {
                fileType = @"com.microsoft.waveform-audio";
                ex = @"wav";
            }
        }
            break;

        case kAudioFormatMPEGLayer3:
            fileType = @"com.apple.quicktime-movie";
            ex = @"mp3";
            break;

        case kAudioFormatMPEG4AAC:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;

        case kAudioFormatAppleLossless:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;

        default:
            break;
    }



    exportSession.outputFileType = fileType;
    exportSession.outputURL = urlout;

    //exportSession.outputFileType = AVFileTypeWAVE; // output file type
    exportSession.timeRange = exportTimeRange; // trim time range
    exportSession.audioMix = exportAudioMix; // fade in audio mix


    // perform the export
    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (AVAssetExportSessionStatusCompleted == exportSession.status) {
            NSLog(@"AVAssetExportSessionStatusCompleted");
        } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
            // a failure may happen because of an event out of your control
            // for example, an interruption like a phone call comming in
            // make sure and handle this case appropriately
            NSLog(@"AVAssetExportSessionStatusFailed %@",exportSession.error);
        } else {
            NSLog(@"Export Session Status: %d", exportSession.status);
        }
    }];

    return YES;
    }

You can't do that with AVAssetExportSession because the presets are quite fixed in their usage. 您无法使用AVAssetExportSession做到这一点,因为这些预设在用途上是相当固定的。 A preset value of AVAssetExportPresetPassthrough will keep your input formats on output. 预设值AVAssetExportPresetPassthrough将使您的输入格式保留在输出中。

As your task will be manipulating the audio sample buffers directly you should use the second variant that AVFoundation will give you: paired AVAssetReader and AVAssetWriter setup. 由于您的任务将直接操作音频样本缓冲区,因此您应该使用AVFoundation会给您的第二种变体:成对的AVAssetReader和AVAssetWriter设置。 You'll find proper sample code as in AVReaderWriterOSX from Apple developer source. 您可以从Apple开发人员源中的AVReaderWriterOSX中找到正确的示例代码。 This should also work with iOS besides you have different I/O format settings available. 除了可以使用不同的I / O格式设置之外,此功能还可以在iOS上使用。 The availability to decompress audio as PCM and write back to uncompressed .wav file should be given. 应该提供将音频解压缩为PCM并写回未压缩的.wav文件的可用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM