1

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

I have an event that provides (raw) frames (1920x1080) (25fps) from IpCamera. 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.
My question is how can I write the frame to ConvertLiveMediaTask in faster way? are there any possibilites to write it in background? Please give me suggestions.

Balu
  • 137
  • 2
  • 11

2 Answers2

0

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). You must know that real-time encoding of FullHD is VERY CPU consuming task; 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.

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
0

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

Edgars Pivovarenoks
  • 1,526
  • 1
  • 17
  • 31