Twometer / CSVideo

C# Library for writing video files using FFmpeg with audio support
Apache License 2.0
10 stars 7 forks source link

Only first frame is exported #2

Open tigrouind opened 1 month ago

tigrouind commented 1 month ago

Code to reproduce the issue

if (FFmpegLoader.Load(@"ffmpeg"))
{
    using (var writer = new VideoWriter(@"somefile.mp4"))
    using (var bitmap = new Bitmap(1920, 1080))
    using (var graphics = Graphics.FromImage(bitmap))
    {
        writer.Open();
        var audio = new float[writer.AudioSamplesPerFrame];

        Random rand = new Random();
        for (int i = 0; i < 1000; i++)
        {
            if (writer.WriteVideo)
            {
                var color = rand.Next(2) == 0 ? Brushes.Yellow : Brushes.Red;
                graphics.FillRectangle(color, 0, 0, 1920, 1080);
                writer.WriteVideoFrame(bitmap);
            }
            else
            {
                writer.WriteAudioFrame(audio);
            }
        }
    }
}

Expected result

The movie alternate between yellow and red frames.

Actual result

Only first frame is shown. Time length of movie seems correct (there is as many frames as expected).

Additionally

Here is a list of things that are not obvious and took me lot of time to figure out :

Twometer commented 1 month ago

Thanks for the report, can you try

  1. using i % 2 == 0 instead of rand.Next() for the alternating images?
  2. creating a new Bitmap for each frame instead of reusing it?

That'll help narrow it down, but I'll look into the other issues as well.

tigrouind commented 1 month ago

@Twometer :

  1. Fun fact is my initial code example was using i % 2 == 0 but I edited it, fearing that WriteVideoFrame() would always end up on a odd or even value (thus using same color) because calls are interleaved with WriteAudioFrame().
  2. I also tried that before but that does not help as well.