Take the 2-minute tour ×

I am using DirectShow.Net and I am capturing video from a webcam and saving it to an AVI file. AVI files can get large and I would like to save it in a different format. I am using ICaptureGraphBuilder2::SetOutputFileName( MediaSubType.type, name, out, out). ICaptureGraphBuilder will only let me use a MediaSubType of .AVI or .ASF to save a file. If i try to change the type it tries to save as, it will crash:

            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);

        //Add the Video input device to the graph
        hr = graphBuilder.AddFilter(theDevice, "source filter");

        //Add the Video compressor filter to the graph
        hr = graphBuilder.AddFilter(theCompressor, "compressor filter");

        //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);


        //Render any preview pin of the device
        hr = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, theDevice, null, null);

Is there a way to save to anything other then AVI or ASF without too much hassle?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

The problem is most likely not the AVI container format but the codec that you are using to compress the video.

You will need to add a different video compression filter (probably MPEG4 or AVC) and wherever you get that will probably also supply an MP4 mux that you can and should use instead of the avi mux that you are using now.

share|improve this answer
I have CCCP codec pack installed which gives me access to ffdshow codecs. but Specifically if i change SetOutputFileName(MediaSubType.AVI) it will work. SetOutputFileName(MediaSubType.MPEG) will not work. ICaptureGraphBuilder2 which is what captureGraphBuilder is a child of only supports .AVI or .ASF –  Grant May 18 '11 at 22:49
It looks like you aren't adding a mux filter to the graph. So direct show will use the first suitable one it finds which is most likely the AVI filter. One very useful exercise is to use the graph builder app to replicate what you are doing in code and then see what kind of graph it builds when you click render pin, you may need to connect some or all of your filters by yourself in order to get the graph you expect. All that said I usually end up wiring the whole graph which is significantly more code and significantly more reliable than relying on direct show to create the graph you expect. –  Yaur May 18 '11 at 22:59
I have a graph which works. if i name my file .avi it will save a file that ends up being 90mb for 4 seconds. if i change the name to .mkv it saves the video and ends up 1.33mb for 8 seconds. big improvement. I haven't really converted graphs to code before though, specifically C# –  Grant May 18 '11 at 23:32
also the video that is captured seems to be faster than what i actually captured. do you know if there is a way to force it to run slower or feed it FPS stabilization? –  Grant May 18 '11 at 23:42
~10MB per second sounds like uncompressed video to me. You have graph that works, but I bet that it doesn't look like you think it does. Your MKV is using some compression (how are you building that graph?) which is why it is so much smaller. you can use IAMStreamConfig::SetFormat on the capture filter to set things like frame rate... also you probably want to use the capture pin instead of the preview pin. –  Yaur May 19 '11 at 0:38
show 1 more comment

Have you looked at EmguCV (www.emgu.com which is a .net wrapper for open cv. This has a number of classes in it that may help.

share|improve this answer
I will take a look at it but I have code already up and running for Directshow which captures and saves, except that it saves as .avi –  Grant May 18 '11 at 22:13
add comment

Your Answer


