提供一个可以运行的ffmpeg工程:https://gitee.com/qiuguolu1108/ffmpeg-study
ffmpeg -list_devices true -f dshow -i dummy
使用上面的命令获取本机上的音频设备名称
$ ffmpeg -list_devices true -f dshow -i dummy
[dshow @ 04df42c0] DirectShow video devices (some may be both video and audio devices)
[dshow @ 04df42c0] "HIK 1080P Camera"
[dshow @ 04df42c0] Alternative name "@device_pnp_\\?\usb#vid_2bdf&pid_0284&mi_00#6&5d2db91&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
[dshow @ 04df42c0] "OBS Virtual Camera"
[dshow @ 04df42c0] Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\{A3FCE0F5-3493-419F-958A-ABA1250EC20B}"
[dshow @ 04df42c0] DirectShow audio devices
[dshow @ 04df42c0] "麦克风 (USB PnP Sound Device)"
[dshow @ 04df42c0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{48A90BCF-5168-49A0-8D1E-31F869E59C93}"
[dshow @ 04df42c0] "麦克风 (HIK 1080P Camera-Audio)"
[dshow @ 04df42c0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{A09BD548-15D1-4C91-82D0-2AC97F2CD11B}"
dummy: Immediate exit requested
这是在我自己的电脑上运行的结果,麦克风 (USB PnP Sound Device)
就是音频设备的名称。
#define __STDC_CONSTANT_MACROS
extern "C" {
#include
#include
#include
}
#include
#include
#include
#include
char err_buf[1024];
void GB2312ToUtf8(const char* gb2312, char* utf8)
{
if (NULL == gb2312 || NULL == utf8) {
return;
}
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
memset(utf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
if (wstr)
delete[] wstr;
}
AVFormatContext* OpenAudioDevice(const char* device_name)
{
AVInputFormat* format = av_find_input_format("dshow");
AVFormatContext* fmt_ctx = NULL;
AVDictionary* options = NULL;
av_dict_set(&options, "sample_size", "16", 0);
av_dict_set(&options, "channels", "2", 0);
av_dict_set(&options, "sample_rate", "44100", 0);
int ret = avformat_open_input(&fmt_ctx, device_name, format, &options);
if (ret < 0) {
av_strerror(ret, err_buf, 1024);
SPDLOG_ERROR("Failed to open audio device, [{}]:{}.", ret, err_buf);
return NULL;
}
return fmt_ctx;
}
int main()
{
spdlog::set_level(spdlog::level::trace);
avdevice_register_all();
FILE* fp = fopen("audio.pcm", "wb+");
if (!fp) {
SPDLOG_ERROR("Failed to open file");
}
const char* device_name = "audio=麦克风 (High Definition Audio 设备)";
char device_name_utf8[1024] = { 0 };
GB2312ToUtf8(device_name, device_name_utf8);
AVFormatContext* fmt_ctx = OpenAudioDevice(device_name_utf8);
if (!fmt_ctx) {
SPDLOG_ERROR("Failed to open audio device.");
return -1;
}
AVStream* stream = fmt_ctx->streams[0];
AVCodecParameters* params = stream->codecpar;
SPDLOG_INFO("sample rate = {}", params->sample_rate);
SPDLOG_INFO("channels = {}", params->channels);
SPDLOG_INFO("sample format = {}", av_get_bits_per_sample(params->codec_id));
int ret, count = 0;
AVPacket packet;
av_init_packet(&packet);
while ((ret = av_read_frame(fmt_ctx, &packet)) == 0 && count++ < 20) {
SPDLOG_DEBUG("packet size = {}", packet.size);
fwrite(packet.data, packet.size, 1, fp);
av_packet_unref(&packet);
}
av_packet_unref(&packet);
fclose(fp);
avformat_close_input(&fmt_ctx);
return 0;
}
[2021-09-14 21:07:22.678] [info] [main.cpp:76] sample rate = 44100
[2021-09-14 21:07:22.679] [info] [main.cpp:77] channels = 2
[2021-09-14 21:07:22.679] [info] [main.cpp:78] sample format = 16
[2021-09-14 21:07:23.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:23.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:24.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:24.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:25.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:25.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:26.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:26.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:27.202] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:27.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:28.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:28.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:29.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:29.703] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:30.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:30.702] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:31.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:31.702] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:32.203] [debug] [main.cpp:86] packet size = 88200
[2021-09-14 21:07:32.703] [debug] [main.cpp:86] packet size = 88200
使用下面这条命令可以播放录制的音频:
ffplay -f s16le -ac 2 -ar 44100 audio.pcm
使用上面提供的工程时,生成的audio.pcm文件在player目录中。