音视频开发进阶-学习笔记3-使用LAME编码mp3文件

语言: CN / TW / HK

1.编码工具类 Mp3Encoder.hpp & Mp3Encoder.cpp

Mp3Encoder.hpp 头文件代码:

//
//  Mp3Encoder.hpp
//  RFAVP
//
//  Created by 庞日富 on 2021/1/25.
//

#ifndef Mp3Encoder_hpp
#define Mp3Encoder_hpp

#include <stdio.h>

#import "lame.h"

// 声明一个类:Mp3Encoder ,
// Mp3Encoder 类提供一个公开方法:
class Mp3Encoder
{

    
private:
    FILE* pcmFile;
    FILE* mp3File;
    lame_t lameClient;


public:
    
//    Mp3Encoder(); //方法未实现,先注释
//    ~Mp3Encoder(); //方法未实现,先注释
    int Init(const char* pcmFilePath, const char* mp3FilePath, int sampleRate , int chanels, int bitRate);
    void encode();
    void destory();
    
};
    
    
#endif /* Mp3Encoder_hpp */

复制代码

Mp3Encoder.cpp 实现文件代码:

//
//  Mp3Encoder.cpp
//  RFAVP
//
//  Created by 庞日富 on 2021/1/25.
//

#include "Mp3Encoder.hpp"



int Mp3Encoder::Init(const char *pcmFilePath, const char *mp3FilePath, int sampleRate, int chanels, int bitRate){
    int ret = -1;
    pcmFile = fopen(pcmFilePath, "rb");
    
    if (pcmFile) {
        mp3File = fopen(mp3FilePath, "wb");
        if (mp3File) {
            lameClient = lame_init();
            lame_set_in_samplerate(lameClient, sampleRate);
            lame_set_out_samplerate(lameClient, sampleRate);
            lame_set_num_channels(lameClient, chanels);
            lame_set_brate(lameClient, bitRate/1000);
            lame_init_params(lameClient);
            ret = 0;
        }
    }
    return ret;
}

void Mp3Encoder::encode() {
    
    int bufferSize = 1024 * 256;
    short* buffer = new short[bufferSize / 2];
    short* leftBuffer = new short[bufferSize / 4];
    short* rightBuffer = new short[bufferSize / 4];
    unsigned char* mp3_buffer = new unsigned char[bufferSize];
    size_t readBufferSize = 0;
    

    
    while ((readBufferSize = fread(buffer, 2, bufferSize / 2, pcmFile)) > 0) {
            for (int i = 0; i < readBufferSize; i++) {
                if (i % 2 == 0) {
                    leftBuffer[i / 2] = buffer[i];
                } else {
                    rightBuffer[i / 2] = buffer[i];
                }
            }
            size_t wroteSize = lame_encode_buffer(lameClient, (short int *) leftBuffer, (short int *) rightBuffer, (int)(readBufferSize / 2), mp3_buffer, bufferSize);
            fwrite(mp3_buffer, 1, wroteSize, mp3File);
        }
    
    delete [] buffer;
    delete [] leftBuffer;
    delete [] rightBuffer;
    delete [] mp3_buffer;

}



void Mp3Encoder::destory(){
    if (pcmFile) {
        fclose(pcmFile);
    }
    
    if (mp3File) {
        fclose(mp3File);
        lame_close(lameClient);
    }
}

复制代码

iOS集成

FirstVC.mm 文件中调用编码工具类的具体用法

//
//  FirstVC.m
//  RFAVP
//
//  Created by PRF on 2021/1/22.
//

#import "FirstVC.h"
#import <Masonry/Masonry.h>


#include "Mp3Encoder.hpp"
#import "CommonUtil.h"


@interface FirstVC ()

@property (nonatomic,strong) UIButton *EncodeBtn;

@end

@implementation FirstVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"音视频进阶";
    [self.EncodeBtn addTarget:self action:@selector(encodeClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)encodeClick:(UIButton *)btn{

    NSLog(@"按钮点击事件");
    Mp3Encoder *encoder = new Mp3Encoder();
    // 源文件的的路径
    const char* pcmFilePath = [[CommonUtil bundlePath:@"vocal.pcm"] cStringUsingEncoding:NSUTF8StringEncoding];
    // 要生成的mp3文件的路径
    const char *mp3FilePath = [[CommonUtil documentsPath:@"vocal.mp3"] cStringUsingEncoding:NSUTF8StringEncoding];
    int sampleRate = 44100;
    int channels = 2;
    int bitRate = 128 * 1024;
    // 初始化解码器,传入源文件路径,生成的文件路径,采样频率,声道数,码率
    encoder->Init(pcmFilePath, mp3FilePath, sampleRate, channels, bitRate);
    // 编码
    encoder->encode();
    //关闭文件
    encoder->destory();
    
    delete encoder;
    
    NSLog(@"Encode Success");
    NSLog(@"file save path : %s",mp3FilePath);
    
}


-(UIButton *)EncodeBtn{
    if (!_EncodeBtn) {
        _EncodeBtn = [UIButton new];
        [self.view addSubview:_EncodeBtn];
        [_EncodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(50);
        }];
        _EncodeBtn.backgroundColor = [UIColor blueColor];
        [_EncodeBtn setTitle:@"音视频测试" forState:UIControlStateNormal];
    }
    return _EncodeBtn;
}


@end

复制代码

源码地址:

LAME_Demo