Skip to main content
 首页 » 编程设计

c#之有没有另一种方法可以在 directshow 中保存视频

2024年02月23日15开发

我正在使用 DirectShow.Net,我正在从网络摄像头捕获视频并将其保存为 AVI 文件。 AVI 文件可能会变大,我想以不同的格式保存它。我正在使用 ICaptureGraphBuilder2::SetOutputFileName(MediaSubType.type, name, out, out)。 ICaptureGraphBuilder 将只允许我使用 .AVI 或 .ASF 的 MediaSubType 来保存文件。如果我尝试更改它尝试另存为的类型,它将崩溃:

            graphBuilder = (IGraphBuilder) new FilterGraph(); 
 
        //Create the Capture Graph Builder 
 
        ICaptureGraphBuilder2 captureGraphBuilder = null; 
        captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2(); 
 
        //Create the media control for controlling the graph 
        mediaControl = (IMediaControl) this.graphBuilder; 
 
        // Attach the filter graph to the capture graph 
        int hr = captureGraphBuilder.SetFiltergraph(this.graphBuilder); 
        DsError.ThrowExceptionForHR(hr); 
 
        //Add the Video input device to the graph 
        hr = graphBuilder.AddFilter(theDevice, "source filter"); 
        DsError.ThrowExceptionForHR(hr); 
 
 
        //Add the Video compressor filter to the graph 
        hr = graphBuilder.AddFilter(theCompressor, "compressor filter"); 
        DsError.ThrowExceptionForHR(hr); 
 
        //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink 
        IBaseFilter mux; 
        IFileSinkFilter sink; 
        hr = captureGraphBuilder.SetOutputFileName(MediaSubType.MPEG1Video, textBox1.Text, out mux, out sink); 
        captureGraphBuilder.SetOutputFileName 
 
        DsError.ThrowExceptionForHR(hr); 
 
 
        //Render any preview pin of the device 
        hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theDevice, null, null); 
        DsError.ThrowExceptionForHR(hr); 

有没有一种方法可以轻松地保存为 AVI 或 ASF 以外的任何格式?

请您参考如下方法:

问题很可能不是 AVI 容器格式,而是您用来压缩视频的编解码器。

您将需要添加一个不同的视频压缩过滤器(可能是 MPEG4 或 AVC),无论您得到什么,都可能还会提供您可以而且应该使用的 MP4 多路复用器,而不是您现在使用的 avi 多路复用器。