Coder Social home page Coder Social logo

openh264lib.net's Introduction

OpenH264Lib.NET

Cisco's openh264 wrapper library for .NET Framework.
Written in C++/CLI to bridge other .NET Framework language like C#. ย 

How to use

// create encoder and decoder
var encoder = new OpenH264Lib.Encoder("openh264-2.1.1-win32.dll");
var decoder = new OpenH264Lib.Decoder("openh264-2.1.1-win32.dll");

// setup encoder
float fps = 10.0f;
int bps = 5000 * 1000;         // target bitrate. 5Mbps.
float keyFrameInterval = 2.0f; // insert key frame interval. unit is second.
encoder.Setup(640, 480, bps, fps, keyFrameInterval, (data, length, frameType) =>
{
    // called when each frame encoded.
    Console.WriteLine("Encord {0} bytes, frameType:{1}", length, frameType);
    
    // decode it to Bitmap again...
    var bmp = decoder.Decode(data, length);
    if (bmp != null) Console.WriteLine(bmp.Size);
});

// encode frame
foreach(var bmp in bitmaps)
{
    encoder.Encode(bmp);
}

See Example

  1. Open 'OpenH264Lib.sln' Visual Studio solution file.
  2. Build OpenH264Lib project from VisualStudio 'Build OpenH264Lib' menu. (Not 'Build Solution'), or in Solution Explorer, right click 'OpenH264Lib' project and build. Then created OpenH264Lib.dll.
  3. Build OpenH264Sample project from VisualStudio 'Build Solution' menu, or in Solution Explorer, right click 'OpenH264Sample' project and build. This is example C# project how to use OpenH264Lib.dll.
  4. Download 'openh264-2.1.1-win32.dll' from Cisco's openh264 Github repository, and copy it to OpenH264Sample/bin/Debug/ directory.
    • If you are going to use 'openh264-x.x.x-win64.dll', build dll and exe as x64 pratform in step (2) and (3).
    • Use Cisco's dll version 2.1.0 and later. (issue #19). If you are going to use dll version 2.0.0 and before, use old repository.
  5. Execute OpenH264Sample.exe. This program encode image files to H264 avi file, and decode avi to image.

Remarks

Since this is only a wrapper library, you have to know about original openh264 implementation when you add/modify features.

And...

You can make H264 video recorder by using OpenH264Lib.NET and UsbCamera and AviWriter(in MotionJPEGWriter).

int index = 0;
var camera = new Github.secile.Video.UsbCamera(index, new Size(640, 480));
camera.Start();

// create H264 encorder.
var path = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\test.avi";
var encoder = new OpenH264Lib.Encoder("openh264-2.1.1-win32.dll");

// write avi for every frame encorded.
var fps = 10.0f;
var writer = new Github.secile.Avi.AviWriter(System.IO.File.OpenWrite(path), "H264", camera.Size.Width, camera.Size.Height, fps);
OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
{
    var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
    writer.AddImage(data, keyFrame);
    Console.WriteLine("Encord {0} bytes, KeyFrame:{1}", length, keyFrame);
};

// setup encorder.
int bps = 100000; // 100kbps
encoder.Setup(camera.Size.Width, camera.Size.Height, bps, fps, 2.0f, onEncode);

// encode image for every captured image.
var timer = new System.Timers.Timer(1000 / fps) { SynchronizingObject = this };
timer.Elapsed += (s, ev) =>
{
    var bmp = camera.GetBitmap();
    pbxImage.Image = bmp;
    encoder.Encode(bmp);
};
timer.Start();

// release resource when close.
this.FormClosing += (s, ev) =>
{
    camera.Release();
    timer.Stop();
    writer.Close();
    encoder.Dispose();
};

openh264lib.net's People

Contributors

digitalprojections avatar secile avatar simonkmr avatar zacuke 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.