ffmpeg 采集音频数据

音视频数据采集的步骤:

  • 设备注册
  • 设置对应的采集方式,avfoundation、dshow、alas
  • 打开设备

具体的例子:

cpp 复制代码
#include <stdio.h>
extern "C"{
    #include <libavutil/avutil.h>
    #include <libavdevice/avdevice.h>
    #include <libavformat/avformat.h>
    #include <libavcodec/avcodec.h>
}



int main(int argc, char const *argv[])
{
    /* code */
    // 注册所有的设备
    avdevice_register_all();

    // 输出的文件
    char *filename = "./audio.pcm";

    // 文件操作
    FILE *outfile = fopen(filename,"wb+");

    // 错误缓冲区
    char errr[1024];

    // 错误码
    int ret = 0;

    // 存储音视频封装格式中包含的信息的结构体,
    AVFormatContext *format_context ;

    // 定义设备地址
    char *device_name = "hw:0";

    AVPacket pkt ;
    av_init_packet(&pkt);
    // 获得媒体数据的格式
    AVInputFormat * iformat = av_find_input_format("alas");

    if((ret=avformat_open_input(&format_context,device_name,iformat,NULL))!=0){
        av_strerror(ret,errr,1024);
    }
    int count = 0;
     while(ret = (av_read_frame(format_context, &pkt))== 0&& 
        count++ < 500) {
        av_log(NULL, AV_LOG_INFO, "pkt size is %d(%p), count=%d\n",
            pkt.size,pkt.data, count);
        fwrite(pkt.data, 1, pkt.size, outfile);
        fflush(outfile);
        av_packet_unref(&pkt);//release pkt
    }

    
    fclose(outfile);
    avformat_close_input(&format_context);
    
    return 0;
}

针对输出的结果进行输出。

cpp 复制代码
ffplay -ar 44100 -ac 2 -f s16le audio.pcm 
  • -ar 比特率
  • -ac 通道数
  • -f 格式
相关推荐
视频技术分享4 小时前
音视频SDK深度解析:从技术演进到核心开发要点
音视频
芯跳加速6 小时前
AI 视频自动化学习日记 · 第四天
人工智能·自动化·音视频
科技小E6 小时前
视频分析平台EasyGBS视频质量诊断技术解析与实战功能应用
音视频
一招定胜负6 小时前
【实战】Python + 讯飞语音识别 + 通义千问:课堂视频自动转结构化教学数据
python·音视频·语音识别
Black蜡笔小新6 小时前
GB28181视频汇聚融合平台EasyCVR如何赋能重塑安防监控可视化
音视频
爱钓鱼的程序员小郭7 小时前
腾讯云 vs 阿里云 视频直播费用分析报告
阿里云·音视频·腾讯云
Roselind_Yi7 小时前
技术拆解:《从音频到动效:我是如何用 Web Audio API 拆解音乐的?》
前端·javascript·人工智能·音视频·语音识别·实时音视频·audiolm
Black蜡笔小新7 小时前
国标GB28181视频汇聚平台EasyCVR智慧社区全场景可视化管控与智能安防实践
音视频
小博士爱吃西红柿7 小时前
grok-video-3 视频生成 API 对接配置教程
音视频
#麻辣小龙虾#7 小时前
html浏览器自动播放视频策略
前端·html·音视频