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 格式
相关推荐
扫地僧98514 小时前
MultiTalk 是一种音频驱动的多人对话视频生成模型
音视频
The god of big data14 小时前
为什么在1080p的屏幕下,通常观看4K视频要比1080p的视频来的清晰?
人工智能·音视频
Tipriest_14 小时前
介绍常见的图像和视频存储格式以及其优劣势
音视频·blender·视频格式·图像格式
国际云,接待15 小时前
【视频直播出海】阿里云ApsaraVideo Live:从零搭建全球直播平台的“星际航行”指南!
阿里云·云计算·音视频
卖猪肉的痴汉1 天前
1.1 Linux 编译FFmpeg 4.4.1
linux·ffmpeg
东风西巷2 天前
AudioLab安卓版:音频处理,一应俱全
ffmpeg·音视频·软件需求
FakeOccupational2 天前
ESP32 005 MicroPython I2S 实现音频传输与播放
音视频
FF-Studio2 天前
【DSP笔记 · 第5章】数字滤波器的蓝图:从数学公式到硬件实现的艺术
笔记·fpga开发·自动化·音视频·音频·信号处理
光电的一只菜鸡2 天前
ubuntu之坑(十四)——安装FFmpeg进行本地视频推流(在海思平台上运行)
ubuntu·ffmpeg·音视频
weixin_428498492 天前
FFmpeg 压缩视频文件
ffmpeg