Dusan
Dusan

Reputation: 5144

How can I capture video and play it with C#?

I want to build application which needs to be able to capture video from a web camera using C#. The captured video should be compressed using some codec (nothing special, anything available that saves space) and written to a file while capturing. A live preview of capture is not necessary.

The first question is: Which API is suitable for this and what would you recommend (I have seen DirectShow, Windows Media Foundation wrapper, etc. I am not sure which is would be best for managed environment and C#)?

I also need a video player in WPF which will play captured video. This player must be able to play captured video from an arbitrary position, pause and start/stop video.

Putting it all together, video is captured from a webcam in the background and at the same time the player plays that video being captured, but it can be paused, re-winded, stopped - something like a modern DVR.

The second question: Is it possible to create such a player using WPF MediaElement? (Confusion is about the file which is at the same time filled from the capture and played in the player)

Upvotes: 1

Views: 22341

Answers (5)

Lupa
Lupa

Reputation: 62

I can recommend WPF's ffmediaelement. It's built on FFmpeg. So what FFmpeg can do, ffmediaelement can also do. It can capture video from both the webcam and the capture card. Unlike DirectShow, it's very simple.

Here's how to set up a video source:

Media.Source = new Uri("device://dshow/?video=Osprey-460e Video Device 1C");

... and parameters for it in MediaOpening event:

Media.OnMediaOpening(s, e) =>
{
    e.Options.Input["framerate"] = "25";
    e.Options.Input["video_size"] = "768x576";
    e.Options.Input["pixel_format"] = "yuyv422";
};

Upvotes: 0

Aurgho Bhattacharjee
Aurgho Bhattacharjee

Reputation: 436

I have used the AForge set of libraries to accept the videos frame by frame. I used the private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image) function to do this.

Upvotes: 1

Cinchoo
Cinchoo

Reputation: 6322

You can try the DirectShow.net library.

Upvotes: 0

Likurg
Likurg

Reputation: 2760

A nice example of how to make all what you want: WebCam

Upvotes: 2

nickm
nickm

Reputation: 1775

I've used VlcDotNet. It is very flexible and simple to use. It has a pretty active user base as well.

I've used it for displaying live video streams as well as recording to files.

Upvotes: 0

Related Questions