inam101
inam101

Reputation: 92

AForge.NET -> AVIWriter Adding Images as Frames

I want to make a video from images, and each image should stay for one second. The AVIWriter has 25 frames rate, so i have to add one image 25 times to make it stay for one second.

I tried changing the frame-rate, but it is not working.

Can anyone suggest a workaround?

The following is the code for writing frames into video:

    private void writeVideo()
    {
        // instantiate AVI writer, use WMV3 codec
        AVIWriter writer = new AVIWriter("wmv3");
        // create new AVI file and open it
        writer.Open(fileName, 320, 240);
        // create frame image
        Bitmap image = new Bitmap(320, 240);
        var cubit = new AForge.Imaging.Filters.ResizeBilinear(320, 240);
        string[] files = Directory.GetFiles(imagesFolder);
        writer.FrameRate = 25;
        int index = 0;
        int failed = 0;
        foreach (var item in files)
        {
            index++;
            try
            {
                image = Image.FromFile(item) as Bitmap;
                //image = cubit.Apply(image);

                for (int i = 0; i < 25; i++)
                {
                    writer.AddFrame(image); 
                }   
            }
            catch
            {
                failed++;
            }
            this.Text = index + " of " + files.Length + ". Failed: " + failed;
        }
        writer.Close();
    }

Upvotes: 0

Views: 7612

Answers (3)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

The default frame rate for AVIWriter is 25. As you have no option to specify dropped or otherwise skipped frames, why wouldn't you set AVIWriter::FrameRate property to 1 before you start populating your writer with frames?

Upvotes: 0

aegis89
aegis89

Reputation: 11

The solution of Hakan is working, if you use this code:

AVIWriter videoWriter;
videoWriter = new AVIWriter("wmv3");
videoWriter.FrameRate = 1;
videoWriter.Open("test.avi", 320, 240);
videoWriter.AddFrame(bitmap1);
videoWriter.AddFrame(bitmap2);
videoWriter.AddFrame(bitmap3);
videoWriter.Close();

There is well one bitmap displayed per second. (just for giving a directly working piece of code).

Upvotes: 1

Hakan Sağlam
Hakan Sağlam

Reputation: 21

Hello I had the same problem and saw this topic read it all and no comment for http://www.aforgenet.com/framework/docs/html/bc8345f8-8e09-c1a4-4834-8330e5e85605.htm There is a note like that "The property should be set befor opening new file to take effect."

Upvotes: 2

Related Questions