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

FatalExecutionEngineError #86

Closed AppeazeTheCheese closed 2 years ago

AppeazeTheCheese commented 3 years ago

I'm aware this error isn't very helpful but here it is anyway:

Managed Debugging Assistant 'FatalExecutionEngineError' 
  Message=Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x1fc555ce, on thread 0x42f4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

So, I'm writing a discord bot to be able to detect gifs and videos that can crash discord clients. Well, turns out it crashes the library too in a non-catchable way. Basically this only happens with videos of 2 different resolutions that have been stitched together in one way or another. Here is the problem method:

public static bool ScanVideo(string filePath) // Return true if video is safe, false if not
{
    var first = true;
    ImageData firstFrame = new ImageData();

    var file = MediaFile.Open(filePath);
    bool foundNextFrame;

    while (true)
    {
        ImageData frame;
        try
        {
            // EXCEPTION HERE
            foundNextFrame = file.Video.TryGetNextFrame(out frame);
        }
        catch
        {
            return false;
            // Considering FFmpeg had an error, Discord will more than likely crash
        }
        if (!foundNextFrame)
            break;
        if(first)
        {
            firstFrame = frame;
            first = false;
        }

        if (frame.ImageSize.Width != firstFrame.ImageSize.Width || frame.ImageSize.Height != firstFrame.ImageSize.Height)
        {
            // Discord will crash
            return false;
        }
    }

    return true;
}

Any help would be much appreciated! In case anyone would like to try this out for themselves, this is the video I've been testing with.