1 内容介绍
1,本文主要介绍如何使用lame库,将pcm文件编码为mp3文件,不使用ide.
2,环境为macos.各个环境的代码都是一样的,主要是编译库的脚本不一样.
3,打算写一个整合了ios,android,centos,windows,macos上的编译脚本,发布为另一篇文章.
2 编译lame库
1,编译lame库的脚本,将在整理之后发布出来.
2,暂时可以先下载已经编译好的版本,for macos!
https://pan.baidu.com/s/11wyA7QuYuB6YUd-FyqYgcg?pwd=j0f9
3 编写源代码main.c文件
#include
#include
#include
FILE *pcmFile;
FILE *mp3File;
lame_t lameClient;
int init(char *pcmFilePath, char *mp3FilePath, int sampleRate, int channels, int bitRate);
void encode();
void destroy();
int main(int argc, char *argv[])
{
//输入参数.
char *pcmFilePath, *mp3FilePath;
int sampleRate = 44100;
int channels = 2;
int bitRate = 44100 * 2 * 8;
printf("There are %d params\n", argc);
for (int i = 0; i < argc; i++)
{
printf("param %d:%s\n", i, argv[i]);
if (i == 1)
{
pcmFilePath = argv[1];
}
else if (i == 2)
{
mp3FilePath = argv[2];
}
}
int ret = init(pcmFilePath, mp3FilePath, sampleRate, channels, bitRate);
if (ret)
{
printf("error!");
exit(ret);
}
encode();
destroy();
return 0;
}
int init(char *pcmFilePath, char *mp3FilePath, int sampleRate, int channels, int bitRate)
{
int ret = -1;
pcmFile = fopen(pcmFilePath, "rb");
if (!pcmFile)
{
printf("pcm file open failed!\n");
return -1;
}
mp3File = fopen(mp3FilePath, "wb");
if (!mp3File)
{
printf("mp3 file open failed!\n");
return -1;
}
lameClient = lame_init();
lame_set_in_samplerate(lameClient, sampleRate);
lame_set_out_samplerate(lameClient, sampleRate);
lame_set_num_channels(lameClient, channels);
lame_set_brate(lameClient, bitRate / 1000);
lame_init_params(lameClient);
ret = 0;
return ret;
}
void encode()
{
//一次操作256KB大小的数据.
int bufferSize = 1024 * 256;
//一个short是2个字节.
short *buffer = calloc(bufferSize / 2, sizeof(short));
short *leftBuffer = calloc(bufferSize / 4, sizeof(short));
short *rightBuffer = calloc(bufferSize / 4, sizeof(short));
unsigned char *mp3Buffer = (unsigned char *)malloc(bufferSize);
size_t readBufferSize = 0;
// 一次读bufferSize/2个2个字节的数据==读一个bufferSize的数据.
// 每次读一个float,左右声道交错存储.
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), mp3Buffer, bufferSize);
//写入文件
fwrite(mp3Buffer, 1, wroteSize, mp3File);
}
free(buffer);
free(leftBuffer);
free(rightBuffer);
free(mp3Buffer);
}
void destroy()
{
if (pcmFile)
{
fclose(pcmFile);
}
if (mp3File)
{
fclose(mp3File);
lame_close(lameClient);
}
}
4 编译
如果是clang编译器:
clang -L./lame-3.100/lib/ -I./lame-3.100/include -lmp3lame main.c
如果是gcc编译器:
gcc -L./lame-3.100/lib/ -I./lame-3.100/include -lmp3lame main.c
两个编译器的命令是一致的.
1,-lxxx,自动寻找libxxx.a,或者libxxx.so
2.-Lxxx,表示添加库搜索路径.
3,-Ixxx,表示增加头文件搜索路径.
4,库搜索路径包含软链时,ld提示文件夹找不到.
5,没有指定输出文件名,默认的输出结果为a.out
5 运行
./a.out timeless.pcm timeless.mp3
1,timeless.pcm为输入的pcm文件,timeless.mp3为输出mp3文件.
2,注意pcm文件必须为44100的采样率,双声道,采样深度为16bit.
[图片上传失败...(image-13fa3e-1661690480718)]
测试音频文件也提供了
https://pan.baidu.com/s/1Vdoo2ieEump-zANcXLab5g?pwd=bw4w
该音频文件为timeless.pcm,44100采样率,双声道,采样深度为16bit.
6 总结
1,timeless.mp3,是小编最喜欢的歌曲之一,时间是最宝贵的财富,希望我们都能珍惜当下.
2,这个系列好久之前就想写了,拖了好久.
3,最后本人的公众号,不定期更新小编原创文章,希望能得到各位的关注.