Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
While recording video by ffmpeg VideoFileWriter class, i am getting error like 'A video file was not opened yet'

here is my code :
C#
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);
    }
}
Posted
Updated 21-May-14 3:02am
v2
Comments
ZurdoDev 21-May-14 9:27am    
Which line of code and what is the exact error?
Pavan Kumar 21-May-14 9:58am    
while writing the video frame, writer.WriteVideoFrame((Bitmap)pbVideo.Image);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900