关于在DirectShowNet中渲染流的问题

时间:2011-06-09 16:53:38

标签: c# directshow directshow.net

目前我有一个毁坏的avi文件,我的程序创建。我发现通过进入graphedit,我可以正确地重新配置它。

我发现如果我这样做:

SourceFile(test1.avi) -> AVI Splitter -> ffdshow video encoder -> AVI Mux -> File Writer

我可以恢复正确的视频流。

现在我正在尝试将其转换为我可以调用的C#Directshow.Net程序。

我创建了一个图形,我从想要转换的视频文件中添加了SourceFilter,我添加了一个抓取器,找到然后添加ffdshow编码器,AVI Mux然后再添加setOutputFileName()。

{
        filename = textBox1.Text;
        IFilterGraph2 filter = new FilterGraph() as IFilterGraph2;
        ICaptureGraphBuilder2 MainGraph = new CaptureGraphBuilder2() as ICaptureGraphBuilder2;
        int result;
        result = MainGraph.SetFiltergraph(filter);

        IBaseFilter source;
        result = filter.AddSourceFilter(filename, filename, out source);

        ISampleGrabber samplegrabber = (ISampleGrabber)new SampleGrabber();
        IBaseFilter basegrab = (IBaseFilter)samplegrabber;

        AMMediaType media;
        media = new AMMediaType();
        media.majorType = MediaType.Video;
        media.subType = MediaSubType.RGB32;
        media.formatType = FormatType.VideoInfo;
        result = samplegrabber.SetMediaType(media);

        DsUtils.FreeAMMediaType(media);
        result = samplegrabber.SetBufferSamples(true);

        result = filter.AddFilter(basegrab, "filter");

        //MainGraph.RenderStream(null, null, source, basegrab, null);



        IMediaEvent mediaEvent;
        IMediaControl mediacontrol;
        mediaEvent = filter as IMediaEvent;
        mediacontrol = filter as IMediaControl;






        IntPtr hEvent;
        result = mediaEvent.GetEventHandle(out hEvent);

        ManualResetEvent mre;

        mre = new ManualResetEvent(false);
        mre.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(hEvent, true);

        //Thread t = new Thread(new ThreadStart(this.EventWait));

        //mediacontrol.Run();

        object newsource = null;
        IBaseFilter encoder;

        Guid iid = typeof(IBaseFilter).GUID;
        foreach (DsDevice device in DsDevice.GetDevicesOfCat(FilterCategory.VideoCompressorCategory))
        {
            if (device.Name.CompareTo("ffdshow video encoder") == 0)
            {
                device.Mon.BindToObject(null, null, ref iid, out newsource);
                break;
            }
        }

        encoder = (IBaseFilter)newsource;
object newsource2 = null;
        IBaseFilter encoder2;

        Guid iid2 = typeof(IBaseFilter).GUID;
        foreach (DsDevice device in DsDevice.GetDevicesOfCat(FilterCategory.LegacyAmFilterCategory))
        {
            if (device.Name.CompareTo("AVI Mux") == 0)
            {
                device.Mon.BindToObject(null, null, ref iid2, out newsource2);
                break;
            }
        }

        encoder2 = (IBaseFilter)newsource2;
        result = filter.AddFilter(encoder, "encoder");
        result = filter.AddFilter(encoder2, "Avi Mux");


        FileWriter file = new FileWriter();
        IFileSinkFilter fs = (IFileSinkFilter)file;
        IBaseFilter mux;
        IFileSinkFilter sink;
        Guid cat;
        Guid med;
  cat = PinCategory.Capture;
        med = MediaType.Video;


        //result = filter.AddFilter(encoder, "encoder");
        result = MainGraph.SetOutputFileName(MediaSubType.Avi, "C:\\Users\\Caleb\\Desktop\\conversion.avi", out mux, out sink);

        result = MainGraph.RenderStream( cat,  med, source, null, mux);
mediacontrol.Run();

}

这段代码根据我的知识创建了相同的图形。它创建我的文件,但不转换任何东西。它创建的文件是0bytes。我在渲染错误或我的代码中出现其他错误吗?

1 个答案:

答案 0 :(得分:1)

我输入了一段代码:

DsRotEntry m_rot = new DsRotEntry(filter);

这让我可以用graphedit查看我制作的滤镜。

发生的事情是我添加了过滤器,但没有一个互相连接。

那么我所做的就是得到了我添加的每个滤波器,找到了与它们相关的输入和输出引脚

Filter2.FindPin( "name of the input pin here", out pinName);
Filter1.FindPin( "name of the output pin here", out PinName2);

MainFilter.connect( PinName2, pinName);

这在过滤器之间创建了一个连接。我没有这样做,我做了这个,现在我越来越接近我想要的。

所以基本上对于Directshow.net和DirectShowNet-Lib它的程序就像

find a filter -> add a filter -> find a filter -> add a filter -> find the pins -> connect the pins -> run