首页 > 解决方案 > ffmpeg avformat_open_input() 无法打开包含中文字符的 dshow 设备 url

问题描述

USB网络摄像头还可以,但我要使用的设备是一个名为“无他伴侣(竖屏)”的“虚拟摄像头”,其视频来自Android或iOS等智能手机。将手机连接到 PC,在手机上运行一个应用程序,然后运行一个 PC 客户端应用程序,它可以预览视频。手机应用名为“无他相机”,PC应用名为“无他伴侣”,网址为https://www.wuta-cam.com/

我在Windows命令行下用命令运行FFmpeg,ffmpeg -list_devices true -f dshow -i dummy列出设备就可以了。(为了正确显示中文,请chcp 65001提前运行。)

运行命令ffplay -f dshow -i video="无他伴侣(竖屏)",就可以播放视频了。(当然需要提前确认其PC客户端预览正常。)

现在我想在我的程序中从那个虚拟相机获取解码帧,我avformat_open_input()用调用video=无他伴侣(竖屏),它失败了,返回值为 -5,I/O 错误。

有人知道原因吗?请帮忙。提前致谢。

下面是我的代码片段。

avdevice_register_all();
avcodec_register_all();
//const char * url= "video=Logitech Webcam C930e";// This is fine.
char url[] = "video=无他伴侣(竖屏)";// This is bad.

AVFormatContext *pFmtCtx = avformat_alloc_context();
AVInputFormat *iformat = av_find_input_format("dshow");
int nRet = 0;
nRet = avformat_open_input(&pFmtCtx, url, iformat, NULL);
if (nRet)
{
    const size_t buffer_size = 256;
    char err_description[buffer_size];
    av_strerror(nRet, err_description, buffer_size);
    printf("%s.\n", err_description);// --> I/O error.
    printf("FAILED to open input.(Line:%d,%d)\n",  __LINE__, nRet);
    return -1;
}

标签: ffmpeg

解决方案


FFmpeg 可能无法直接处理汉字。如果设备名称中包含中文字符,FFmpeg 会报找不到给定名称的设备。我尝试了 Windows API 函数WideCharToMultiByte(),它可以工作。


推荐阅读