iOS-音頻轉(zhuǎn)換

iOS處理音頻過程中有時候需要不同格式的音頻進行轉(zhuǎn)換割卖,最近需要將m4a格式的音頻轉(zhuǎn)換成wav究珊,在網(wǎng)上搜索之后代碼整理如下:

- (void)convetM4aToWav:(NSURL *)originalUrl  destUrl:(NSURL *)destUrl {

    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:originalUrl options:nil];
    
    //讀取原始文件信息
    NSError *error = nil;
    AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset error:&error];
    if (error) {
        NSLog (@"error: %@", error);
        return;
    }
    
    AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderAudioMixOutput
                                               assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
                                               audioSettings: nil];
    if (![assetReader canAddOutput:assetReaderOutput]) {
        NSLog (@"can't add reader output... die!");
        return;
    }
    [assetReader addOutput:assetReaderOutput];
    

    AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:destUrl
                                                           fileType:AVFileTypeCoreAudioFormat
                                                              error:&error];
    if (error) {
        NSLog (@"error: %@", error);
        return;
    }
    AudioChannelLayout channelLayout;
    memset(&channelLayout, 0, sizeof(AudioChannelLayout));
    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
    NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                    [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                                    [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                    [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
                                    [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                                    [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
                                    [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                    [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
                                    nil];
    AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
                                                                               outputSettings:outputSettings];
    if ([assetWriter canAddInput:assetWriterInput]) {
        [assetWriter addInput:assetWriterInput];
    } else {
        NSLog (@"can't add asset writer input... die!");
        return;
    }
    
    assetWriterInput.expectsMediaDataInRealTime = NO;
    
    [assetWriter startWriting];
    [assetReader startReading];
    
    AVAssetTrack *soundTrack = [songAsset.tracks objectAtIndex:0];
    CMTime startTime = CMTimeMake (0, soundTrack.naturalTimeScale);
    [assetWriter startSessionAtSourceTime:startTime];
    
    __block UInt64 convertedByteCount = 0;
    
    dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
    [assetWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue
                                            usingBlock: ^
     {
         while (assetWriterInput.readyForMoreMediaData) {
             CMSampleBufferRef nextBuffer = [assetReaderOutput copyNextSampleBuffer];
             if (nextBuffer) {
                 // append buffer
                 [assetWriterInput appendSampleBuffer: nextBuffer];
                 NSLog (@"appended a buffer (%zu bytes)",
                                     CMSampleBufferGetTotalSampleSize (nextBuffer));
                 convertedByteCount += CMSampleBufferGetTotalSampleSize (nextBuffer);
                 
                 
             } else {
                 [assetWriterInput markAsFinished];
                 [assetWriter finishWritingWithCompletionHandler:^{
                     
                 }];
                 [assetReader cancelReading];
                 NSDictionary *outputFileAttributes = [[NSFileManager defaultManager]
                                                       attributesOfItemAtPath:[destUrl path]
                                                       error:nil];
                 NSLog (@"FlyElephant %lld",[outputFileAttributes fileSize]);
                 break;
             }
         }
         
     }];
}

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末取试,一起剝皮案震驚了整個濱河市怀吻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蓬坡,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萨赁,死亡現(xiàn)場離奇詭異杖爽,居然都是意外死亡,警方通過查閱死者的電腦和手機慰安,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門化焕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人查刻,你說我怎么就攤上這事。” “怎么了蝶押?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵棋电,是天一觀的道長。 經(jīng)常有香客問我企锌,道長,這世上最難降的妖魔是什么撕攒? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任抖坪,我火速辦了婚禮闷叉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蚯瞧。我一直安慰自己,他們只是感情好埋合,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布饥悴。 她就那樣靜靜地躺著西设,像睡著了一般瓣铣。 火紅的嫁衣襯著肌膚如雪贷揽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天蓖救,我揣著相機與錄音循捺,去河邊找鬼雄人。 笑死,一個胖子當(dāng)著我的面吹牛础钠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播踩萎,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼很钓,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了回还?” 一聲冷哼從身側(cè)響起叹洲,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蝗柔,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體癣丧,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡胁编,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了早直。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片市框。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡枫振,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出粪滤,到底是詐尸還是另有隱情,我是刑警寧澤肆汹,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布县踢,位于F島的核電站伟件,受9級特大地震影響议经,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜咧织,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一籍救、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧闪萄,春花似錦奇颠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至耿战,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狈涮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工歌馍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留松却,地道東北人溅话。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像飞几,于是被迫代替她去往敵國和親屑墨。 傳聞我的和親對象是個殘疾皇子躁锁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件槐秧、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,102評論 4 62
  • Cheese(奶酪)這一由牛乳忧设、山羊乳或綿羊乳經(jīng)過發(fā)酵所制成的美味,在很早以前命雀,就成為葡萄酒的完美搭檔吏砂。 Chee...
    SimplyWine閱讀 263評論 0 0
  • Markdown語法學(xué)習(xí) 目錄 Markdown語法學(xué)習(xí)語法補充Markdown簡介基本語法標(biāo)題分隔線強調(diào)鏈接圖片...
    質(zhì)數(shù)人生閱讀 1,228評論 0 5
  • A pitcher will throw the ball towards home to its home pl...
    子騏閱讀 169評論 0 0
  • 唐 · 盧照鄰 汀葭肅徂暑淀歇,江樹起初涼浪默。 水疑通織室,舟似泛仙潢纳决。 連橈渡急響乡小,鳴棹下浮光。 日晚菱歌唱胜榔,風(fēng)煙滿夕...
    水鳥千斬翼閱讀 319評論 0 0