Convert To Mp3

當我們在進行音頻處理的時候,我們能夠進行編碼成為 .caf格式,但是 .mp3 才是更方便和更為流行的格式。當然主要原因是 .caf 5M/minute 的文件大小讓我們無可奈何,擁有高壓縮率的 .mp3站出來了。

當然,一個原因是Apple 眾多文件格式中,包括MPEG-1 (.mp3), MPEG-2 ADTS (.aac), AIFF, CAF, and WAVE。最重要的事是你可以僅僅使用CAF,因為它能包含任何iphone支持的編碼格式的數據,在iPhone上面它是推薦的文件格式。

功能所需,我們需要輸出 .mp3 文件,OK,AVFoundation framework:

-(void)handleExportTapped :(NSURL *)assetURL
{
  AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
  AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
                                    initWithAsset: songAsset
                                    presetName: AVAssetExportPresetAppleM4A];
  NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
  exporter.outputFileType = @"com.apple.m4a-audio";
  NSString *exportFile = [myDocumentsDirectory() stringByAppendingPathComponent: @"exported.m4a"];
   NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
   [exporter exportAsynchronouslyWithCompletionHandler:^{
  AVAssetExportSessionStatus status = [exporter status];
  switch (status)
  {
    case AVAssetExportSessionStatusCompleted:
    {
      NSLog(@"export is ok");
      break;
    }
    case AVAssetExportSessionStatusFailed:
    {
      break;
    }
    default:
    {
      break;
    }
  }
}];

  }

導出音頻文件,利用支持的 AVAssetExportPresetAppleM4A 編碼最后轉換成mp3,然并卵!那么問題來了,事實上在Apple 框架支持中不支持mp3的編碼,只有解碼,在蘋果官方ipod,itunes中是大力支持AAC格式編碼的,當然為了推廣卻沒有被人們所熟悉。


最后,我們只能通過第三方的框架LameMP3Encoder去轉碼,LAME是高質量的MPEG音頻層III(MP3)編碼器,mp3的流行也是LAME算法的一個功勞。使用方法如下所示:

  • 導入libmp3lame.a靜態庫和lame.h頭文件
- (void)cafToMp3:(NSString*)cafFileName
{
NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
_mp3FilePath = [docsDir stringByAppendingPathComponent:@"record.mp3"];

@try {
int read, write;
FILE *pcm = fopen([cafFileName cStringUsingEncoding:1], "rb");
FILE *mp3 = fopen([_mp3FilePath cStringUsingEncoding:1], "wb");
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];

lame_t lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);

do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);

fwrite(mp3_buffer, write, 1, mp3);

} while (read != 0);

lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
//Detrming the size of mp3 file
NSFileManager *fileManger = [NSFileManager defaultManager];
NSData *data = [fileManger contentsAtPath:_mp3FilePath];
NSString* str = [NSString stringWithFormat:@"%d K",[data length]/1024];
NSLog(@"size of mp3=%@",str);

}
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 視頻編碼與封裝方式詳解 1.編碼方式和封裝格式 2.視頻編碼標準兩大系統 MPEG-1 MPEG-2 MPEG-3...
    latthias閱讀 6,417評論 0 22
  • 最近在折騰itunes音頻處理(裁剪編輯,格式轉換,波形繪制) 音頻的一些概念 對每個音頻文件有兩部分:1是文件格...
    osbornZ閱讀 2,194評論 1 5
  • 前言 說到視頻,大家自己腦子里基本都會想起電影、電視劇、在線視頻等等,也會想起一些視頻格式 AVI、MP4、RMV...
    ForestSen閱讀 23,376評論 10 203
  • Linear PCM 在介紹Core Audio之前,先介紹一下最常用的非壓縮數字音頻格式Linear PCM(線...
    huangjun0閱讀 4,414評論 0 2
  • 今天上班得知單位食堂最近新推出了蔬菜外賣服務。對于下班還要專門買菜做飯的我而言,這可是一件天大的好事,于是打算抽空...
    木青觀察閱讀 395評論 0 1