通過命令獲取音視頻設備列表:
ffmpeg -list_devices true -f dshow -i dummy

搜狗截圖20200617200342.png
代碼實現(xiàn)音頻列表的獲取 pcm數(shù)據(jù)的采集
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include <libavutil/log.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
}
#include <windows.h>
#include <vector>
#include <string>
#include <memory>
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "Winmm.lib")
using std::vector;
using std::string;
using std::shared_ptr;
void capture_audio()
{
//windows api 獲取音頻設備列表(ffmpeg 好像沒有提供獲取音視頻設備的api)
int nDeviceNum = waveInGetNumDevs();
vector<string> vecDeviceName;
for (int i = 0; i < nDeviceNum; ++i)
{
WAVEINCAPS wic;
waveInGetDevCaps(i, &wic, sizeof(wic));
//轉(zhuǎn)成utf-8
int nSize = WideCharToMultiByte(CP_UTF8, 0, wic.szPname, wcslen(wic.szPname), NULL, 0, NULL, NULL);
shared_ptr<char> spDeviceName(new char[nSize + 1]);
memset(spDeviceName.get(), 0, nSize + 1);
WideCharToMultiByte(CP_UTF8, 0, wic.szPname, wcslen(wic.szPname), spDeviceName.get(), nSize, NULL, NULL);
vecDeviceName.push_back(spDeviceName.get());
av_log(NULL, AV_LOG_DEBUG, "audio input device : %s \n", spDeviceName.get());
}
if (vecDeviceName.size() <= 0)
{
av_log(NULL, AV_LOG_ERROR, "not find audio input device.\n");
return;
}
string sDeviceName = "audio=" + vecDeviceName[0];//使用第一個音頻設備
//ffmpeg
avdevice_register_all(); //注冊所有輸入輸出設備
AVInputFormat* ifmt = av_find_input_format("dshow"); //設置采集方式 dshow
if (ifmt == NULL)
{
av_log(NULL, AV_LOG_ERROR, "av_find_input_format for dshow fail.\n");
return;
}
AVFormatContext* fmt_ctx = NULL; //format 上下文
int ret = avformat_open_input(&fmt_ctx, sDeviceName.c_str(), ifmt, NULL); //打開音頻設備
if (ret != 0)
{
av_log(NULL, AV_LOG_ERROR, "avformat_open_input fail. return %d.\n", ret);
return;
}
AVPacket pkt;
FILE* fp = fopen("dst.pcm", "wb");
int count = 0;
while (count++ < 10)
{
ret = av_read_frame(fmt_ctx, &pkt);
if (ret != 0)
{
av_log(NULL, AV_LOG_ERROR, "av_read_frame fail, return %d .\n", ret);
break;
}
fwrite(pkt.data, 1, pkt.size, fp);
av_packet_unref(&pkt);//必須釋放pkt申請的內(nèi)存,否則會內(nèi)存泄露
}
fflush(fp);//刷新文件io輸出緩沖區(qū)
fclose(fp);
avformat_close_input(&fmt_ctx);
}
int main(int argc, char** argv)
{
av_log_set_level(AV_LOG_DEBUG); //設置ffmpeg日志庫等級
capture_audio();
Sleep(1);
}
設置音頻輸出驅(qū)動并使用ffplay 進行播放
命令:
set SDL_AUDIODRIVER=directsound //設置可用的音頻輸出驅(qū)動
ffplay -ar 44100 -channels 2 -f s16le -i dst.pcm //使用ffplay 命令 進行播放

2.png