用oc方法通过AVFoundation库把mp3文件提取pcm文件

+ (void)mp3ToPCMWithMp3File:(NSString*)mp3FilePath  outPutPCMPath:(NSString*)outPutPCMPath mp3ToPcmComplete:(Mp3ToPcmComplete)mp3ToPcmComplete {

    AVAsset*asset = [OCmp3ToPCMreadMp3FileWithMp3File:mp3FilePath];

    AVAssetReader*assetReader = [OCmp3ToPCMinitAssetReaderWithAsset:asset];


    AudioChannelLayoutchannelLayout;

    memset(&channelLayout,0,sizeof(channelLayout));

    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

    NSDictionary*outputSettings =@{AVFormatIDKey:@(kAudioFormatLinearPCM),    // 音频格式

                                     AVSampleRateKey:@(44100),    // 采样率

//                                    AVSampleRateKey : @(22050),    // 采样率

                                     AVNumberOfChannelsKey:@(2),    // 通道数 1 || 2

                                     AVChannelLayoutKey: [NSDatadataWithBytes:&channelLayoutlength:sizeof(channelLayout)],  // 声音效果(立体声)

                                     AVLinearPCMBitDepthKey:@(16),  // 音频的每个样点的位数

                                     AVLinearPCMIsNonInterleaved:@NO,  // 音频采样是否非交错

                                     AVLinearPCMIsFloatKey:@NO,    // 采样信号是否浮点数

                                     AVLinearPCMIsBigEndianKey:@NO// 音频采用高位优先的记录格式

                                     };

    AVAssetReaderAudioMixOutput *readerAudioMixOutput = [[AVAssetReaderAudioMixOutput alloc] initWithAudioTracks:asset.tracks audioSettings:outputSettings];

    if(![assetReadercanAddOutput:readerAudioMixOutput]) {

        NSLog(@"can't add readerAudioMixOutput");

        return;

    }

    [assetReaderaddOutput:readerAudioMixOutput];


    AVAssetWriter*assetWriter = [OCmp3ToPCMinitAssetWriterWithPCMName:outPutPCMPath];


    if(![assetWritercanApplyOutputSettings:outputSettingsforMediaType:AVMediaTypeAudio]) {

        NSLog(@"can't apply outputSettings");

        return;

    }


    AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:outputSettings];

    writerInput.expectsMediaDataInRealTime = NO;


    if(![assetWritercanAddInput:writerInput]) {

        NSLog(@"can't add writerInput");

        return;

    }


    [assetWriteraddInput:writerInput];


    [assetReaderstartReading];

    [assetWriterstartWriting];


    AVAssetTrack *track = asset.tracks.firstObject;

    if(!track) {

        return;

    }


    CMTimestartTime =CMTimeMakeWithSeconds(0, track.naturalTimeScale);

    [assetWriterstartSessionAtSourceTime:startTime];


    dispatch_queue_t mediaInputQueue = dispatch_queue_create([@"mediaInputQueue" cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);

    [writerInputrequestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{

        while(writerInput.isReadyForMoreMediaData) {

            CMSampleBufferRefnextBuffer = readerAudioMixOutput.copyNextSampleBuffer;

            if(nextBuffer) {

                [writerInputappendSampleBuffer:nextBuffer];

            }else{

                [writerInputmarkAsFinished];

                [assetReadercancelReading];

                [assetWriterfinishWritingWithCompletionHandler:^{

                    NSLog(@"mp3转pcm完成");

                    if(mp3ToPcmComplete) {

                        mp3ToPcmComplete();

                    }

                }];

                break;

            }

        }

    }];


}

+ (AVAsset*)readMp3FileWithMp3File:(NSString*)mp3FilePath {

    NSURL*fileURL = [NSURLfileURLWithPath:mp3FilePath];

    AVAsset*asset = [AVAssetassetWithURL:fileURL];

    returnasset;

}

+ (AVAssetReader*)initAssetReaderWithAsset:(AVAsset*)asset {

    NSError*error;

    AVAssetReader*assetReader;

    assetReader = [[AVAssetReaderalloc]initWithAsset:asseterror:&error];

    returnassetReader;

}

+ (AVAssetWriter*)initAssetWriterWithPCMName:(NSString*)outPutPCMPath {

    NSError*error;

    AVAssetWriter*assetWriter;

    NSURL*outPutURL = [NSURLfileURLWithPath:outPutPCMPath];

    assetWriter = [[AVAssetWriteralloc]initWithURL:outPutURLfileType:AVFileTypeWAVEerror:&error];

    returnassetWriter;

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容