如何使用 Direct Show.net Library 制作屏幕捕获视频?

发布于 2024-09-30 21:37:43 字数 1517 浏览 3 评论 0原文

如何使用 Direct Show.net Library 制作屏幕捕获视频? 我阅读了msdn Direct show文档并找到了在以下代码中更改视频源设备的方法。 此代码将网络摄像头作为视频设备。

   public IBaseFilter FindCaptureDevice()
{
  int hr = 0;
  IEnumMoniker classEnum = null;
  IMoniker[] moniker = new IMoniker[1];
  object source = null;
  // Create the system device enumerator
  ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

  // Create an enumerator for the video capture devices
  hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum,0);
  DsError.ThrowExceptionForHR(hr);

  // The device enumerator is no more needed
  Marshal.ReleaseComObject(devEnum);

  // If there are no enumerators for the requested type, then 
  // CreateClassEnumerator will succeed, but classEnum will be NULL.
  if (classEnum == null)
  {
    throw new ApplicationException("No video capture device was detected.\r\n\r\n" +
                                   "This sample requires a video capture device, such as a USB WebCam,\r\n" +
                                   "to be installed and working properly.  The sample will now close.");
  }




  if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)

  {

    Guid iid = typeof(IBaseFilter).GUID;
    moniker[0].BindToObject(null, null, ref iid, out source);
  }
  else
  {
    throw new ApplicationException("Unable to access video capture device!");   
  }


  Marshal.ReleaseComObject(moniker[0]);
  Marshal.ReleaseComObject(classEnum);


  return (IBaseFilter) source;
}

How to make screen capture video using Direct Show.net Library?
I read msdn Direct show document and find the way to change video source device within the following code.
This code got webcamera as video device.

   public IBaseFilter FindCaptureDevice()
{
  int hr = 0;
  IEnumMoniker classEnum = null;
  IMoniker[] moniker = new IMoniker[1];
  object source = null;
  // Create the system device enumerator
  ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

  // Create an enumerator for the video capture devices
  hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum,0);
  DsError.ThrowExceptionForHR(hr);

  // The device enumerator is no more needed
  Marshal.ReleaseComObject(devEnum);

  // If there are no enumerators for the requested type, then 
  // CreateClassEnumerator will succeed, but classEnum will be NULL.
  if (classEnum == null)
  {
    throw new ApplicationException("No video capture device was detected.\r\n\r\n" +
                                   "This sample requires a video capture device, such as a USB WebCam,\r\n" +
                                   "to be installed and working properly.  The sample will now close.");
  }




  if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)

  {

    Guid iid = typeof(IBaseFilter).GUID;
    moniker[0].BindToObject(null, null, ref iid, out source);
  }
  else
  {
    throw new ApplicationException("Unable to access video capture device!");   
  }


  Marshal.ReleaseComObject(moniker[0]);
  Marshal.ReleaseComObject(classEnum);


  return (IBaseFilter) source;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情愿 2024-10-07 21:37:43

您需要一个过滤器来捕获屏幕并将视频发送到流中。在 DirectShow SDK 中有一个名为 PushSource 的示例过滤器,里面有一个 PushSourceDesktop。编译它并作为源过滤器插入到图表中。

You need a filter which captures screen and sends the video down the stream. In DirectShow SDK there is a sample filter called PushSource and inside there is PushSourceDesktop. Compile it and insert into your graph as a source filter.

离旧人 2024-10-07 21:37:43

好的,

public void CaptureVideo()
{
  int hr = 0;
  IBaseFilter sourceFilter = null;
  IBaseFilter audiosourceFilter = null;
  IBaseFilter asfWriter = null;
  IFileSinkFilter pTmpSink = null;

  try
  {

    GetInterfaces();
    hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
    DsError.ThrowExceptionForHR(hr);

    sourceFilter = FindVideoCaptureDevice(); //return Video source filter
    audiosourceFilter = FindAudioCaptureDevice(); // return audio source filter

    // Add Video source filter
    hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
    DsError.ThrowExceptionForHR(hr);
    // Add audio source filter
    hr = this.graphBuilder.AddFilter(audiosourceFilter,"Audio Capture");
    DsError.ThrowExceptionForHR(hr);

    //set outputname "Test.avi" .avi type
    hr = this.captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, "Test.avi", out asfWriter, out pTmpSink);
    DsError.ThrowExceptionForHR(hr);

    //render preview video on window form
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null);
    DsError.ThrowExceptionForHR(hr);

    //render Audio preview   
    //hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Audio, audiosourceFilter, null, null);
    //DsError.ThrowExceptionForHR(hr);

    // Render Video to Test.avi
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, sourceFilter, null, asfWriter);
    Marshal.ThrowExceptionForHR(hr);
    // Render Audio into Test.avi
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Audio, audiosourceFilter, null, asfWriter);
    Marshal.ThrowExceptionForHR(hr);


    Marshal.ReleaseComObject(sourceFilter);
    Marshal.ReleaseComObject(audiosourceFilter);

    SetupVideoWindow();


    rot = new DsROTEntry(this.graphBuilder);


    hr = this.mediaControl.Run();
    DsError.ThrowExceptionForHR(hr);


    this.currentState = PlayState.Running;
  }
  catch
  {
    MessageBox.Show("An unrecoverable error has occurred.");
  }
}

OK,

public void CaptureVideo()
{
  int hr = 0;
  IBaseFilter sourceFilter = null;
  IBaseFilter audiosourceFilter = null;
  IBaseFilter asfWriter = null;
  IFileSinkFilter pTmpSink = null;

  try
  {

    GetInterfaces();
    hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
    DsError.ThrowExceptionForHR(hr);

    sourceFilter = FindVideoCaptureDevice(); //return Video source filter
    audiosourceFilter = FindAudioCaptureDevice(); // return audio source filter

    // Add Video source filter
    hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
    DsError.ThrowExceptionForHR(hr);
    // Add audio source filter
    hr = this.graphBuilder.AddFilter(audiosourceFilter,"Audio Capture");
    DsError.ThrowExceptionForHR(hr);

    //set outputname "Test.avi" .avi type
    hr = this.captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, "Test.avi", out asfWriter, out pTmpSink);
    DsError.ThrowExceptionForHR(hr);

    //render preview video on window form
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null);
    DsError.ThrowExceptionForHR(hr);

    //render Audio preview   
    //hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Audio, audiosourceFilter, null, null);
    //DsError.ThrowExceptionForHR(hr);

    // Render Video to Test.avi
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, sourceFilter, null, asfWriter);
    Marshal.ThrowExceptionForHR(hr);
    // Render Audio into Test.avi
    hr = this.captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Audio, audiosourceFilter, null, asfWriter);
    Marshal.ThrowExceptionForHR(hr);


    Marshal.ReleaseComObject(sourceFilter);
    Marshal.ReleaseComObject(audiosourceFilter);

    SetupVideoWindow();


    rot = new DsROTEntry(this.graphBuilder);


    hr = this.mediaControl.Run();
    DsError.ThrowExceptionForHR(hr);


    this.currentState = PlayState.Running;
  }
  catch
  {
    MessageBox.Show("An unrecoverable error has occurred.");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文