不要輕易使用ffmpeg的audio_device_number來設置音頻設備


最近項目中需要使用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", "0", 0);
char str_device_gb2312_name[128];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -1;
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", "1", 0);
char str_device_gb2312_name[128];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -1;
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[128];
_snprintf(str_device_gb2312_name, sizeof(str_device_gb2312_name), "audio=%s", pchDeviceName);
char *pchUtfName = gb2312_to_utf8(str_device_gb2312_name);
int ret = -1;
AVFormatContext *m_pAudioFmtCtx = avformat_alloc_context();
ret = avformat_open_input(&m_pAudioFmtCtx, pchUtfName, m_pAudioInputFormat, &options);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM