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 格式
相关推荐
Android系统攻城狮8 小时前
Linux Gstreamer深度解析之gst_audio_encoder_set_frame_max调用流程与实战(六十一)
android·linux·运维·音视频·gstreamer音视频·音视频进阶·gstreamer音视频进阶
可乐鸡翅yeah_10 小时前
业务中 M3U8 水印相关坑,硬水印和动态水印区别
前端·网络·数据库·ffmpeg·m3u8在线
redfred19 小时前
HyperFrames 部署教程:5.6 万 Star 开源「写 HTML 出视频」框架,Windows 从零上手(CLI 三条命令 + 7 个避坑)
ffmpeg·开源·自动化·html·音视频
martindelophy19 小时前
用自然语言剪视频:我们在 Timeline Studio 中实现了 ChatCut 对话剪辑
android·音视频
特视界说19 小时前
没有完整 3D 模型,能做产品三维动画吗?
3d·微信·音视频·新浪微博
特视界说19 小时前
产品宣传动画和企业宣传片有什么区别,应该先做哪个?
微信·音视频·新浪微博·新人首发
bluebonnet2719 小时前
【docker】容器内映射外部音频驱动
docker·容器·音视频
Arcai102420 小时前
PostgreSQL数据备份和恢复完全指南
数据库·postgresql·ffmpeg·备份一体机·备份软件·smartstor·智备科技
可乐鸡翅yeah_1 天前
FFmpeg 生成 HLS 独立分片 independent‑segments 参数到底有什么用
javascript·ffmpeg·音视频·safari·m3u8
企业数字化笔记1 天前
视频理解怎样从“看见”变成“判断”?几何规则、轨迹特征与动作模型的选型
音视频