I got a webcam that records Video and i wanna write it to a file.
While there are many libraries (mostly based on ffmpeg) that allow you to combine frames, i need a library that can be used to continously write frames to file.
I found such a library called SharpAvi but now i am stuck at converting my image to the DIB bitmap format it expects.
Heres my Code so far:
using FlashCap;
using SharpAvi.Codecs;
using SharpAvi.Output;
var path = Console.ReadLine();
var devices = new CaptureDevices();
var descriptor = devices.EnumerateDescriptors().First(x => x.Characteristics.Length > 0);
var characteristics = descriptor.Characteristics.First(x => x.Height == 720);
var writer = new AviWriter(path);
var stream = writer.AddEncodingVideoStream(new UncompressedVideoEncoder(
characteristics.Width,
characteristics.Height));
stream.Height = characteristics.Height;
stream.Width = characteristics.Width;
using var device = await descriptor.OpenAsync(
characteristics,
async bufferScope =>
{
byte[] image = bufferScope.Buffer.ExtractImage();
var ms = new MemoryStream(image);
byte[] buffer = new byte[bitmap.Height * bitmap.Width * (characteristics.FixedBitsPerPixel/8)];
//how can i get the image as a DIB bitmap in my buffer?
await stream.WriteFrameAsync(true, buffer, 0, buffer.Length);
});
await device.StartAsync();
await Task.Delay(5000);
await device.StopAsync();
the example uses a standard System.Drawing Bitmap and then uses this code:
var bits = bitmap.LockBits(new Rectangle(0, 0, screenWidth, screenHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
However the System.Drawing.Common nuget is not cross platform which is a key criteria. I tried using ImageSharp and IronSoftware.Drawing but didn't get it to run. The documentation theres this line.
AVI expects uncompressed data in the format of a standard Windows DIB, that is a bottom-up bitmap of the specified bit-depth. For each frame, put its data in a byte array and call IAviVideoStream.WriteFrame.