我如何使用 DirectShowLib 捕获 elgato 视频流 window?

How can i use DirectShowLib to capture the elgato video stream window?

我已将 elgato 捕获设备连接到我的电脑,我正在尝试捕获并实时观看 elgato 捕获设备的 window。

我在 google 中搜索并找到了这个答案:

Can you use Elgato's HDMIComponent Game Capture HD as a video-in device in 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();

mediaControl 不存在。

我错过了什么?

videoFeed 是您的控件,用于托管您要嵌入的视频。

mediaControl 将是 IMediaControl:

类型的变量
IMediaControl mediaControl = graph as IMediaControl;
mediaControl.Run();

GetPinthis or similar; DsROTEntry is not mandatory but helps inspect filter graph externally 使用 GraphEdit 或类似工具。