简体   繁体   中英

Recording video in C#

I have to do the following: record video from camera using C#. Camera I use produces video frames (framerate is not fixed) and I have to somehow put all the frames together and create video file. Also I need to use different codecs such as AVI or MPEG4 (these codecs are required, others are optional).

The main problem I faced is how to create video in which framerate is not fixed. I have a stream of frames, for example I can recive the first frame after 1ms, then after 20ms, then 36ms and so on. If I create video with 25 frames/second the result will be wrong because it means that frames are added after 40ms.

I tried to use Aforge.Video library, it has a method http://www.aforgenet.com/framework/docs/html/84a560df-bfd5-e0d6-2812-f810b56a254d.htm which adds a frame according to timespan. But this method has problems with setting bitrate - the bitrate value I pass to the method simply ignored ( http://www.aforgenet.com/forum/viewtopic.php?f=2&t=2665 ).

So my questions is: is there some C# library which I can use to video recording? I have to support AVI and MPEG4, also possibility to set bitrate and the last but not the least - record video with variable framerate.

Thanks in advance. Any suggestions would be very helpful.

UPD1: I can't connect to camera directly - all I have is a stream of frames and I need to convert this stream to video at run-time.

UPD2: A library I'm looking for should satisfy the following properties. It has to contain a method (or some way how to do the same) to add next frame with a timestamp just like in Aforge.Video.FFMPEG:

public void WriteVideoFrame(Bitmap frame, TimeSpan timestamp)

And it should be possible to choose different codecs (at least AVI and MPEG4) and also to set bitrate. Is there some alternatives to Aforge.Video.FFMPEG? Because Aforge doesn't work properly - the bitrate value is ignored, also some codecs are not supported (MPEG2 for example).

UPD3: One more questions about codec license. If I use open source library, should I worry about the codec license?

  • AVI and MPEG4 are not codecs but container formats
  • APIs and Libraries are all well known, the problem here is that specific formats and codecs have their own limitations, eg AVI file has to have fixed frame rate for video (with a workaround that you might have high FPS file with skipped frames)
  • Library search and recommendation Qs are considered off-topic and opinion based here on SO

The finest control API you typically use from C# code for video recording is DirectShow through DirectShow.NET library. You choose formats and codecs, AVI multiplexer is available as a stock component, and there are third party replacements. For MPEG-4 ( .MP4 ) you need a third party component, eg this . Video compression component is typically subject to third party filter choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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