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 格式
相关推荐
starvapour7 小时前
Ubuntu系统下基于终端的音频相关命令
linux·ubuntu·音视频
Memory_荒年9 小时前
FFmpeg:音视频界的“万能瑞士军刀”
ffmpeg
高山流水&上善17 小时前
基于BERT情感分析与多维度可视化的B站热门视频评论分析系统
人工智能·bert·音视频
QJtDK1R5a18 小时前
V4L2 vs GStreamer vs FFmpeg:Linux多媒体处理的三个层级
linux·运维·ffmpeg
阿酷tony19 小时前
如何做视频课程的报名观看?实现报名后,才能观看视频?
音视频
福大大架构师每日一题19 小时前
ollama v0.20.0 更新:Gemma 4 全家桶发布,音频、视觉、MoE、BPE 支持全面升级
音视频·ollama
Flamingˢ2 天前
ZYNQ + OV5640 + HDMI 视频系统调试记录:一次 RGB888 与 RGB565 引发的黑屏问题
arm开发·嵌入式硬件·fpga开发·vim·音视频
Flamingˢ2 天前
YNQ + OV5640 视频系统开发(二):OV5640_Data IP 核源码解析
arm开发·嵌入式硬件·网络协议·tcp/ip·fpga开发·vim·音视频
Flamingˢ2 天前
ZYNQ + OV5640 视频系统开发(三):AXI VDMA 帧缓存原理
arm开发·嵌入式硬件·fpga开发·vim·音视频
Hello World . .2 天前
Linux:Linux命令行音视频播放器
linux·音视频