how to record and save video from ip camera using AForge?

Lynx picture Lynx · Sep 17, 2012 · Viewed 8.6k times · Source

i try to do a recording function where the source is from IP Camera in C# using AForge but i cant really understand the coding as i am a new learner.. The recorded video will then saved in my computer.
as i konw this function is to write/create a video and save it in my storage.

AVIWriter writer = new AVIWriter("DIB ");
// create new AVI file and open it
writer.Open(@"C:\test.avi", 768, 576);
// create frame image
Bitmap image = new Bitmap(768, 576);

for (int i = 0; i < 240; i++)
{
// update image
image.SetPixel(i, i, Color.Red);
// add the image as a new frame of video file
writer.AddFrame(image);
}
writer.Close(); 

but how to read the mjepg form source file(IP cam) and pass it to AVIWriter? can someone give me guidance on how to achieve that

new learner, please guide me....

Answer

BerBuz picture BerBuz · Nov 18, 2012

You need to reference AForge.video AForge.video.VFW

If your camera provides an MJPEGstream then ( sorry it’s in VB )

Imports AForge.Video
Imports AForge.Video.VFW
'…
Dim VideoStream As MJPEGStream = New MJPEGStream("<your MJPEG URL>")
Dim VFWriter = New AVIWriter(("your compression codec 4CC ex:xvid>"))
VFWriter.FrameRate = <framerate>

AddHandler VideoStream.NewFrame, AddressOf NewStreamFrame '<Your Handler>

'…

Public Sub StartRecording()
VFWriter.Open("<destinationFile.avi>", <FrameSize.Width>, <FrameSize.Height>)
‘FrameSize.Width and height must correspond to what your camera is sending
       VideoStream.Start()
End Sub

Public Sub StopRecording()
        VFWriter.Close()
End Sub


Private Sub NewStreamFrame(sender As System.Object, e As NewFrameEventArgs)
        VFWriter.AddFrame(e.Frame)
End Sub

The code is not complete I removed error handling and other decorations. This code will read and save video without displaying it If you already have a VideoSourcePlayer, the player can also fire a NewFrame event with the image frame as argument. In this case you will not need to manually add handler.