AForge获取本机视频设备列表

最近项目写了一个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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值