最近项目中需要使用ffmpeg实现录音功能,使用的ffmpeg-3.4.4的库,根据源代码dshow.c中的定义

{ "audio_device_number", "set audio device number for devices with same name (starts at 0)", OFFSET(audio_device_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },

在PC机存在两个麦克风设备的场合,添加一个[录音设备选择]对话框,可供用户选择。

打个比方:目前的PC机存在两个音频输入设备:

  • 0:麦克风 (HD Webcam C270)
  • 1:麦克风 (Realtek High Definition Audio)

备注:

  • 0号采集设备可以采集音频与视频,不能播放音频;1号采集设备仅可以采集音频,可以播放音频。
  • PC机设备播放设备只有1个,那就是:麦克风 (Realtek High Definition Audio),即为0号播放设备。
  • 音频采集设备的序号与音频播放设备的序号很可能不一致。

如果指定 0号采集设备:麦克风 (HD Webcam C270)作为音频采集设备,简化代码如下:

char *pchDeviceName = "麦克风 (HD Webcam C270)";
AVInputFormat *m_pAudioInputFormat = av_find_input_format("dshow");
AVDictionary *options = NULL;
av_dict_set(&options, "audio_device_number", "", );
char str_device_gb2312_name[];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -;
AVFormatContext *m_pAudioFmtCtx = avformat_alloc_context();
ret = avformat_open_input(&m_pAudioFmtCtx, pchUtfName, m_pAudioInputFormat, &options);

这时avformat_open_input函数成功,推测是因为0号音频采集设备与0号音频播放设备都存在所致。

如果指定 1号采集设备:麦克风 (Realtek High Definition Audio)作为音频采集设备,简化代码如下:

char *pchDeviceName = "麦克风 (Realtek High Definition Audio)";
AVInputFormat *m_pAudioInputFormat = av_find_input_format("dshow");
AVDictionary *options = NULL;
av_dict_set(&options, "audio_device_number", "", );
char str_device_gb2312_name[];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -;
AVFormatContext *m_pAudioFmtCtx = avformat_alloc_context();
ret = avformat_open_input(&m_pAudioFmtCtx, pchUtfName, m_pAudioInputFormat, &options);

这时avformat_open_input函数失败原因是I/O fail,推测是因为1号音频采集设备存在但0号音频播放设备不存在所致。

此时的ffmpeg日志如下:

2019-12-10 13:57:21 076[ERR] Could not find audio only device with name [麦克风 (Realtek High Definition Audio)] among source devices of type audio.
2019-12-10 13:57:21 077[INF] Searching for audio device within video devices for 麦克风 (Realtek High Definition Audio)
2019-12-10 13:57:21 090[ERR] Could not find audio only device with name [麦克风 (Realtek High Definition Audio)] among source devices of type video.

  

解决方法如下(不指定音频设备序号,仅仅指定音频设备名称):

char *pchDeviceName = "麦克风 (Realtek High Definition Audio)";
AVInputFormat *m_pAudioInputFormat = av_find_input_format("dshow");
AVDictionary *options = NULL;
char str_device_gb2312_name[];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -;
AVFormatContext *m_pAudioFmtCtx = avformat_alloc_context();
ret = avformat_open_input(&m_pAudioFmtCtx, pchUtfName, m_pAudioInputFormat, &options);

不要轻易使用ffmpeg的audio_device_number来设置音频设备的更多相关文章

  1. ffmpeg转码参数设置

    ffmpeg用了很久了,也没有想写点什么. 刚接触ffmpeg也是有大量的不理解的地方,不过慢慢的了解多了基本上都是可以使用的. 本文主要介绍如何使用ffmpeg.exe进行转码.编译好的ffmpeg ...

  2. ffmpeg 速查手册

    ref : http://linux.51yip.com/search/ffmpeg ffmpeg是一个源于Linux的工具软件,是FLV视频转换器,可以轻易地实现FLV向其它格式avi.asf. m ...

  3. [FFmpeg] ffmpeg参数详解

    ffmpeg 参数语法 ffmpeg [[options][`-i' input_file]]... {[options] output_file}... 如果没有输入文件,那么视音频捕捉就会起作用. ...

  4. FFMPEG解码流程

    FFMPEG解码流程:  1. 注册所有容器格式和CODEC: av_register_all()  2. 打开文件: av_open_input_file()  3. 从文件中提取流信息: av_f ...

  5. 【转】ffmpeg参数中文详细解释

    感谢“大神”的无私奉献:http://blog.csdn.net/leixiaohua1020/article/details/15811977 a) 通用选项 -L license-h 帮助-fro ...

  6. ffmpeg视频格式转换(Java)

    命令: 高品质: ffmpeg -i E:\input\a.wmv -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 4 -y E ...

  7. c# ffmpeg常用参数

    c#  ffmpeg常用参数 转换文件格式的同时抓缩微图: ffmpeg -i "test.avi" -y -f image2 -ss 8 -t 0.001 -s 350x240 ...

  8. ffmpeg命令学习

    1.组成 程序:ffmpeg.ffplay.ffprobe.ffserverffmpeg:转码程序ffplay:播放程序ffserver:服务器程序 库:libavcodec.libavdevice. ...

  9. FFmpeg介绍及参数详细说明

    FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件).它提供了录制.转换以及流化音视频的完整解决方案.它包含了非常先进的音频/视频编解码库l ...

随机推荐

  1. nginx 之 root和alias

    转载:  https://www.jianshu.com/p/4be0d5882ec5 https://blog.csdn.net/Erica_1230/article/details/7855311 ...

  2. 一种关闭Windows 8.1 Windows Defender的简单办法

    背景 安装好Windows 8.1,像往常一样,打开[服务]准备关闭Windows Defender,发现不能设置启动类型而且其默认为启动状态,如下图所示. 两个有两种方式关闭它.一是安装第三方安全软 ...

  3. OnUpdateError

    DataSetProvider1.OnUpdateError void __fastcall TFrmItem::Query1UpdateError(TDataSet *ASender, EFDExc ...

  4. ie11 div不显示背景颜色解决方案

    我的一个场景就是,一个空的div,但是想加个背景颜色,方案就是在div加个空content,利用before属性加上背景<div class="hilan"></ ...

  5. python 生成螺旋矩阵

    对于任意 m*n 矩阵,将 1~m*n 的数字按照螺旋规则在矩阵中排列. 如 m=3,n=3,期望结果为: [ [ , , ], [ , , ], [ , , ] ] 以下代码支持方阵以及非方阵. c ...

  6. Python基础之for循环

    for循环:用户按照顺序循环可迭代对象的内容 1. for循环字符串 msg = "string" for i in msg: print(i) 执行结果为: s t r i n ...

  7. shell基础之二 bash特性详解

    https://blog.51cto.com/13520779/2093146 合格linux运维人员必会的30道shell编程面试题及讲解:https://blog.51cto.com/oldboy ...

  8. Failed to execute goal maven-gpg-plugin 1.5 Sign

    问题描述: 解决办法:跳过maven-gpg-plugin <build> <pluginManagement> <plugins> <plugin> ...

  9. 使用selenium IDE 等一系列需要下载的东西的地址

    转载来自:http://blog.csdn.net/u012246342/article/details/53005730 selenium 官网 IDE 等一系列 下载地址:http://www.s ...

  10. 2.jdk1.8+springboot中http1.1之tcp连接复用实现

    接上篇:https://www.cnblogs.com/Hleaves/p/11284316.html 环境:jdk1.8 + springboot 2.1.1.RELEASE + feign-hys ...