GithubHelp home page GithubHelp logo

yinlong0918 / itaudio.pcm.to.mp3.ios Goto Github PK

View Code? Open in Web Editor NEW

This project forked from foolsparadise/itaudio.pcm.to.mp3.ios

0.0 0.0 0.0 937 KB

iOS 录音后的音频文件,转码,转成mp3格式,pcm->mp3, wav->mp3

License: MIT License

C 100.00%

itaudio.pcm.to.mp3.ios's Introduction

audioPCMtoMP3

iOS 录音后的音频文件,转码,转成mp3格式,pcm->mp3, wav->mp3,使用以下方法

usage:

#import "lame.h"
-(NSString *)audioPCMtoMP3:(NSString *)wavPath toFile:(NSString *)mp3Path
{
    // wavPath 被转换的音频文件位置,
    // 比如这样:/var/mobile/Containers/Data/Application/AA6D7D77-089E-4F93-84CF-4E944EA94298/Library/Caches/a.wav
    // mp3Path 输出生成的Mp3文件位置,
    // 比如这样:/var/mobile/Containers/Data/Application/AA6D7D77-089E-4F93-84CF-4E944EA94298/Library/Caches/b.mp3
    // 另外要注意,码率的设置
    /*
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    [paths1 objectAtIndex:0]
    NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    [paths2 objectAtIndex:0]
    */
    @try {
        int read, write;
        
        FILE *pcm = fopen(wavPath.UTF8String, "rb");
        fseek(pcm, 4*1024, SEEK_CUR); //skip file header
        FILE *mp3 = fopen(mp3Path.UTF8String, "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.0); //注意这里的码率设置,要与wavPath文件的码率保持一致
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);
        
        do {
            read = (int)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 {
        NSLog(@"MP3生成成功");
    }
}

itaudio.pcm.to.mp3.ios's People

Contributors

foolsparadise avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.