Code Monkey home page Code Monkey logo

zylab.emguffmpeg's Introduction

EmguFFmpeg

NuGet version (EmguFFmpeg) NuGet downloads (EmguFFmpeg) Build status

A FFmpeg.AutoGen warpper library, make ffmpeg api easier to use.

NOTE not recommended in production environments,

  • api is unstable.

Features

  1. compatible with Bitmap, EmguCV and OpenCvSharp4.
  2. easy decoding and encoding of data through IEnumerable interface.
  3. video/audio frame format converter.
  4. filter supported.

Example

Decode

// create media reader by file
using(MediaReader reader = new MediaReader("input.mp4"))
{
    // get packet from reader (demultiplexing to packet)
    foreach(var packet in reader.ReadPacket())
    {
        // get frame from different stream (decode to frame).
        foreach (var frame in reader[packet.StreamIndex].ReadFrame(packet))
        {
            IntPtr[] data = frame.Data; // FFmpeg AVFrame.data
	    byte[][] managed = frame.GetData(); // copy AVFrame.data to managed data
	    if(frame is VideoFrame)
	        Bitmap bitmap = frame.ToBitmap(); // add EmguFFmpeg.Bitmap, only for video frame
	    Mat mat = frame.ToMat(); // add EmguFFmpeg.OpenCvSharp or EmguFFmpeg.EmguCV (video and audio)
        }
    }
}

Encode

/* create a 60s duration video */

int height = 600;
int width = 800;
int fps = 30;
int duration = 60;

// create media writer
using(MediaWriter writer = new MediaWriter("output.mp4"))
{
    // create media encoder
    MediaEncode videoEncode = MediaEncode.CreateVideoEncode(writer.Format.VideoCodec, writer.Format.Flags, width, height, fps);
    // add stream by encoder
    writer.AddStream(videoEncode);
    // init writer
    writer.Initialize();
    // create video frame, default video frame is green image 
    VideoFrame videoFrame = new VideoFrame(AVPixelFormat.AV_PIX_FMT_YUV420P, width, height);

    // write frame by timespan see EmguFFmpeg.Example/Mp4VideoWriter.cs#L95
    // write 60s duration video
    long lastpts = -1;
    Stopwatch timer = Stopwatch.StartNew();
    for(stopwatch.Elapsed <= TimeSpan.FromSeconds(duration)) 
    {
        long curpts = (long)(timeSpan.TotalSeconds * fps);
        if(curpts > lastpts)
        {
            lastpts = curpts;
            
            /* fill video frame data, see more code in example*/
            
            videoFrame.PTS = curpts;
            // write frame, encode to packet and write to writer
            foreach (var packet in writer.First().WriteFrame(videoFrame))
            {
                writer.WritePacket(packet);
            }
        }
    }

    // flush encode cache
    writer.FlushMuxer();
}

Index

  1. EmguFFmpeg
    FFmpeg.AutoGen warpper It's dependent on FFmpeg.AutoGen.
    for windows:
    • nuget install EmguFFmpeg
    • download the dll of ffmpeg with different licenses on your own.
  2. EmguFFmpeg.EmguCV
    Some extension methods for data exchange between EmguFFmpeg and EmguCV.
    It's dependent on EmguFFmpeg and EmguCV.
    for windows:
    • nuget install EmguFFmpeg.EmguCV
    • nuget install Emgu.CV.runtime.windows
    • download the dll of ffmpeg with different licenses on your own.
  3. EmguFFmpeg.OpenCvSharp
    Some extension methods for data exchange between EmguFFmpeg and OpenCvSharp.
    please read here before use.
    It's dependent on EmguFFmpeg and OpenCvSharp.
    for windows:
    • nuget install EmguFFmpeg.OpenCvSharp
    • nuget install OpenCvSharp4.runtime.win
    • download the dll of ffmpeg with different licenses on your own.
  4. EmguFFmpeg.Bitmap
    Some extension methods for Bitmap. only supported video frame.
  5. EmguFFmpeg.Example
    EmguFFmpeg example.

TODO

  • Convert MediaFrame data easy with EmguCV etc.
  • Add MedaiFilter support.
  • Add Subtitle support.
  • Data exchange with NAudio and SharpAVI.

zylab.emguffmpeg's People

Contributors

iol0ol1 avatar zy-ph avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.