美文网首页
用oc方法通过AVFoundation库把mp3文件提取pcm文

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

作者: 嘿嘿和露红叶 | 来源:发表于2018-06-23 17:48 被阅读39次

    + (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;

    }

    相关文章

      网友评论

          本文标题:用oc方法通过AVFoundation库把mp3文件提取pcm文

          本文链接:https://www.haomeiwen.com/subject/rgkdyftx.html