All Projects → radek-k → Ffmediatoolkit

radek-k / Ffmediatoolkit

Licence: mit
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.

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Ffmediatoolkit

Ffmpegcommand
FFmpegCommand适用于Android的FFmpeg命令库,实现了对音视频相关的处理,能够快速的处理音视频,大概功能包括:音视频剪切,音视频转码,音视频解码原始数据,音视频编码,视频转图片或gif,视频添加水印,多画面拼接,音频混音,视频亮度和对比度,音频淡入和淡出效果等
Stars: ✭ 394 (+152.56%)
Mutual labels:  ffmpeg, decoder, encoder, mp4
Swiftffmpeg
A Swift wrapper for the FFmpeg API
Stars: ✭ 243 (+55.77%)
Mutual labels:  ffmpeg, mp4, h264
Opentouryo
”Open棟梁”は、長年の.NETアプリケーション開発実績にて蓄積したノウハウに基づき開発した.NET用アプリケーション フレームワークです。 (”OpenTouryo” , is an application framework for .NET which was developed using the accumulated know-how with a long track record in .NET application development.)
Stars: ✭ 233 (+49.36%)
Mutual labels:  library, netstandard, netcore
camstudio
CamStudio fork
Stars: ✭ 83 (-46.79%)
Mutual labels:  h264, ffmpeg, mp4
Androidffmpeg
android 读取摄像头和麦克风,使用rtmp推流
Stars: ✭ 298 (+91.03%)
Mutual labels:  ffmpeg, encoder, h264
Ffmediaelement
FFME: The Advanced WPF MediaElement (based on FFmpeg)
Stars: ✭ 733 (+369.87%)
Mutual labels:  ffmpeg, mp4, h264
live-stream-media-source-extensions
Live stream h264 encoded mp4 video on media source extensions using ffmpeg, node.js, socket.io, and express. Works in chrome, firefox, safari, and android. Not iOS compatible. Work has moved to mse-live-player repo =>
Stars: ✭ 24 (-84.62%)
Mutual labels:  h264, ffmpeg, mp4
Jave2
The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project
Stars: ✭ 570 (+265.38%)
Mutual labels:  ffmpeg, decoder, encoder
Axiom
An FFmpeg GUI for Windows
Stars: ✭ 560 (+258.97%)
Mutual labels:  ffmpeg, encoder, mp4
Trinity
android video record editor muxer sdk
Stars: ✭ 609 (+290.38%)
Mutual labels:  ffmpeg, mp4, h264
Colore
A powerful C# library for Razer Chroma's SDK
Stars: ✭ 121 (-22.44%)
Mutual labels:  library, netstandard, netcore
Oss.core
.net core下的领域开源电商网站,类库使用的标准库项目,集成微信和支付宝。 暂时还在开发阶段
Stars: ✭ 128 (-17.95%)
Mutual labels:  netstandard, netcore
Swift Html Entities
HTML5 spec-compliant character encoder/decoder for Swift
Stars: ✭ 130 (-16.67%)
Mutual labels:  decoder, encoder
Silk V3 Decoder
kn007's blog
Stars: ✭ 1,832 (+1074.36%)
Mutual labels:  decoder, encoder
Azos
A to Z Sky Operating System / Microservice Chassis Framework
Stars: ✭ 137 (-12.18%)
Mutual labels:  netstandard, netcore
Docnet
DocNET is as fast PDF editing and reading library for modern .NET applications
Stars: ✭ 128 (-17.95%)
Mutual labels:  netstandard, netcore
Irremoteesp8266
Infrared remote library for ESP8266/ESP32: send and receive infrared signals with multiple protocols. Based on: https://github.com/shirriff/Arduino-IRremote/
Stars: ✭ 1,964 (+1158.97%)
Mutual labels:  decoder, encoder
Wav
Battle tested Wav decoder/encoder
Stars: ✭ 139 (-10.9%)
Mutual labels:  decoder, encoder
Cityengine Sdk
CityEngine is a 3D city modeling software for urban design, visual effects, and VR/AR production. With its C++ SDK you can create plugins and standalone apps capable to execute CityEngine CGA procedural modeling rules.
Stars: ✭ 137 (-12.18%)
Mutual labels:  decoder, encoder
Activelogin.authentication
Support Swedish BankID (svenskt BankID) authentication in .NET.
Stars: ✭ 141 (-9.62%)
Mutual labels:  netstandard, netcore

FFMediaToolkit

Build status Nuget License

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

Features

  • Decoding/encoding audio-video files in many formats supported by FFmpeg.
  • Extracting audio data as floating point arrays.
  • Access to any video frame by timestamp.
  • Creating videos from images with metadata, pixel format, bitrate, CRF, FPS, GoP, dimensions and other codec settings.
  • Supports reading multimedia chapters and metadata.
  • Cross-platform - works on Linux, Windows, and macOS - with .NET Core or .NET Framework projects.

Code samples

  • Extract all video frames as PNG files

    int i = 0;
    var file = MediaFile.Open(@"C:\videos\movie.mp4");
    while(file.Video.TryGetNextFrame(out var imageData))
    {
        imageData.ToBitmap().Save([email protected]"C:\images\frame_{i++}.png");
        // See the #Usage details for example .ToBitmap() implementation
        // The .Save() method may be different depending on your graphics library
    }
    
  • Video decoding

    // Opens a multimedia file.
    // You can use the MediaOptions properties to set decoder options.
    var file = MediaFile.Open(@"C:\videos\movie.mp4");
    
     // Gets the frame at 5th second of the video.
    var frame5s = file.Video.GetFrame(TimeSpan.FromSeconds(5));
    
    // Print informations about the video stream.
    Console.WriteLine($"Bitrate: {file.Info.Bitrate / 1000.0} kb/s");
    var info = file.Video.Info;
    Console.WriteLine($"Duration: {info.Duration}");
    Console.WriteLine($"Frames count: {info.NumberOfFrames ?? "N/A"}");
    var frameRateInfo = info.IsVariableFrameRate ? "average" : "constant";
    Console.WriteLine($"Frame rate: {info.AvgFrameRate} fps ({frameRateInfo})");
    Console.WriteLine($"Frame size: {info.FrameSize}");
    Console.WriteLine($"Pixel format: {info.PixelFormat}");
    Console.WriteLine($"Codec: {info.CodecName}");
    Console.WriteLine($"Is interlaced: {info.IsInterlaced}");
    
  • Encode video from images.

    // You can set there codec, bitrate, frame rate and many other options.
    var settings = new VideoEncoderSettings(width: 1920, height: 1080, framerate: 30, codec: VideoCodec.H264);
    settings.EncoderPreset = EncoderPreset.Fast;
    settings.CRF = 17;
    using(var file = MediaBuilder.CreateContainer(@"C:\videos\example.mp4").WithVideo(settings).Create())
    {
        while(file.Video.FramesCount < 300)
        {
            file.Video.AddFrame(/*Your code*/);
        }
    }
    

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.

  • Windows - You can download it from the BtbN/FFmpeg-Builds or gyan.dev. You only need *.dll files from the .\bin directory (not .\lib) of the ZIP package. Place the binaries in the .\ffmpeg\x86_64\(64bit) in the application output directory or set FFmpegLoader.FFmpegPath.
  • Linux - Download FFmpeg using your package manager.
  • macOS - Install FFmpeg via Homebrew.

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.

  • For ImageSharp library (.NET Standard/Core - cross-platform):

    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.PixelFormats;
    ...
    public static Image<Bgr24> ToBitmap(this ImageData imageData)
    {
        return Image.LoadPixelData<Bgr24>(imageData.Data, imageData.ImageSize.Width, imageData.ImageSize.Height);
    }
    
  • For .NET Framework System.Drawing.Bitmap (Windows only):

    // ImageData -> Bitmap (unsafe)
    public static unsafe Bitmap ToBitmap(this ImageData bitmap)
    {
        fixed(byte* p = bitmap.Data)
        {
            return new Bitmap(bitmap.ImageSize.Width, bitmap.ImageSize.Height, bitmap.Stride, PixelFormat.Format24bppRgb, new IntPtr(p));
        }
    }
    
    // Bitmap -> ImageData (safe)
    public static ImageData ToImageData(this Bitmap bitmap)
    {
        var rect = new Rectangle(Point.Empty, bitmap.Size);
        var bitLock = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    
        var bitmapData = ImageData.FromPointer(bitLock.Scan0, bitmap.Size, ImagePixelFormat.Bgr24);
        bitmap.UnlockBits(bitLock);
        return bitmapData;
    }
    
  • For .NET Framework/Core desktop apps with WPF UI. (Windows only):

    using System.Windows.Media.Imaging;
    ...
    // ImageData -> BitmapSource (unsafe)
    public static unsafe BitmapSource ToBitmap(this ImageData bitmapData)
    {
        fixed(byte* ptr = bitmapData.Data)
        {
            return BitmapSource.Create(bitmapData.ImageSize.Width, bitmapData.ImageSize.Height, 96, 96, PixelFormats.Bgr32, null, new IntPtr(ptr), bitmapData.Data.Length, bitmapData.Stride);
        }
    }
    
    // BitmapSource -> ImageData (safe)
    public static ImageData ToImageData(this BitmapSource bitmap)
    {
        var wb = new WriteableBitmap(bitmap);
        return ImageData.FromPointer(wb.BackBuffer, ImagePixelFormat.Bgra32, wb.PixelWidth, wb.PixelHeight);
    }
    
  • FFMediaToolkit will also work with any other graphics library that supports creating images from Span<byte>, byte array or memory pointer

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].