使用lame将.caf录音转.MP3格式

2018-04-25  本文已影响26人  任兴金
//首先在录音过程中,需要对录音参数进行设置
    NSMutableDictionary *settingDic = [[NSMutableDictionary alloc] init];
    //设置录音格式
    [settingDic setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
    [settingDic setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
    //录音通道数  1 或 2 
    [settingDic setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    //线性采样位数  8、16、24、32
    [settingDic setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    //录音的质量
    [settingDic setValue:[NSNumber numberWithInt:AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];

lame地址
https://github.com/hedgehogIda/caf-mp3
下载完成直接拖到项目中就可以,包含图文件lame.h,然后再需要转MP3的地方调用如下方法:

//caf文件地址
-(NSString*)toMP3{
     //caf音频地址
NSString * cafPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/CDPAudioFiles/CDPAudioRecord.caf"];
    //转MP3的地址
    NSString *mp3FileName = @"Mp3File";
    mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
    NSString *mp3FilePath = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName];
    
    @try {
        int read, write;
        
        FILE *pcm = fopen([cafPath cStringUsingEncoding:1], "rb");  //source
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output
        
        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_set_num_channels(lame, 2);
        lame_set_brate(lame,16);
        lame_set_mode(lame,3);
        lame_set_quality(lame,1); /* 2=high 5 = medium 7=low 音质*/
        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 {
       return mp3FilePath;
        NSLog(@"转换成功");
    }
    return @"";
}

问题分析:
录音完转MP3声音严重失真?

本人尝试按上面代码中的设置是没有问题的,具体每个参数对音频质量的影响我也是菜鸟
 1.caf录音的设置保持与转换的基本一致
 2.通道双通道
 3.录音质量调高

录制完成后声音太小

录制过程中,设置AudioSession会话类别
-(void)setAudioSessionCategory:(NSString *)category{
   NSError *sessionError;

   [_session setCategory:category error:&sessionError];
   
   //启动音频会话管理
   if(_session==nil||sessionError){
       DLog(@"CDPAudioRecorder设置AVAudioSession会话类别Category错误:%@",sessionError);
   }else{
       [_session setActive:YES error:nil];
   }
}
录制结束后,再这样设置一下
//录音结束
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
   if (flag) {
       [_session setActive:NO error:nil];
       [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
       DLog(@"CDPAudioRecorder录音完成,文件大小为%@",[CDPAudioRecorder fileSizeAtPath:_recordURL.path]);
   }
}

上一篇下一篇

猜你喜欢

热点阅读