用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),? ? // 通道數(shù) 1 || 2

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVChannelLayoutKey: [NSDatadataWithBytes:&channelLayoutlength:sizeof(channelLayout)],? // 聲音效果(立體聲)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMBitDepthKey:@(16),? // 音頻的每個樣點的位數(shù)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsNonInterleaved:@NO,? // 音頻采樣是否非交錯

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsFloatKey:@NO,? ? // 采樣信號是否浮點數(shù)

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AVLinearPCMIsBigEndianKey:@NO// 音頻采用高位優(yōu)先的記錄格式

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

? ? 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轉(zhuǎn)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;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容