如何使用libavdevice正确查询系统中的所有视频设备?打开输入int err = avformat_open_input(&context, NULL, fmt, NULL);失败,因为未提供设备名称(第二个参数是设备名称,"/dev/video0"例如,如果提供,则仅找到该设备):
avcodec_register_all();
avdevice_register_all();
AVFormatContext* context = avformat_alloc_context();
AVInputFormat *fmt = av_find_input_format("video4linux");
printf("trying to open input");
int err = avformat_open_input(&context, "/dev/video0", fmt, NULL);
if(err != 0){
fprintf(stderr, "ffmpeg failed to open input");
}
static struct AVDeviceInfoList* devices_list;
avdevice_list_devices(context, &devices_list);
int devices_count = devices_list->nb_devices;
for(int i = 0; i < devices_count; i++){
printf("Checking device nr. %d \n", i);
AVDeviceInfo* current_device_info = devices_list->devices[i];
printf("Find Device: %s", current_device_info->device_description);
}
avformat_free_context(context);
Run Code Online (Sandbox Code Playgroud)
如何初始化 AVFormatContext 以查找所有 v4l2 设备?我想如果上下文部分初始化(假设需要所有 v4l2devices),它应该正确查询所有内容。当上下文运行时,avformat_open_input必须事先提供确切的设备名称。