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

Exception thrown at (swscale-5.dll) Access violation reading location #87

Closed NormantasST closed 11 months ago

NormantasST commented 3 years ago

The full error is "Exception thrown at 0x00007FFB719230C2 (swscale-5.dll) in GrayScaleFilter.exe: 0xC0000005: Access violation reading location 0x000001B5E690B000."

I'm using one function to export images into a folder. Another to build a video from them.

my code:

private static void CombineImagesUsingFFMTK(string fileLocation, int frameCount)
        {
            File.Delete(outputPath);
            System.IO.DirectoryInfo directory = new DirectoryInfo(outputImageDir);
            FFMediaToolkit.Decoding.MediaFile mediaFile = FFMediaToolkit.Decoding.MediaFile.Open(fileLocation);
            // You can set there codec, bitrate, frame rate and many other options.
            var settings = new VideoEncoderSettings(width: mediaFile.Video.Info.FrameSize.Width, height: mediaFile.Video.Info.FrameSize.Height, framerate: 30, codec: VideoCodec.H264);
            settings.EncoderPreset = EncoderPreset.Fast;
            settings.CRF = 17;
            using (var video = MediaBuilder.CreateContainer(outputPath).WithVideo(settings).Create())
            {
                for (int i = 0; i < frameCount; i++)
                {
                    Bitmap bitmap = new Bitmap($"{outputImageDir}\\{i}.jpeg");
                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size);
                    BitmapData bitLock = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                    FFMediaToolkit.Graphics.ImageData bitmapImageData = FFMediaToolkit.Graphics.ImageData.FromPointer(bitLock.Scan0, ImagePixelFormat.Bgr24, bitmap.Size);
                    Console.WriteLine($"Adding: image {outputImageDir}\\{i}.jpeg to video");
                    video.Video.AddFrame(bitmapImageData);
                    bitmap.UnlockBits(bitLock);
                }
            }
        }

The error happens on line video.Video.AddFrame(bitmapImageData);

I looked into other issues, with similar problems (write protected, access violation) and modified from the example code into this. Not sure what to do.

Tried using different libraries to decode the video but this one feels like the most Intuitive. I had a version (Using Splicer) where I get the video, but I can't use the timeline to skip video and it runs on 1fps, 73frames (2,3sec video) becomes 73sec video.

sunilhalvie commented 1 year ago

Hi Normantas, I am facing the same issue, can you please guide me to solve this if you had figured it out?