radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
352 stars 56 forks source link

Video.AddFrame(FrameToImageData(ImageData)) causes program to exit #125

Closed wjadevries closed 11 months ago

wjadevries commented 1 year ago

I have a C# Windows .net6 application where I want to capture video to a file. I initialize a file like this:

var file = MediaBuilder.CreateContainer(outputFilePath).WithVideo(settings).Create();

In the Capture_NewFrame eventhandler (Aforge.Video.DirectShow) I save the incoming frames like this:

                System.Drawing.Bitmap bitmap = eventArgs.Frame;
                var rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size);
                var bitLock = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                var bitmapData = ImageData.FromPointer(bitLock.Scan0, ImagePixelFormat.Bgr24, bitmap.Size);
                file.Video.AddFrame(bitmapData);
                bitmap.UnlockBits(bitLock);

I have two cams: a Fantech Luminous C30 USB cam and an Integrated webcam in my Dell laptop. The Fantech works fine; no issues. But when I use the integrated webcam, the application quits unexpectedly when it reaches the call file.Video.AddFrame(bitmapData); The code is in a try/catch block, but that isn't triggered. The application just quits.

The bitmapData for the integrated cam is Bgr24, 1280 x 720, Stride 3840, IsPooled = false, 2764800 bytes, For the working Fantech cam the bitmapData is Bgr24, 1920 x 1080, Stride 5760, IsPooled = false, 6220800 bytes.

During the same Capture_NewFrame eventhandler I also show the incoming bitmap on an Image, which works fine for both cams.

In the event viewer I see this:

Faulting application name: TestCarMaster.exe, version: 0.7.0.0, time stamp: 0x6331eb0e Faulting module name: swscale-6.dll, version: 6.8.112.0, time stamp: 0x6368faa4 Exception code: 0xc0000005 Fault offset: 0x000000000004b3d6 Faulting process ID: 0x1d87c Faulting application start time: 0x01d8f2bad24f37e1 Faulting application path: C:\Repos\Wessel\Projects\TestCar\TestCarMaster\bin\Debug\net6.0-windows\TestCarMaster.exe Faulting module path: C:\Repos\Wessel\Projects\TestCar\TestCarMaster\bin\Debug\net6.0-windows\ffmpeg\swscale-6.dll Report ID: d8c8ee0c-38e1-47ab-ac72-9e4d23d950fd Faulting package full name: Faulting package-relative application ID: