Joshua Frank
Joshua Frank

Reputation: 13838

How can I preview and capture in DirectShow with segmented output files?

I'm using c# and DirectShow.NET, along with some sample code found online, to try to display the current webcam output in real time to a window in my application and output the video to files.

Currently, I'm using ICaptureGraphBuilder2 to create a graph and output a single output file and it works. But I actually have a more detailed requirement that I cannot figure out how to implement:

When recording, I want to be able to slice the output into 5 minute segments, like the -segment parameter in ffmpeg. As far as I can tell, ICaptureGraphBuilder2 chooses the file writer for you, and the default writer just keeps appending to the same file. How can this be customized? How could I implement a custom writer and get the graph to use that instead?

Upvotes: 1

Views: 251

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69672

Mostly because the API itself belongs to ancient times when segmenting long recordings was not the most important feature, doing segmented writing with DirectShow is not easy. It is impossible with standard API provided by OS, and requires some effort with third party filters and/or development.

The fundamental problem you have are that media pipeline does not allow state transition of parts: you cannot have webcam active while written file is closed, finalized, reopened and segmented.

So you have to address the challenge in one of the two ways:

  1. You can replace multiplexer and writer filters with custom filter that segment recording internally, and finalize the current file starting new one without losing a frame.

As you are doing C# development, this option basically suggests that you possibly find ready to use third party filter that implement the necessary functionality.

  1. You can build a two-graph design with one graph having webcam capture and optionally compression, then the other one having compression is the first one does not have it and actual recording into file. Then data is transferred between the graphs and when it comes to segmenting you stop second graph finalizing the file, then start new file there, and the first graph is always running without interruption supplying new video frames. The glue layer that connects to graphs can be custom developed or you can check out GMFBridge filters which are popular and are used by quite some users.

To my best knowledge you can use GMFBridge to build two-graph topologies with segmenting of output files, even though it might require that you check the source code to find out how the filters are consumed correctly and what it takes to make them work in this scenario of live video capture.

Upvotes: 1

Related Questions