mp3lame linux 编译,linux下lame编程:wav转MP3示例代码
1. wav转MP3示例程序代码:
/*
gcc -I /usr/include/lame/ lame_test.c -lmp3lame -o lame_test -lm
*/#include
#include
#include
#define INBUFSIZE 4096
#define MP3BUFSIZE (int) (1.25 * INBUFSIZE) + 7200
int encode(char* inPath, char* outPath) {
int status = 0;
lame_global_flags* gfp;
int ret_code;
FILE* infp;
FILE* outfp;
short* input_buffer;
int input_samples;
char* mp3_buffer;
int mp3_bytes;
gfp = lame_init();
if (gfp == NULL) {
printf("lame_init failed/n");
status = -1;
goto exit;
}
ret_code = lame_init_params(gfp);
if (ret_code < 0) {
printf("lame_init_params returned %d/n",ret_code);
status = -1;
goto close_lame;
}
infp = fopen(inPath, "rb");
outfp = fopen(outPath, "wb");
input_buffer = (short*)malloc(INBUFSIZE*2);
mp3_buffer = (char*)malloc(MP3BUFSIZE);
do{
input_samples = fread(input_buffer, 2, INBUFSIZE, infp);
//fprintf(stderr, "input_samples is %d./n", input_samples);
mp3_bytes = lame_encode_buffer_interleaved(gfp, input_buffer,input_samples/2, mp3_buffer, MP3BUFSIZE);
//fprintf(stderr, "mp3_bytes is %d./n", mp3_bytes);
if (mp3_bytes < 0) {
printf("lame_encode_buffer_interleaved returned %d/n", mp3_bytes);
status = -1;
goto free_buffers;
} else if(mp3_bytes > 0) {
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
}while (input_samples == INBUFSIZE);
mp3_bytes = lame_encode_flush(gfp, mp3_buffer, sizeof(mp3_buffer));
if (mp3_bytes > 0) {
printf("writing %d mp3 bytes/n", mp3_bytes);
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
free_buffers:
free(mp3_buffer);
free(input_buffer);
fclose(outfp);
fclose(infp);
close_lame:
lame_close(gfp);
exit:
return status;
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("usage: lame_test rawinfile mp3outfile/n");
}
encode(argv[1], argv[2]);
return 0;
}
2. 录音成MP3格式程序代码:
/*
gcc -I /usr/include/lame/ -lmp3lame -o mp3_record mp3_record.c -lm
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "lame.h"
#define BUFF_SIZE 512
#define INBUFF_SIZE 4096
#define MP3BUFF_SIZE (int) (1.25 * INBUFF_SIZE) + 7200
static int run=1;
static void stop(int signum){
fprintf(stderr, "exit/n");
run=0;
}
int SetFormat(int fd, unsigned int bit, unsigned int channel, unsigned int hz)
{
int ioctl_val;
/* set bit format */
ioctl_val = bit;
if(ioctl(fd, SNDCTL_DSP_SETFMT, &ioctl_val) < 0){
fprintf(stderr, "Set fmt to bit %d failed:%s/n", bit, strerror(errno));
return -1;
}
if (ioctl_val != bit) {
fprintf(stderr, "do not support bit %d, supported %d/n", bit,ioctl_val);
return (-1);
}
/*set channel */
ioctl_val = channel;
if ((ioctl(fd, SNDCTL_DSP_CHANNELS, &ioctl_val)) == -1) {
fprintf(stderr, "Set Audio Channels %d failed:%s/n", channel, strerror(errno));
return (-1);
}
if (ioctl_val != channel) {
fprintf(stderr, "do not support channel %d,supported %d/n", channel, ioctl_val);
return (-1);
}
/*set speed */
ioctl_val = hz;
if (ioctl(fd, SNDCTL_DSP_SPEED, &ioctl_val) == -1) {
fprintf(stderr, "Set speed to %d failed:%s/n", hz, strerror(errno));
return (-1);
}
if (ioctl_val != hz) {
fprintf(stderr, "do not support speed %d,supported is %d/n", hz,ioctl_val);
return (-1);
}
return 0;
}
int main(int argc, char **argv)
{
int status =0;
int snd_f;
int fd_f;
lame_global_flags* gfp;
short *input_buff;
char *mp3_buff;
if(argc !=2){
fprintf(stderr, "useage: ./mp3_record test.mp3/n");
return -1;
}
signal(SIGINT,stop);
if((snd_f = open("/dev/dsp", O_RDONLY)) < 0){
fprintf(stderr, "open audio device error: %s", strerror(errno));
status = -1;
goto exit;
}
if((fd_f = open(argv[1], O_CREAT | O_WRONLY)) < 0){
fprintf(stderr, "open file error: %s", strerror(errno));
status = -1;
goto exit;
}
if (SetFormat(snd_f, 16, 2, 44100) < 0) {
fprintf(stderr, "cannot set /dev/dsp in bit 16, channel 2, speed 44100/n");
status = -1;
goto exit;
}
gfp = lame_init();
if (gfp == NULL) {
printf("lame_init failed/n");
status = -1;
goto exit;
}
int ret_code = lame_init_params(gfp);
if (ret_code < 0) {
printf("lame_init_params returned %d/n",ret_code);
status = -1;
goto close_lame;
}
input_buff = (short *)malloc(INBUFF_SIZE*2);
mp3_buff = (char *)malloc(MP3BUFF_SIZE);
int samples;
int mp3_bytes;
int write_bytes;
int n=100;
while(run){
//while(n>0){
//n--;
//fprintf(stderr, "n is %d./n", n);
memset(input_buff, 0 , INBUFF_SIZE*2);
memset(mp3_buff, 0 , MP3BUFF_SIZE);
samples = read(snd_f, input_buff, INBUFF_SIZE*2);
if (samples < 0) {
perror("read sound device failed");
status = -1;
goto free_buffers;
}
// fprintf(stderr, "samples is %d./n", samples);
mp3_bytes = lame_encode_buffer_interleaved(gfp, input_buff, samples/4, mp3_buff, MP3BUFF_SIZE);
//fprintf(stderr, "mp3_bytes is %d./n", mp3_bytes);
if (mp3_bytes < 0) {
printf("lame_encode_buffer_interleaved returned %d/n", mp3_bytes);
status = -1;
goto free_buffers;
}
write_bytes = write(fd_f, mp3_buff, mp3_bytes);
//fprintf(stderr, "write_bytes is %d./n", write_bytes);
if(write_bytes < 0){
perror("write sound data file failed");
status = -1;
goto free_buffers;
}
}
mp3_bytes = lame_encode_flush(gfp, mp3_buff, sizeof(mp3_buff));
if (mp3_bytes > 0) {
fprintf(stderr, "writing %d mp3 bytes/n", mp3_bytes);
if(write(fd_f, mp3_buff, mp3_bytes) <0)
fprintf(stderr, "'writing mp3 bytes error/n");
}
else
fprintf(stderr, "writing mp3 bytes 0/n");
free_buffers:
free(mp3_buff);
mp3_buff = NULL;
free(input_buff);
input_buff = NULL;
close(snd_f);
close(fd_f);
close_lame:
lame_close(gfp);
exit:
return status;
}
Powered by Zoundry Raven
mp3lame linux 编译,linux下lame编程:wav转MP3示例代码相关推荐
- linux编译环境下,华为LiteOS初体验(STM32F407)
linux编译环境下,华为LiteOS初体验(STM32F407) 1.概述 系统:Ubuntu22.04,linux版本号如下 Linux linux 5.15.0-40-generic #43-U ...
- c linux 获取cpuid_Linux下C编程 -- 得到系统的CPU信息(cpuid)
在 linux下可以通过查看 cat /proc/cpuinfo查看CPU的相关信息,但是在linux下C编程需要使用汇编语言来实现,因为C语言中没有实现查看CPU信息的函数,一般实现如下: (C中运 ...
- nuttx linux 编译,Linux下编译nuttx模拟系统
******************************************** 编译Linux系统下的可执行NUTTX系统: ******************************** ...
- 04.iOS 使用lame将wav转换为mp3
在iOS设备中进行录音,录音文件的格式为wav.但这种格式文件会很大,上传到服务器会消耗大量流量.为了适应终端的播放功能以及文件大小的要求,特将wav转换为mp3格式文件来使用. 注意:在录制wav文 ...
- java+lame实现wav到mp3的转换
lame以及lame命令参数设置请大家自行搜索,这里给大家一个例子,java+lame实现压缩wav to mp3. 注意:lame.exe与java工程src放在同一文件夹,否则请使用绝对路径调用l ...
- mp3lame linux 编译,Linux下使用FFmpeg将ogg转mp3
团队的小伙伴接到一个任务,要将几万个ogg转换成mp3格式,网络上的工具只支持一个一个转换,需要大量的人工去操作,很麻烦. 我突然想起来遥远的几年前,曾经使用过FFmpeg转换安卓上传的语音文件,想到 ...
- nuttx linux 编译,linux下建立nuttx开发环境
1.安装ARM Toolchain.(也可以选择使用buildroot自己搭建开发环境,不过我在linux下使用ARM Toolchain觉得挺好用,官方的开发环境也能够比较好支持ARM Toolch ...
- hiredis linux 编译,linux下的hiredis的安装和使用
1.linux下如何安装hiredis 1)下载地址 https://github.com/redis/hiredis 2)编译和安装 解压后的文件夹执行 make;make install; 3)头 ...
- linux 编译mysql_linux下编译MYSQL
本机环境:ubuntu12.04,fedora-17 MYSQL版本:5.5.28 CMAKE版本:2.8.9 一.下载最新版本的cmake,解压后编译安装. sudo ./configure --p ...
- mips linux 编译,Linux环境下mips-gcc编译
背景 在之后的学习中,需要在自己的mips架构cpu上编写一系列工具.不想使用mips汇编代码编写这些工具,希望使用c语言编写,并编译为mips机器语言.所以这次编译没有编译c库(不能使用自带的头文件 ...
最新文章
- 安卓复杂的首页布局_到底该如何快速进入安卓编程?
- 记录腾讯云中矿机病毒处理过程(重装系统了fu*k)
- inputstream示例_Java InputStream close()方法与示例
- Elasticsearch之中文分词器插件es-ik(博主推荐)
- iOS之自定义pickerview(行驶里程数)
- 正则表达式过滤多个词语并替换为相同长度的星号
- word参考文献后面空格太大
- Pytorch 情感分类进阶
- python中分号中没内容_被“嫌弃”的分号的一生:不要在Python中使用无用分号了...
- DB2导出 mysql导入_db2数据库导入导出数据
- 制度罚则-- 日志规范
- [QQ机器人]nonebot每日一言插件
- 数据库事务和范式理解
- Crash内核调试手段
- Django项目——报错处理
- hexo+github搭建个人博客网站问题汇总和解决办法
- 华为2288H V5 U盘安装CentOS7.5
- 红石外汇|每日汇评:在美国CPI之前,英镑多头仍保持对近一年高点的控制
- android动态调试七种武器,安卓动态调试七种武器之长生剑 - Smali Instrumentation
- Android 用于视频回放显示时间刻度的一个自定义View