CSharp中处理H264的编码和解码

2023-07-11 14:07:17 浏览数 (1)

前言

开源的H264库

https://github.com/cisco/openh264

C#的封装

https://github.com/secile/OpenH264Lib.NET

示例代码:

https://gitee.com/psvmc/z-h264-tools

添加依赖

下面的这个命令并没有下载C#的封装库,只下载了C 的DLL(openh264-2.1.1-win32.dll/openh264-2.1.1-win64.dll)

代码语言:javascript复制
Install-Package OpenH264.NET -Version 1.0.4

我没有使用这种方式,而是自己下载了最新版放在项目下openh264-2.3.1-win32.dll

生成事件=>生成前事件命令行:

代码语言:javascript复制
xcopy /Y /d $(ProjectDir)Libsopenh264-2.3.1-win32.dll $(TargetDir)

至于C#的封装我下载源码后,把目标设置为4.5.2,生成DLL后引用即可。

调用

代码语言:javascript复制
namespace z_h264_tools
{
    using System;
    using System.Threading;
    using Utils;

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow
    {
        private bool _isStart;

        public MainWindow()
        {
            InitializeComponent();
            Closing  = MainWindow_Closing;
            ScreenTest();
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _isStart = false;
        }

        private void ScreenTest()
        {
            _isStart = true;
            var encoder = new OpenH264Lib.Encoder("openh264-2.3.1-win32.dll");
            var decoder = new OpenH264Lib.Decoder("openh264-2.3.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
            (
                1920,
                1080,
                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...
                    if (_isStart)
                    {
                        Dispatcher.Invoke
                        (
                            () =>
                            {
                                var bmp = decoder.Decode(data, length);
                                if (bmp != null)
                                {
                                    Console.WriteLine(bmp.Size);
                                    var bitmapImage = ZImageUtils.Bitmap2BitmapImage(bmp);
                                    MImg.Source = bitmapImage;
                                    bmp.Dispose();
                                }
                            }
                        );
                    }
                }
            );
            new Thread
            (
                () =>
                {
                    while (_isStart)
                    {
                        var screen = ZScreenUtils.GetScreen();
                        encoder.Encode(screen);
                        screen.Dispose();
                        Thread.Sleep(1000 / 10);
                    }
                }
            ).Start();
        }
    }
}

0 人点赞