I am trying to execute a function that records a portion of a live stream to disk.
This works when the live stream is replaced with a video file that is stored on disk.
After swapping the video file for an IP camera URL I am able to view the live stream, however when I attempt to record the code breaks after writing 79bytes. The error is an AccessViolationException
Here is the full stack trace
System.Exception
HResult=0x80131500
Message=Capture error
Source=Emgu.CV.World
StackTrace:
at Emgu.CV.VideoCapture.Run(ExceptionHandler eh)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Inner Exception 1:
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I have tried to use a command found in this post
netsh winsock reset
but only to achieve the same error.
Can anyone help me?
Here is the methods I am using:
private void ProcessFrame(object sender, EventArgs e)
{
int fourcc = Convert.ToInt32(_cam1.GetCaptureProperty(CapProp.FourCC));
string destpath = @"somepath.mp4";
int Width = Convert.ToInt32(_cam1.GetCaptureProperty(CapProp.FrameWidth));
int Height = Convert.ToInt32(_cam1.GetCaptureProperty(CapProp.FrameHeight));
Mat m = new Mat();
VideoWriter writer = new VideoWriter(destpath, -1, FPS, new Size(Width, Height), true);
while (FrameNo < 870)
{
_cam1.Read(m);
writer.Write(m);
cam1Stream.Image = m;
FrameNo++;
}
if (writer.IsOpened)
{
writer.Dispose();
_cam1.Stop();
}
}
private void RecordButton_Click(object sender, EventArgs e)
{
{
_cam1 = new VideoCapture("udp://someURL", VideoCapture.API.Ffmpeg);
FPS = _cam1.GetCaptureProperty(CapProp.Fps);
_cam1.ImageGrabbed += ProcessFrame;
_cam1.Start();
}
}
Well looking into your code, I can see two errors.
First, Whenever a image is grabbed from the capture (_cam1
) ProcessFrame
gets called where it (re-)creates the (Video)writer on every call. Sure you want to do that?
Secondly, have you checked if the FPS property is available on a camera? I assume that it's not.
My advice would be, to see first how it runs a read-only loop on the camera without attempting to write.
Once this part runs smooth, you can take it a step forward and try the writing.
User contributions licensed under CC BY-SA 3.0