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
362 stars 56 forks source link
csharp decoder dotnet encoder ffmpeg frame-extraction h264 library mp4 netcore netstandard video video-encoder

FFMediaToolkit

Build status Nuget License

This library is not recommended for production use.

FFMediaToolkit is a .NET library for creating and reading multimedia files. It uses native FFmpeg libraries by the FFmpeg.Autogen bindings.

Features

Code samples

Setup

Install the FFMediaToolkit package from NuGet.

dotnet add package FFMediaToolkit
PM> Install-Package FFMediaToolkit

FFmpeg libraries are not included in the package. To use FFMediaToolkit, you need the FFmpeg shared build binaries: avcodec, avformat, avutil, swresample, swscale.

You need to set FFmpegLoader.FFmpegPath with a full path to FFmpeg libraries.

If you want to use 64-bit FFmpeg, you have to disable the Build -> Prefer 32-bit option in Visual Studio project properties.

Usage details

FFMediaToolkit uses the ref struct ImageData for bitmap images. The .Data property contains pixels data in a Span<byte>.

If you want to process or save the ImageData, you should convert it to another graphics object, using one of the following methods.

These methods are not included in the program to avoid additional dependencies and provide compatibility with many graphics libraries.

Visual Basic usage

Writing decoded bitmap directly to the WPF WriteableBitmap buffer using the TryReadFrameToPointer method:

Dim file As FileStream = New FileStream("path to the video file", FileMode.Open, FileAccess.Read)
Dim media As MediaFile = MediaFile.Load(file)
Dim bmp As WriteableBimap = New WriteableBitmap(media.Video.Info.FrameSize.Width, media.Video.Info.FrameSize.Height, 96, 96, PixelFormats.Bgr24, Nothing)
bmp.Lock()
Dim decoded As Boolean = media.Video.TryGetFrame(TimeSpan.FromMinutes(1), bmp.BackBuffer, bmp.BackBufferStride)
If decoded Then
    bmp.AddDirtyRect(New Int32Rect(0, 0, media.Video.Info.FrameSize.Width, media.Video.Info.FrameSize.Height))
End If
bmp.Unlock()
imageBox.Source = bmp

Converting ImageData to a byte array:

Dim data() As Byte = media.Video.GetNextFrame().Data.ToArray()

Licensing

This project is licensed under the MIT license.