录音文件lame转换MP3相关配置


文件下载整个功能完成了,那么对应的文件上传也跑不了。So~ Look here~



业务需求是录制音频然后上传到七牛并且Android可以读。

与安卓沟通了一下统一了mp3格式,大小质量都不错。由于AVAudioRecorder录音的格式为.caf或者.wav而且很大需要进行转换压缩为MP3格式。这里需要用到三方库 lame

使用lame转换后音频的质量和

 _recorder = [[AVAudioRecorder alloc] initWithURL:_recordFilePath settings:setting error:NULL];

里的 setting 息息相关。 所以整理了两个配置。

我把这两种的配置写在了工具类所以直接贴代码了~要用的话直接CV大法就可以。

lame三方库的资源


  • 获取转换文件所在文件夹
+ (NSString *)getRecPathUrl{
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *recordDir = [str stringByAppendingPathComponent:@"RecordCourse"];

    return recordDir;
}
  • 获取时间戳用于文件的命名
+ (NSString *)getTimestamp{
    NSDate *nowDate = [NSDate date];
    double timestamp = (double)[nowDate timeIntervalSince1970]*1000;
    long nowTimestamp = [[NSNumber numberWithDouble:timestamp] longValue];
    NSString *timestampStr = [NSString stringWithFormat:@"%ld",nowTimestamp];
    return timestampStr;
}
  • PCM转换MP3配置
+ (NSDictionary *)getAudioSettingWithPCM {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //设置录音格式
    [dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
    //设置录音采样率,8000是电话采样率,对于一般录音已经够了
    [dic setObject:@(44100.0) forKey:AVSampleRateKey];
    //设置通道,这里采用双声道
    [dic setObject:@(1) forKey:AVNumberOfChannelsKey];
    //每个采样点位数,分为8、16、24、32
    [dic setObject:@(16) forKey:AVLinearPCMBitDepthKey];
    //是否使用浮点数采样
    [dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
    //....其他设置等
    return dic;
}
  • CAF转换MP3配置
+ (NSDictionary *)getAudioSettingWithCAF {
    NSDictionary *setting = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithInt:AVAudioQualityMin],
                             AVEncoderAudioQualityKey,
                             [NSNumber numberWithInt:16],
                             AVEncoderBitRateKey,
                             [NSNumber numberWithInt:2],
                             AVNumberOfChannelsKey,
                             [NSNumber numberWithFloat:44100.0],
                             AVSampleRateKey,
                             nil];

    return setting;
}
  • PCM转换MP3的lame方法
+ (NSString *)audioPCMtoMP3:(NSString *)wavPath {
    NSString *cafFilePath = wavPath;

     大专栏  录音文件lame转换MP3相关配置NSString *mp3FilePath = [NSString stringWithFormat:@"%@.mp3",[NSString stringWithFormat:@"%@%@",[cafFilePath substringToIndex:cafFilePath.length - 4],[self getTimestamp]]];

    NSFileManager* fileManager = [NSFileManager defaultManager];
    if([fileManager removeItemAtPath:mp3FilePath error:nil]){
        NSLog(@"删除原MP3文件");
    }
    @try {
        int read, write;
        FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置
        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, 22050.0);
        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 {
        return mp3FilePath;
    }
}
  • CAF转换MP3的lame方法
+ (NSString *)audioCAFtoMP3:(NSString *)wavPath {

    NSString *cafFilePath = wavPath;

    NSString *mp3FilePath = [NSString stringWithFormat:@"%@.mp3",[NSString stringWithFormat:@"%@%@",[cafFilePath substringToIndex:cafFilePath.length - 4],[self getTimestamp]]];

    @try {
        int read, write;

        FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置

        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_num_channels(lame,1);//设置1为单通道,默认为2双通道
        lame_set_in_samplerate(lame, 44100.0);
        lame_set_VBR(lame, vbr_default);

        lame_set_brate(lame,8);

        lame_set_mode(lame,3);

        lame_set_quality(lame,2);

        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;
    }
}

两种质量大小不错都可以使用????

原文地址:https://www.cnblogs.com/lijianming180/p/12326572.html

时间: 2024-07-30 03:09:40

录音文件lame转换MP3相关配置的相关文章

xcode 中运用lame进行caf文件到mp3文件的转换

需首先引用lamp.h,libmp3lame.a - (void)audio_PCMtoMP3:(NSString *) cafFilePath ToMp3File:(NSString *) mp3FilePath { NSFileManager* fileManager=[NSFileManager defaultManager]; if([fileManager removeItemAtPath:mp3FilePath error:nil]) { NSLog(@"删除"); } @

spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE b

MP3 Lame 转换 参数 设置(转)

我们在对音频格式的转换中,打交道最多的就是MP3了.如果你能彻底玩转MP3,那么对你的音频创作和对其他音频格式的掌握会有很大的帮助.下面我们给大家介绍MP3制作软件:LAME 要制作出高音质的MP3靠以前广为流传的MP3编码器是不行的.LAME与一般MP3编码器与众不同,它支持几乎所有能够采用到MP3编码中的技术,LAME支持CBR(固定码率)和VBR(动态码率,还有一个效果不是很出众的ABR),LAME是MP3史上具有里程碑意义的软件,LAME是一个Command line程序,象Dos程序一

ios开发之Info.plist文件相关配置

前言:在iOS开发中有些情况下需要对Info.plist文件进行配置,以下介绍几种相关配置.以后遇到需要配置的再更新... 开发环境:swift3.0.1,Xcode8.1 一,项目中需要使用第三方字体 1,打开Info.plist文件选中Information Property List选择加号添加一条记录Fonts provided by application(区分大小写) 2,这是一个数组然后在其item中添加已拷贝在本项目中的字体文件名称. 二,项目中需要使用调用手机的图库 添加key

SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

1.概念:Feign 接口服务 2.具体内容 现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例: Dept dept = this.restTemplate .exchange(DEPT_GET_URL + id, HttpMethod.GET, new HttpEntity<Object>(this.headers), Dept.class) .getBody(); 所有的数据的调用和转换都必须由用户自己来完成,而我们本身不擅长这些,我们习惯的

apache的.htaccess文件作用和相关配置

首先.htaccess什么? .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. 当我们使用apache部署一个网站代码准备部署到网上的时候,我们手中的apache的httpd.conf大家肯定都知道.这是apache的配置文件,然而我们大多数的网站都是基于云服务器来部署的,还有就是团队协作开发的时候,我们很难直接修改公共的httpd.conf,这时 .htaccess就是httpd.conf的衍生品,它起着和httpd.conf相同的作用. .htaccess的

maven资源文件的相关配置

https://www.cnblogs.com/pixy/p/4798089.html https://blog.csdn.net/u011900448/article/details/78281269 https://www.cnblogs.com/hi-feng/p/7892724.html maven资源文件的相关配置构建Maven项目的时候,如果没有进行特殊的配置,Maven会按照标准的目录结构查找和处理各种类型文件.src/main/java和src/test/java 这两个目录中的

Sox语音转换的相关知识

SoX-linux 里操作音频的瑞士军刀 Sox是最为著名的Open Source声音文件 格式转换工具.已经被广泛移植到Dos.windows.OS2.Sun.Next.Unix.Linux等多个操作系统 平台.Sox项目是由Lance Norskog创立的,后来被众多的开发 者逐步完善,现在已经能够支持很多种声音文件格式和声音处理效果.基本上常见的声音格式都能够支持.更加有用的是,Sox能够进行声音滤波.采样频率转换,这对那些从事声讯平台开发或维护的朋友非常有用.当然,Sox里面也包括一些D

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be