https://blog.csdn.net/weixin_43746325/article/details/126432552
linux下命令
ffmpeg -f alsa -i hw:0 out.wav
这个命令倒是可以运行,不报错,但没有声音,这时可去学习alsa的知识,把基础的设备弄明白了
该代码尚未运行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <libavutil/avutil.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
int main(int argc, char** argv)
{
int ret = 0;
char errors[1024];
int count = 0;
AVPacket pkt;//数据存放
char *devicename = "hw:0";//mac: :0
AVFormatContext *fmt_ctx = NULL; //记得赋值NULL 上下文
AVDictionary *options = NULL;
char *out = "./audio.pcm";
FILE *outfile = NULL;
char input_command[128];
int flag=-1;
av_log_set_level(AV_LOG_DEBUG);
avdevice_register_all(); //打开所有设备
AVInputFormat *iformat = av_find_input_format("alsa");//设置平台格式 mac: avfoundation
if( (ret = avformat_open_input(&fmt_ctx, devicename,iformat,&options) )< 0)//传入参数 打开设备
{
av_strerror(ret,errors,1024);
av_log(NULL,AV_LOG_DEBUG,"Failed to open audio device,[%d]%s\n",ret,errors);
return -1;
}
av_init_packet(&pkt);//数据初始化 干净的空间;
//create file
outfile = fopen(out,"wb+");
flag=fcntl(0,F_GETFL); //获取当前flag
flag |=O_NONBLOCK; //设置新falg
fcntl(0,F_SETFL,flag); //更新flag
while((ret = av_read_frame(fmt_ctx,&pkt)) == 0)
{
//write FILE
fwrite(pkt.data,pkt.size,1,outfile);
fflush(outfile);
if((ret=read(0,input_command,sizeof(input_command))) > 0)
{
if(strncmp(input_command, "over",4) == 0)
{
av_log(NULL,AV_LOG_DEBUG,"over\n");
break;
}
else
{
av_log(NULL,AV_LOG_DEBUG,"请重新输入\n");
}
memset(input_command, 0, sizeof(input_command));
}
av_log(NULL,AV_LOG_DEBUG,"pkt_size:%d(%p)\n",pkt.size,pkt.data);
av_packet_unref(&pkt);//缓冲区 内存释放
}
fclose(outfile);
avformat_close_input(&fmt_ctx);
av_log(NULL,AV_LOG_DEBUG,"Finish\n");
return 0;
}
gcc av_log_optimi.c -I/usr/ffmpeg4.1/include -L/usr/ffmpeg4.1/lib -o av_log_optimi -lavutil -lavdevice -lavformat -lavcodec
./av_log_optimi
输入 over 结束录音。
录音文件播放,使用ffpaly播放
ffplay -ar 44100 -ac 2 -f s16le audio.pcm
ar表示 采样率
ac 表示通道数
f 表示采样大小格式
|