AForge获取本机视频设备列表

来源:互联网 发布:蜂窝数据用量0 编辑:程序博客网 时间:2024/04/29 13:36

最近项目写了一个ffmpeg rtmp推流器,需要查询电脑视频列表。

研究了下大概有3种方法

1.利用ffmpeg打印

void show_dshow_device() {AVFormatContext *pFormatCtx = avformat_alloc_context();AVDictionary* options = NULL;av_dict_set(&options, "list_devices", "true", 0);AVInputFormat *iformat = av_find_input_format("dshow");//av_log_set_callback(my_logoutput);printf("========Device Info=============\n");avformat_open_input(&pFormatCtx, "video=dummy", iformat, &options);printf("================================\n");avformat_free_context(pFormatCtx);//av_log_set_callback(NULL);    }

这样打印可能只能在控制台才能看到,对于编程不实用

这里我提供一个投机取巧的方式供大家参考可以得到名称集合

原理就是改变ffmpeg的打印输出log对象 捕获到log 就可以进行分析了

void my_logoutput(void* ptr, int level, const char* fmt, va_list vl) {FILE *fp = fopen("my_log.txt", "a");if (fp) {vfprintf(fp, fmt, vl);fflush(fp);fclose(fp);}}

利用av_log_set_callback函数就可以实现捕获

2.利用原生direshow方法,这个方法是最好的 ffmpeg和其他类库都是基于这个接口。这个我没研究 感觉操作起来比较麻烦
3.利用AForge库

发现用AForge类库是最简单的,因为里面封装好了

现在贴出AForge 代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using AForge.Video.DirectShow;namespace CRLIVE.UI.BLL{    public static class CheckDevices    {        static FilterInfoCollection videoDevices;        public static void AForgeShowDevicesList()        {                        // show device list            try            {                // enumerate video devices                videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);                if (videoDevices.Count == 0)                    throw new ApplicationException();                // add all devices to combo                foreach (FilterInfo device in videoDevices)                {                    Console.WriteLine(device.Name);                }            }            catch (ApplicationException)            {                Console.WriteLine("No local capture devices");            }        }    }}

注意需要引用命名空间
using AForge.Video.DirectShow;

dll下载

1 0
原创粉丝点击