如何使用 DirectShowLib 捕获 elgato 视频流窗口

本文关键字:视频 窗口 elgato 捕获 何使用 DirectShowLib | 更新日期: 2023-09-27 17:57:21

我有elgato捕获设备连接到我的电脑,我正在尝试捕获和观看elgato捕获设备的窗口。

我在谷歌上搜索并找到了这个答案:

您可以将Elgato的HDMIComponent Game Capture HD用作C#中的视频输入设备吗?

这是代码:

IFilterGraph2 graph;
ICaptureGraphBuilder2 captureGraph;
IBaseFilter elgatoFilter;
IBaseFilter smartTeeFilter;
IBaseFilter videoRendererFilter;
Size videoSize;
//Set the video size to use for capture and recording
videoSize = new Size(1280, 720);
//Initialize filter graph and capture graph
graph = (IFilterGraph2)new FilterGraph();
captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
captureGraph.SetFiltergraph(graph);
rot = new DsROTEntry(graph);
//Create filter for Elgato
Guid elgatoGuid = new Guid("39F50F4C-99E1-464A-B6F9-D605B4FB5918");
Type comType = Type.GetTypeFromCLSID(elgatoGuid);
elgatoFilter = (IBaseFilter)Activator.CreateInstance(comType);
graph.AddFilter(elgatoFilter, "Elgato Video Capture Filter");
//Create smart tee filter, add to graph, connect Elgato's video out to smart tee in
smartTeeFilter = (IBaseFilter)new SmartTee();
graph.AddFilter(smartTeeFilter, "Smart Tee");
IPin outPin = GetPin(PinDirection.Output, "Video", elgatoFilter);
IPin inPin = GetPin(PinDirection.Input, smartTeeFilter);
graph.Connect(outPin, inPin);
//Create video renderer filter, add it to graph, connect smartTee Preview pin to video renderer's input pin
videoRendererFilter = (IBaseFilter)new VideoRenderer();
graph.AddFilter(videoRendererFilter, "Video Renderer");
outPin = GetPin(PinDirection.Output, "Preview", smartTeeFilter);
inPin = GetPin(PinDirection.Input, videoRendererFilter);
graph.Connect(outPin, inPin);
//Render stream from video renderer
captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, videoRendererFilter, null, null);
//Set the video preview to be the videoFeed panel
IVideoWindow vw = (IVideoWindow)graph;
vw.put_Owner(videoFeed.Handle);
vw.put_MessageDrain(this.Handle);
vw.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings | WindowStyle.ClipChildren);
vw.SetWindowPosition(0, 0, 1280, 720);
//Start the preview
mediaControl = graph as IMediaControl;
mediaControl.Run();

我在项目中创建了一个新表单,并添加了 DirectShowLib-2005 dll然后我在新表单的顶部添加了:

using DirectShowLib;

在构造函数添加全局变量之前:

IFilterGraph2 graph;
ICaptureGraphBuilder2 captureGraph;
IBaseFilter elgatoFilter;
IBaseFilter smartTeeFilter;
IBaseFilter videoRendererFilter;
Size videoSize;

然后在构造函数中,我添加了其余代码。而且我现在得到一些错误:

在此行上,变量 rot 不存在:

rot = new DsROTEntry(graph);

在使用方法 GetPin 的四行中,因此不存在方法 GetPin:

IPin outPin = GetPin(PinDirection.Output, "Video", elgatoFilter);
IPin inPin = GetPin(PinDirection.Input, smartTeeFilter);
outPin = GetPin(PinDirection.Output, "Preview", smartTeeFilter);
inPin = GetPin(PinDirection.Input, videoRendererFilter);

在这一行上:

vw.put_Owner(videoFeed.Handle);

变量 videoFeed 不存在。

最后这两行:

mediaControl = graph as IMediaControl;
mediaControl.Run();

媒体控件不存在。

我错过了什么?

如何使用 DirectShowLib 捕获 elgato 视频流窗口

videoFeed是托管您嵌入的视频的控件。

mediaControl将是类型为 IMediaControl 的变量:

IMediaControl mediaControl = graph as IMediaControl;
mediaControl.Run();

GetPin这个或类似; DsROTEntry不是强制性的,但有助于使用 GraphEdit 或类似工具从外部检查筛选器图。