Skip to content

Continuous Snapshots on Live Stream Memory stream contains invalid bitmap image #562

Open
@KiwiBryn

Description

@KiwiBryn

Every so often the System.Drawing.Bitmap constructor throws an exception.(I also tried with SKIASharp same but different result)

The code will run for minutes, then a couple of exceptions, then continue. Sometimes the memory stream length is 0 other times its way too large.

Saving images from an RTSP stream 4FPS using OutputToFile works fine so I don't think it is my camera etc.

  await FFMpegArguments.FromUrlInput(new Uri(rtspUrl))
  .OutputToFile("frames/Frame%05d.png", true, 
     Options => 

What could cause the MemoryStream content to be broken?

My repro is based on a modified version of #280 with ms.SetLength(0) after processing the bitmap.

Thanks

Bryn

 using (var ms = new MemoryStream())
 {
    await FFMpegArguments
        .FromUrlInput(new Uri(rtspUrl))
        .OutputToPipe(new StreamPipeSink(ms), options => options
            .ForceFormat("rawvideo")
            .WithVideoCodec(VideoCodec.Png)
            //.Resize(new Size(Config.JpgWidthLarge, Config.JpgHeightLarge))
            .WithCustomArgument("-vf fps=1 -update 1")
        )
        .NotifyOnProgress(o =>
        {
           try
           {
              if (ms.Length > 0)
              {
                 ms.Position = 0;
                 using (var bitmap = new Bitmap(ms)) // <==== Goes pop here
                 {
                    // Save the bitmap
                    bitmap.Save($"frames\\image{frameCount:0000}.png");

                    frameCount += 1;
                 }

                 ms.SetLength(0);
              }
           }
           catch (Exception ex)
           {
              Console.WriteLine($"ms.Position:{ms.Position}");

              Console.WriteLine(ex);
           }
        })
        .ProcessAsynchronously();
 }

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions