前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSharp中处理H264的编码和解码

CSharp中处理H264的编码和解码

作者头像
码客说
发布2023-07-11 14:07:17
6500
发布2023-07-11 14:07:17
举报
文章被收录于专栏:码客码客

前言

开源的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)\Libs\openh264-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();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 添加依赖
  • 调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档