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
357 stars 56 forks source link

End of Stream Exception before end of video is reached #72

Closed jrz371 closed 2 years ago

jrz371 commented 3 years ago

Decoding a video with ffmpeg.exe results in 101 images and every frame is there. Here's the command I use.

.\ffmpeg.exe -i ".\Rotate Around.MP4" "out-%03d.jpg"

When decoding with FFMediaToolkit there is only 86 resulting images. It claims to be reaching the end of stream before the video is actually completed. When viewing the images they also seem to stop early without all the images.

using System;
using System.Drawing;
using System.IO;

namespace Decode
{
    internal static class Program
    {
        public static unsafe Bitmap ToBitmap(this FFMediaToolkit.Graphics.ImageData bitmap)
        {
            fixed (byte* p = bitmap.Data)
            {
                return new Bitmap(bitmap.ImageSize.Width, bitmap.ImageSize.Height, bitmap.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(p));
            }
        }

        static void Main(string[] args)
        {
            FFMediaToolkit.FFmpegLoader.FFmpegPath = "./ffmpeg/";

            string path = @"C:\Users\joshu\Documents\fftest\Rotate Around.MP4";

            string directory = Path.GetDirectoryName(path);

            using(FFMediaToolkit.Decoding.MediaFile mediaFile = FFMediaToolkit.Decoding.MediaFile.Open(path))
            {
                int i = 0;
                while(mediaFile.Video.TryGetNextFrame(out FFMediaToolkit.Graphics.ImageData data))
                {
                    Bitmap bmp = data.ToBitmap();
                    bmp.Save(directory + Path.DirectorySeparatorChar + i++.ToString() + ".png");
                }
            }
        }
    }
}

Here's the video file https://user-images.githubusercontent.com/16514346/110321321-30226e80-801a-11eb-9f8a-9f7669451127.MP4

This video was originally encoded with FFMediaToolkit and I'm a big fan of the library. Thanks for making it!

IsaMorphic commented 3 years ago

Hey there! One question, does this media file contain audio data?

jrz371 commented 3 years ago

No, it does not

IsaMorphic commented 3 years ago

Hmm, okay, interesting, I only ask because I thought the issue might be with how the library reads the file when reading both video and audio. To disable this functionality you'd just set the StreamsToLoad property of a MediaOptions instance to MediaMode.Video and pass the instance to the MediaFile constructor.
Not sure this will help, or if this is the exact issue, but its worth a shot.

jrz371 commented 3 years ago

I gave it a shot but no dice. Thanks for the suggestion though!