How to import live video stream and capture one frame - c#

How would I go about capturing one frame from a feed of video from a webcam or video capture card in C#? I want to display the live feed and have a method that takes one frame and saves it to a remote server either via FTP or over a shared network path.

If you are able to streaming live video, then you can capture the live video stream just click on button by this code:
private Capture capture = null;
private void btnStart_Click(object sender, System.EventArgs e)
{
try
{
if ( capture == null )
throw new ApplicationException( "Please select a video and/or audio device." );
if ( !capture.Cued )
capture.Filename = txtFilename.Text;
capture.Start();
btnCue.Enabled = false;
btnStart.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show( ex.Message + "\n\n" + ex.ToString() );
}
}

You could use OpenCV. If you search on StackOverflow you'll find a lot of source on how to do that.
There's even .NET wrappers for OpenCV like opencvdotnet and Emgu CV.
You will probably end up using a few functions from the library, such as cvCaptureFromCAM() and cvQueryFrame().

Related

Video is not rendered in accord videoSourcePlayer control

I have an accord videoSourcePlayer control in my form.Why is it that it is not rendering the video that I select? Please see my code below:
// Open video file using DirectShow
private void openVideoFileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// create video source
FileVideoSource = new FileVideoSource(openFileDialog.FileName);
// open it
sourceInitialiization = true;
OpenVideoSource(FileVideoSource);
}
}
// Open video source
private void OpenVideoSource(IVideoSource source)
{
// set busy cursor
this.Cursor = Cursors.WaitCursor;
// close previous video source
CloseVideoSource();
// start new video source
videoSourcePlayer.VideoSource = new AsyncVideoSource(source);
videoSourcePlayer.Start();
// reset statistics
statIndex = statReady = 0;
// start timers
timer.Start();
alarmTimer.Start();
//alarmTimer1.Start();
videoSource = source;
this.Cursor = Cursors.Default;
}
In my laptop where I initially coded this program, this works perfectly, but if I transfer it to another machine, say a desktop or another laptop, the code doesn't work anymore. It runs but it doesn't render the video and there is not error detected in the debugger too.
I tried downloading a sample video project from accord framework but I still couldn't get it to play a video on the desktop except on my laptop. What am I missing? Thank you.
Have you subscribed to the NewFrameReceived event?

Take Snapshot using webcam and EmguCV in c# in console application

I need a simple, but complete, example of capturing a snapshot from Logitech USB webcam in a console application, but I don't find.
The next instruction not works.
private Capture _capture = null; //Camera
Where is Capture definition? In the Emgu.CV.World not it is.
I use the 3.2.0 version of EmguCV.
http://www.emgu.com/wiki/index.php?title=Camera_Capture
Thank you in advance.
The error is the attached image
Did you read the entire article? It is instantiated here:
private void SetupCapture(int Camera_Identifier)
{
//update the selected device
CameraDevice = Camera_Identifier;
//Dispose of Capture if it was created before
if (_capture != null) _capture.Dispose();
try
{
//Set up capture device
_capture = new Capture(CameraDevice);
_capture.ImageGrabbed += ProcessFrame;
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
Hope this helps.
Doug

download audio from youtube c#

I'm trying to download audio from youtube using YoutubeExtractor but if click the button i have information "sequence has no elements". How i can solve it?
private void button2_Click(object sender, EventArgs e)
{
try
{
IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(textBox1.Text);
VideoInfo video = videos.Where(info => info.CanExtractAudio).OrderByDescending(info => info.AudioBitrate).First();
if (video.RequiresDecryption)
{
DownloadUrlResolver.DecryptDownloadUrl(video);
}
AudioDownloader download = new AudioDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.AudioExtension));
download.Execute();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The issues is the call to First(). This will throw an exception if your IEnumerable contains no items.
To correct this, you need to be sure of two things:
The DownloadUrlResolver.GetDownloadUrls() function returns at least 1 item.
That at least one item in the videos variable has CanExtractAudio set to true.

X264 recording video Emgucv C#

I'm writing a program to save some webcam video to a file. I'm using the x264 codec found here x264.
When I try writing frames to a file I get this warning message poping up. It still records the video. But I don't want this popup showing on the screen.
x264vfw [warning]: Few frames probably would be lost. Ways to fix this:
x264vfw [warning]: -if you use VirtualDub or its fork than you can enable 'VirtualDub Hack' option
x264vfw [warning]: -you can enable 'File' output mode
x264vfw [warning]: -you can enable 'Zero Latency' option
Is there anyway to config "File ouput mode" or "Zero Latency" by using C# or C++ or command line?
This is the code:
public static void StartCapture()
{
try
{
capture = new Capture();
capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 1920); //1920
capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 1080); //1080
CaptureOutput = new VideoWriter
(
"capture output.avi",
CvInvoke.CV_FOURCC('X','2','6','4'),
50, //fps
(int)capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH),
(int)capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT),
true
);
if (capture != null)
{
capture.ImageGrabbed += SaveFrame;
capture.Start();
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
static void SaveFrame(System.Object sender, EventArgs e)
{
Image<Bgr, Byte> video;
video = capture.RetrieveBgrFrame();
CaptureOutput.WriteFrame(video);
}

How to Convert a Metafile to Image by Drag'n'Droping in a Winform

I develop a Winform Application with the framlework .NET 3.5 in C#.
I would like to allow the user to drag&drop a picture from Word 2007. Basically the user open the docx, select a picture and drag&drop them to my PictureBox.
I've already done the same process with picture files from my desktop and from Internet pages but I can't go through my problem with my Metafile. I've done few researches but I didn't find any solutions solving my issue.
Here is what I've done on my Drag&Drop event :
private void PictureBox_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.MetafilePict)){
Image image = new Metafile((Stream)e.Data.GetData(DataFormats.MetafilePict));
}
}
I can obtain a stream with this code : (Stream)e.Data.GetData(DataFormats.MetafilePict) but I don't know how to convert it into a Metafile or better an Image object.
If you have any idea or solution, I'll be glad to read it.
Thanks,
Here is a working example of Drag n Drop from Word (not for PowerPoint and Excel):
static Metafile GetMetafile(System.Windows.Forms.IDataObject obj)
{
var iobj = (System.Runtime.InteropServices.ComTypes.IDataObject)obj;
var etc = iobj.EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR.DATADIR_GET);
var pceltFetched = new int[1];
var fmtetc = new System.Runtime.InteropServices.ComTypes.FORMATETC[1];
while (0 == etc.Next(1, fmtetc, pceltFetched) && pceltFetched[0] == 1)
{
var et = fmtetc[0];
var fmt = DataFormats.GetFormat(et.cfFormat);
if (fmt.Name != "EnhancedMetafile")
{
continue;
}
System.Runtime.InteropServices.ComTypes.STGMEDIUM medium;
iobj.GetData(ref et, out medium);
return new Metafile(medium.unionmember, true);
}
return null;
}
private void Panel_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.EnhancedMetafile) & e.Data.GetDataPresent(DataFormats.MetafilePict))
{
Metafile meta = GetMetafile(e.Data);
Image image = meta;
}
}
After this you can use image.Save to save picture or you can use it on picturebox or other control.
I think you need to call new Metafile(stream) as there is no method .FromStream in Metafile.
I'm still digging into he web to try different way to solve my issue.
Hopefully I've found this unanswered thread talking about my problem but without any response :
Get Drag & Drop MS Word image + DataFormats.EnhancedMetafile & MetafilePict :
http://www.codeguru.com/forum/showthread.php?t=456722
I work around with another io be able to copy floating Image (Image stored in Shape and not InlineShape) with Word 2003 and pasting into my winform. I can't paste the link of the second source (because of my low reputation on this website) but I'll do if someone request.
So apparently there are a common issue with the fact that you cannot access to your Metafile stored in the Clipboard and by Drag&Drop.
I still need to understand how to get my Metafile by Drag&Drop.

Categories

Resources