While recording video by ffmpeg VideoFileWriter class, i am getting error like 'A video file was not opened yet'
here is my code :
private void butStart_Click(object sender, EventArgs e)
{
try
{
FinalVideo = new VideoCaptureDevice(monikerString);
FinalVideo.VideoResolution = FinalVideo.VideoCapabilities[videoCapabilityNo];
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Start Video", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
pbVideo.Image = (Bitmap)eventArgs.Frame.Clone();
writer.WriteVideoFrame((Bitmap)pbVideo.Image);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "New Frame", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void butRecord_Click(object sender, EventArgs e)
{
try
{
path = Path.GetDirectoryName(Application.ExecutablePath) + DateTime.Now.ToString("dd-MM-yyyy")+"\\";
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
writer.Open(path + "VID" + DateTime.Now.ToString("HHmmss") + videoFormat, width, height, frameRate, VideoCodec.MPEG4, bitRate);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void butStop_Click(object sender, EventArgs e)
{
try
{
if (FinalVideo == null)
return;
if (FinalVideo.IsRunning)
writer.Close();
else
{
this.FinalVideo.Stop();
FinalVideo = null;
pbVideo.Image = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Stop Video", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}