2

我正在使用 DirectShow.NET,我需要编写一个显示自定义字幕的视频播放器。

我的代码基于示例的 Capture.cs,以下是相关部分:

private void SetupGraph(string FileName, Control hWin)
    {
        int hr;
        ISampleGrabber m_sampGrabber = null;
        IGraphBuilder gb = null;

        // Get the graphbuilder object
        m_FilterGraph = new FilterGraph() as IFilterGraph2;
        gb = m_FilterGraph as IGraphBuilder;

        // Get a ICaptureGraphBuilder2 to help build the graph
        ICaptureGraphBuilder2 icgb2 = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;

        try
        {
            // Link the ICaptureGraphBuilder2 to the IFilterGraph2
            hr = icgb2.SetFiltergraph(gb);
            DsError.ThrowExceptionForHR(hr);
            // Add the filters necessary to render the file.  This function will
            // work with a number of different file types.
            IBaseFilter sourceFilter = null;
            hr = gb.AddSourceFilter(FileName, FileName, out sourceFilter);
            DsError.ThrowExceptionForHR(hr);

            // Get the SampleGrabber interface
            m_sampGrabber = (ISampleGrabber)new SampleGrabber();
            IBaseFilter baseGrabFlt = (IBaseFilter)m_sampGrabber;

            // Configure the Sample Grabber
            ConfigureSampleGrabber(m_sampGrabber);

            // Add it to the filter
            hr = gb.AddFilter(baseGrabFlt, "Ds.NET Grabber");
            DsError.ThrowExceptionForHR(hr);

            hr = gb.RenderFile(FileName, null);
            DsError.ThrowExceptionForHR(hr);

            // Configure the Video Window
            IVideoWindow videoWindow = gb as IVideoWindow;
            ConfigureVideoWindow(videoWindow, hWin);

            // Connect the pieces together, use the default renderer
            hr = icgb2.RenderStream(null, null, sourceFilter, baseGrabFlt, null);
            DsError.ThrowExceptionForHR(hr);


            // Now that the graph is built, read the dimensions of the bitmaps we'll be getting
            SaveSizeInfo(m_sampGrabber);

            // Configure the Video Window
            videoWindow = gb as IVideoWindow;
            ConfigureVideoWindow(videoWindow, hWin);
            hr = videoWindow.put_MessageDrain(hWin.FindForm().Handle);

        }
...

如您所见,现在我正在播放 2 个视频,一个使用 RenderFile() 打开以播放音频,另一个使用 RenderStream 打开以在其上添加字幕(因此有两个 videoWindow 配置)。

这显然不是最佳解决方案,但我不知道如何通过 RenderStream 获取音频。

4

0 回答 0