How can I capture video and play it with C#? - 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)

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

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.

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.

You can try the DirectShow.net library.

I can recommend WPF's ffmediaelement. It's built on FFmpeg. So what can FFmpeg, can also ffmediaelement. 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";
};

Related

Basic video editing using C# Windows Forms

I'm building a C# application (Windows Forms and .NetFramework 4.8). I use the Accord.Net framework to record a video in .mp4 format on the computer. After recording, I am using the Windows Media Player control to play the video that was recorded.
string applicationFolder = Application.StartupPath;
if (File.Exists(applicationFolder + #"\temp\videoTemp.mp4"))
{
axWindowsMediaPlayer1.URL = applicationFolder + #"\temp\videoTemp.mp4";
}
I need to edit this video by adding a text at the bottom for a specific time. During recording I can add text using the command below before saving the frame in the .mp4 file:
using (Graphics g = Graphics.FromImage(currentFrame))
{
//g.DrawString("test"...);
}
I thought of doing the same with the video that is played by the Windows Media Player control. For that I looked for some event of the player where I can get a pointer to each new frame of the video playing, but no success.
When creating a Windows Media Player Graphics, the text is reproduced in the video, but only when the application is running, the saved video is not affected.
using (Graphics g = axWindowsMediaPlayer1.CreateGraphics())
{
//g.DrawString("test"...); //it's show only when application is running
}
The question: Does Windows Media Player have any feature where I can capture the current frame of the video being played? If not, is there any way to recover a video saved on the computer, edit it frame by frame and then save it by overwriting the old video using C#?
Sorry for my English. Thank you everyone.
Does Windows Media Player have any feature where I can capture the current frame of the video being played?
No, it's a player. And even if it did, it certainly can't write the result. Since it's a player and all.
If not, is there any way to recover a video saved on the computer, edit it frame by frame and then save it by overwriting the old video using C#?
I generally use ffmpeg for that, it's not terribly difficult and it has hardware acceleration both for decoding and recoding built-in.

Play two sounds parallel in background agent

I have a mp3 file. I have a background sound file like rain sound or some fire sound. I want to play these two files parallel in the app as well as in background. So that it looks like a single music is playing a story with a background. Please provide any guidance to achieve this.
AFAIK there are four ways to play audio in WP: BackgroundAudioPlayer, MediaElement, SoundEffect, Microsoft Media Foundation. As for your question:
BackgroundAudioPlayer - it won't work, as there is only one Instance of BAP on the phone. So to play a different file, you have to change Track's Source, so it won't handle two at the same time.
MediaElement - it's also a bad idea (it won't also run with BAP simultanously), because as MSDN says:
When a MediaElement control plays audio or video content, any background sounds or media already playing are halted. The app launches the playback experience when the user taps the control. Only one MediaElement control can operate at a time.
you can try to play many sounds using SoundEffect (thougt as Documentation says it can be only a wave file), I won't post code here, because there are already many examples: one, two, three
as for Microsoft Media Foundation: Walkthrough, Programming Guide, Supported file formats

C# playing multiple audio files at once from your resources?

I'd like to be able to press a button to play a sound of a button being pressed while music is already playing. SoundPlayer is no help because it stops the music to play the sound.
I have added the sounds to my Resources (WindowsFormsApplication2.Resources.Properties.button) and I don't want to have to type in the file location.
Answer from MerickOWA
You CANNOT play two sounds at once using SoundPlayer.
SoundPlayer is using the native WINAPI PlaySound function to accomplish the task which has no support for playing simultaneous sounds. Creating multiple instances of SoundPlayer won't help.
There are many options most of which involve implementing a lot of the low level API to window's native audio library or DirectSound (note neither are C# and require alot of interop code)
The simplest option would be to depend on windows media player to play the audio for you.
Add a reference to "C:\Windows\System32\wmp.dll"
then use
var player = new WMPLib.WindowsMediaPlayer();
player.URL = #"..\..\bin\debug\tribal dance.wav";
Note: play starts immediately after setting the URL property
The down side of this approach is your reliance on media player for your application to work properly. On the upside, you can use any file format media player supports.

Capturing Images from Playing Video File

I am developing C# desktop application and referring this link to Play video file http://www.codeproject.com/Articles/2632/DirectShow-MediaPlayer-in-C , now from this playing video file ,when I click on this panel (shown in above link ) how to capture Images from playing video file,I searched but I found all examples showing capturing Images from some device(for eg.webcam,etc..) ,In my case Its not device it's file which I am browsing from Directory and Playing ,as I am new to this Video capturing Please direct me how I can do this
"Capture" normally refers to image acquisition when you obtain the image from external source. You want to take a copy of currently displayed video frame instead. There are several ways to do it and when it comes to playback when video is being visualized, the video renderers typically offer GetCurrentImage method which returns a copy of currently displayed video frame, possibly even converted to RGB.
Method syntax slightly differs between video renderer versions.
See previous topics on achieving this:
take picture from webcam c# (references sample application from DirectShow.NET)
IBasicVideo CetCurrentImage catastrophic failure(DirectShow.NET)
Also:
Best DirectShow way to capture image from web cam preview ? SampleGrabber is deprecated
Grabbing images from a DVD using direct show
You need a filter which captures screen and sends the video down the stream.
In the DirectShow Library there is a sample filter called PushSource and inside there is PushSourceDesktop.
At Codeplex there are Examples like this (Click me) or Information (Click me) about Filters.
You can also use a "SampleGrabber" DirectShow filter: just place it in the filter chain between your source filter and render filter.
More info about SampleGrabber:
see MSDN

How to control HD video play position and speed in XNA?

I am developing a game for Windows in C# using Visual Studio 2010 and XNA 4.0. I would like to be able to set and change the play position of an HD video and also play the video in reverse, depending on user input.
I am having trouble finding where to start. XNA's videoPlayer class does not provide these type of functions. I've read that XNA DirectShow is now out of date and slow when using HD video.
I don't quite understand how I would be able to use or implement tools such as ffmpeg with my project. It seems some people have had similar questions and posted solutions but without much detail. These are below.
interop out to talk to the core DX functionality.
write a managed c++ wrapper to interop ffmpeg.
write an mpeg decoder.
I am not sure what would be best and where to begin.
Thanks!
The VideoPlayer class has a "PlayPosition" property, which you should be able to play with.
Otherwise (and I don't know how big your video file is nor how long) try an image sequence and animate the current image sequence and control that with user input. Of course working with image sequences would make audio reversal (if there is audio) etc very complicated.
Last but not least, you can see if you can figure anything out from this mpeg decoder here:
https://www.box.com/shared/ojzfv0qzfx
Something else that might help with mpeg decoding:
http://chrisa.wordpress.com/2007/11/21/decoding-mpeg2-information/

Categories