简体   繁体   English

c#-将实时帧录制为视频的最佳方法是什么

[英]c# what is the best way to record live frames as Video

I have been checking around to convert live frames into video. 我一直在检查以将实时帧转换为视频。 And I found (NReco.VideoConverter) ffmpeg lib to convert live frames to Video, but the problem is it is taking time to write each frame to ConvertLiveMediaTask (async live media task conversion). 而且我发现(NReco.VideoConverter)ffmpeg库可以将实时帧转换为Video,但是问题是将每个帧写入ConvertLiveMediaTask(异步实时媒体任务转换)需要花费时间。

I have an event that provides (raw) frames (1920x1080) (25fps) from IpCamera. 我有一个事件可提供IpCamera的(原始)帧(1920x1080)(25fps)。 Whenever I get frame I am doing the following 每当我得到镜框时,我都会执行以下操作

//Image availbale event fired
//...

//...
// Record video is true
if(record)
{
//////////////############# Time taking part ##############//////////////////////
var bd = frameBmp.LockBits(new Rectangle(0, 0, frameBmp.Width, frameBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);                        
  var buf = new byte[bd.Stride * frameBmp.Height];                       
  Marshal.Copy(bd.Scan0, buf, 0, buf.Length);
  // write to ConvertLiveMediaTask
  convertLiveMediaTask.Write(buf, 0, buf.Length); // ffMpegTask
  frameBmp.UnlockBits(bd);
//////////////////////////////////////////////////////////////////////////////////
}

As the above part is taking much time, I am loosing the frames. 由于上面的部分要花很多时间,因此我失去了框架。

//Stop recording
convertLiveMediaTask.Stop(); //ffMpegTask

Stop recording, for this part I have used BackgroundWorker, because this takes too smuch time to save the media to file. 停止记录,这部分我使用了BackgroundWorker,因为将媒体保存到文件花费了太多时间。
My question is how can I write the frame to ConvertLiveMediaTask in faster way? 我的问题是如何以更快的方式将框架写入ConvertLiveMediaTask? are there any possibilites to write it in background? 有没有可能在后台编写它? Please give me suggestions. 请给我建议。

I'm sure that most time takes encoding and compressing raw bitmaps (if you encode them with h264 or something like that) by FFMpeg because of FullHD resolution (NReco.VideoConverter is a wrapper to FFMpeg). 我敢肯定,由于FullHD分辨率(NReco.VideoConverter是FFMpeg的包装器),大多数时间都需要通过FFMpeg对原始位图进行编码和压缩(如果使用h264或类似的方式进行编码)。 You must know that real-time encoding of FullHD is VERY CPU consuming task; 您必须知道FullHD的实时编码是非常消耗CPU的任务。 if your computer is not able to do that, you may try to play with FFMPeg encoding parameters (decrease video quality / compression ratio etc) or use encoder that requires less CPU resources. 如果您的计算机无法执行此操作,则可以尝试使用FFMPeg编码参数(降低视频质量/压缩比等)或使用需要较少CPU资源的编码器。

If You need to record some limited time live stream, You can split video capturing and compressing/saving into two threads. 如果您需要录制一些有限时间的实时流,则可以将视频捕获和压缩/保存分为两个线程。

Use for example ConcurrentQueue to buffer live frames (En-queue) on one thread without delay, and other thread could save those frames at a pace it can (De-queue). 例如,使用ConcurrentQueue在一个线程上无延迟地缓冲实时帧(入队),而其他线程可以按其可能的速度保存这些帧(出队)。 This way you will not loose frames. 这样您就不会松动框架。

Obviously You will have strain on RAM and also after stopping Live video You will have a delay while saving thread finishes. 显然,您会在RAM上感到紧张,并且在停止实况视频之后,保存线程完成时也会有延迟。

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

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