简体   繁体   English

如何使用Directshow和C#在文本覆盖下进行视频录制?

[英]How to do video recording with Text overlay using Directshow and C#?

According to my previous post i have tried to use sampleGrabber which will grab frames from video file and then it calls call back function: 根据我以前的帖子,我尝试使用sampleGrabber来从视频文件中获取帧,然后调用回调函数:

Type comType = Type.GetTypeFromCLSID(new Guid("e436ebb3-524f-11ce-9f53-0020af0ba770"));
IGraphBuilder graphBuilder = (IGraphBuilder)Activator.CreateInstance(comType);

comType = Type.GetTypeFromCLSID(new Guid("C1F400A0-3F08-11d3-9F0B-006008039E37"));
ISampleGrabber sampleGrabber = (ISampleGrabber)Activator.CreateInstance(comType);

graphBuilder.AddFilter((IBaseFilter)sampleGrabber, "samplegrabber");

AMMediaType mediaType = new AMMediaType();
mediaType.majorType = MediaType.Video;
mediaType.subType = MediaSubType.RGB24;
mediaType.formatType = FormatType.VideoInfo;
sampleGrabber.SetMediaType(mediaType);

int hr = graphBuilder.RenderFile(@"D:\test.wmv", null);

IMediaEventEx mediaEvent = (IMediaEventEx)graphBuilder;
IMediaControl mediaControl = (IMediaControl)graphBuilder;
IVideoWindow videoWindow = (IVideoWindow)graphBuilder;
IBasicAudio basicAudio = (IBasicAudio)graphBuilder;

videoWindow.put_AutoShow(OABool.False);
basicAudio.put_Volume(-10000);

sampleGrabber.SetOneShot(false);
sampleGrabber.SetBufferSamples(true);

//the same object has implemented the ISampleGrabberCB interface.
//0 sets the callback to the ISampleGrabberCB::SampleCB() method.
sampleGrabber.SetCallback(this, 0);

mediaControl.Run();

EventCode eventCode;
mediaEvent.WaitForCompletion(-1, out eventCode);


Marshal.ReleaseComObject(sampleGrabber);
Marshal.ReleaseComObject(graphBuilder);

call back function 回叫功能

   public int SampleCB ( double sampleTime, IMediaSample mediaSample )
   {
    //WHAT TO DO HERE.
   }
  1. What do I do in the call back function to add overlay on each frame and then whole video will get store with overlay text? 我该如何在回叫功能中在每帧上添加叠加层,然后整个视频将与叠加层文本一起存储?

  2. Is there any way to add overlay text when video is recording? 录制视频时,是否可以添加叠加文字?

The best way to add a text-overlay during the recording is to implement a DMO or a DirectShow Transform-Filter . 在录制过程中添加文本覆盖的最佳方法是实现DMODirectShow Transform-Filter I would prefer the DMO, because it is simpler and you can reuse it later in MediaFoundation. 我更喜欢DMO,因为它更简单,您以后可以在MediaFoundation中重用它。

You set RGB24 or RGB32 as Input-type for the DMO/Filter and then you can draw with GDI what you want on each frame. 您将RGB24或RGB32设置为DMO /滤镜的输入类型,然后可以使用GDI在每个帧上绘制所需的内容。

In the Graph it looks this way: VideoSource -> DMO -> ASF Writer . 在Graph中看起来是这样的: VideoSource -> DMO -> ASF Writer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM