请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Windows下无法打开音频设备

#include
#include

extern “C”
{
#include “libavformat/avformat.h”
#include “libavutil/avutil.h”
#include “libavdevice/avdevice.h”
}

int main()
{

avdevice_register_all();

const AVInputFormat *ifmt = av_find_input_format("dshow");

AVFormatContext *fmtCtx = nullptr;
FILE *outFile = fopen("output.pcm", "wb+");

/*
以下是命令查看的
ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version 2024-01-11-git-5e751dabc5-full_build-www.gyan.dev Copyright © 2000-2024 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 58. 36.101 / 58. 36.101
libavcodec 60. 37.100 / 60. 37.100
libavformat 60. 20.100 / 60. 20.100
libavdevice 60. 4.100 / 60. 4.100
libavfilter 9. 17.100 / 9. 17.100
libswscale 7. 6.100 / 7. 6.100
libswresample 4. 13.100 / 4. 13.100
libpostproc 57. 4.100 / 57. 4.100
[dshow @ 000001caa497d640] “Integrated Camera” (video)
[dshow @ 000001caa497d640] Alternative name “@device_pnp_\?\usb#vid_04f2&pid_b6d9&mi_00#6&850bcba&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global”
[dshow @ 000001caa497d640] “麦克风阵列 (Realtek® Audio)” (audio)
[dshow @ 000001caa497d640] Alternative name “@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{0D541114-F0DB-4B7E-BADF-7F045E1881F0}”
[in#0 @ 000001caa49676c0] Error opening input: Immediate exit requested
Error opening input file dummy.
*/

//通过ffmpeg来获取名称,也无法打开设备
std::string devName(“audio=”);
AVDeviceInfoList *devs = nullptr;
avdevice_list_input_sources(ifmt, nullptr, nullptr, &devs);
for (int i = 0; i < devs->nb_devices; i++)
{
AVDeviceInfo devInfo = devs->devices[i];
if (
(devInfo->media_types) == AVMEDIA_TYPE_AUDIO)
devName.append(devInfo->device_name);
}

//不加audio=,也是打开失败
int ret = avformat_open_input(&fmtCtx, devName.c_str(), NULL, NULL);
if (ret < 0)
{
char err[1024];
av_strerror(ret, err, 1024);

    fprintf(stderr, "打开输入设备失败: %s", err);
    return -1;
}

AVPacket *pkt = av_packet_alloc();
int ct = 0;
while (av_read_frame(fmtCtx, pkt) >= 0 && ct++ < 500)
{
    printf("packet size:%d\n", pkt->size);
    fwrite(pkt->data, pkt->size, 1, outFile);
}

fclose(outFile);

av_packet_free(&pkt);

return 0;

}

正在回答 回答被采纳积分+3

1回答

李超 2024-01-31 16:33:19

先确定用ffmpeg 命令是否可以打开音频设备

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信