usb摄像头的设备名称:
(1)window下,可以通过命令查看ffmpeg -list_devices true -f dshow -i dummy
也可以直接在 设备管理器 中查看,例如:
(2)linux下,可以查看/dev/videoX(X可以是0,1......)
(3)代码如下:
avdevice_register_all(); //这个不能少,否则会打开失败。
AVFormatContext *pFormatCtx=nullptr;
#if defined(_WIN32)
input_fmt = av_find_input_format("dshow");
real_url = "video=" + url;//window下的usb摄像头名称为"LRCP USB2.0"
#else
input_fmt = av_find_input_format("video4linux2");
real_url = url;//linux下的usb摄像头名称为"/dev/video0 、/dev/video1"
#endif
if (avformat_open_input(&pFormatCtx, real_url.c_str(), iformat, NULL) != 0)
{
printf("Couldn't open input stream.\n");
return -1;
}