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

There is no .ToBitmap() Method for ReadFrame(framecount) #20

Closed cemalgulbeyaz closed 3 years ago

cemalgulbeyaz commented 4 years ago

var file = MediaFile.Open(@"C:\videos\movie.mp4"); for (int i = 0; i < file.Video.Info.FrameCount; i++) { file.Video.ReadFrame(i).ToBitmap().Save($@"C:\images\frame_{i}.png"); }

I wrote a code from your Seample code but there is no ToBitmap Method. I using Visual Studio 16.4.1 and Project .Net Core 3.1 Windows Worm. Can you help me? Screen Capture of Code; Adsız

radek-k commented 4 years ago

Sorry for the inaccurate information in the readme. I`ve just updated it. Please read the usage details.

In a .NET Core project you should install the ImageSharp library from Nuget and add the following class to your main namespace (in a new file)

public static class Extensions
{
   public static Image<Bgr24> ToBitmap(this ImageData imageData)
   {
      return Image.LoadPixelData<Bgr24>(imageData.Data, imageData.ImageSize.Width, imageData.ImageSize.Height);
   }
}
radek-k commented 4 years ago

The complete .NET Core sample app with ImageSharp should look like this

using System;
using System.IO;
using FFMediaToolkit.Graphics;
using FFMediaToolkit.Decoding;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace YourProjectNamespace
{
    public class Program
    {
        static void Main(string[] args)
        {
            var file = MediaFile.Open(@"C:\videos\movie.mp4");
            int i = 0;
            while(file.Video.TryReadNextFrame(out var imageData))
            {
                using(var fs = new FileStream($@"C:\images\frame_{i++}.png", FileMode.Create))
                {
                    imageData.ToBitmap().SaveAsPng(fs);
                }
            }
        }
    }

    public static class Extensions
    {
        public static Image<Bgr24> ToBitmap(this ImageData imageData)
        {
            return Image.LoadPixelData<Bgr24>(imageData.Data, imageData.ImageSize.Width, imageData.ImageSize.Height);
        }
    }
}
dev-masih commented 4 years ago

@radek-k you sample code for me results in Access violation crash when reaches line ReadFrame. do you have any idea how to resolve that?

alexchri commented 4 years ago

@radek-k you sample code for me results in Access violation crash when reaches line ReadFrame. do you have any idea how to resolve that?

I am hitting the same issue - any ideas here?

radek-k commented 4 years ago

@radek-k you sample code for me results in Access violation crash when reaches line ReadFrame. do you have any idea how to resolve that?

I am hitting the same issue - any ideas here?

@alexchri Please update FFMediaToolkit package to v3.1.0. I've just fixed this bug.

MastalerzKamil commented 3 years ago

The complete .NET Core sample app with ImageSharp should look like this

using System;
using System.IO;
using FFMediaToolkit.Graphics;
using FFMediaToolkit.Decoding;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

namespace YourProjectNamespace
{
    public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var file = MediaFile.Open(@"C:\videos\movie.mp4");
                for (int i = 0; i < file.Video.Info.FrameCount; i++)
                {
                    using(var fs = new FileStream($@"C:\images\frame_{i}.png", FileMode.Create))
                    {
                        file.Video.ReadFrame(i).ToBitmap().SaveAsPng(fs);
                    }
                }
            }
            catch(EndOfStreamException) { }
        }
    }

    public static class Extensions
    {
        public static Image<Bgr24> ToBitmap(this ImageData imageData)
        {
            return Image.LoadPixelData<Bgr24>(imageData.Data, imageData.ImageSize.Width, imageData.ImageSize.Height);
        }
    }
}

@radek-k It works for me but it generates System.IO.EndOfStreamException as I described on issue #43

Ulakurak commented 3 years ago

Kindly request you to update the Setup section. First of all the Zeranoe FFmpeg site link is not working. I have used the https://www.gyan.dev/ffmpeg/builds/ link to get required binaries. On MediaFile.Open(@"C:\videos\movie.mp4") there is error message: System.IO.DirectoryNotFoundException: 'Cannot found the default FFmpeg directory. On Windows you have to specify a path to a directory containing the FFmpeg shared build DLL files

hey-red commented 3 years ago

@Ulakurak

On Windows you have to specify a path to a directory containing the FFmpeg shared build DLL files

Another way is nuget package with FFmpeg dll's. For example https://www.nuget.org/packages/ImageSharp.AVCodecFormats.Native/ It should work out of box with FFMediaToolkit.