How to make screen capture video using Direct Show.net Library?

前端 未结 2 1972
盖世英雄少女心
盖世英雄少女心 2021-02-11 07:21

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 c

相关标签:
2条回答
  • 2021-02-11 07:53

    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.");
      }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题