1、命令查询,找到ffmpeg.exe目录所在,打开控制台(Linux上打开终端)进入该目录,然后执行命令
ffmpeg查询设备可用命令或者代码api实现,命令如下:
ffmpeg -list_devices true -f dshow -i dummy
查询到设备名称后,可以根据名称查询设备的参数
如果是摄像头(假设摄像头名USB 2.0 Camera)命令如下:
ffmpeg -list_options true -f dshow -i video="USB 2.0 Camera"
如果是麦克风(假设麦克风名是麦克风 (USB Microphone)),命令如下:
ffmpeg -list_options true -f dshow -i audio="麦克风 (USB Microphone)"
2、代码方式
void showDevice()
{
AVFormatContext* pFormat = avformat_alloc_context();
AVDictionary* dict = NULL;
av_dict_set(&dict, "list_devices", "true", 0);
const AVInputFormat* fmt = av_find_input_format("dshow");
avformat_open_input(&pFormat, "", fmt, &dict);
}
void showOption(char * device_name)
{
AVFormatContext* pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options, "list_options", "true", 0);
const AVInputFormat* fmt = av_find_input_format("dshow");
avformat_open_input(&pFormatCtx, device_name, fmt, &options);
}
如果在控制台执行会打印在界面上,还可以保存在变量中,示例代码如下:
AVDeviceInfoList* devicelist;
avdevice_list_input_sources(av_find_input_format("dshow"), NULL, NULL, &devicelist);
char device[200];
for (int i = 0; i < devicelist->nb_devices; i++)
{
if (*(devicelist->devices[i]->media_types) == AVMEDIA_TYPE_VIDEO)
{
cout << devicelist->devices[i]->device_name << endl;
sprintf(device, "video=%s", devicelist->devices[i]->device_name);
showOption(device);
}
else if (*(devicelist->devices[i]->media_types) == AVMEDIA_TYPE_AUDIO)
{
cout << devicelist->devices[i]->device_name << endl;
sprintf(device, "audio=%s", devicelist->devices[i]->device_name);
showOption(device);
}
}