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阅读 11,549评论 0 22
  • 最近在折腾itunes音频处理(裁剪编辑,格式转换,波形绘制) 音频的一些概念 对每个音频文件有两部分:1是文件格...
    osbornZ阅读 6,696评论 1 5
  • 前言 说到视频,大家自己脑子里基本都会想起电影、电视剧、在线视频等等,也会想起一些视频格式 AVI、MP4、RMV...
    ForestSen阅读 23,713评论 10 203
  • Linear PCM 在介绍Core Audio之前,先介绍一下最常用的非压缩数字音频格式Linear PCM(线...
    huangjun0阅读 10,148评论 0 2
  • 今天上班得知单位食堂最近新推出了蔬菜外卖服务。对于下班还要专门买菜做饭的我而言,这可是一件天大的好事,于是打算抽空...
    木青观察阅读 2,972评论 0 1