首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C#利用ffmpeg和opencv进行视频的解码播放

  • 25-02-19 03:41
  • 3473
  • 5129
blog.csdn.net

目录

说明

效果

项目 

代码

下载


说明

利用周杰大佬的开源项目 Sdcb.FFmpeg

项目地址:https://github.com/sdcb/Sdcb.FFmpeg/

效果

C#利用ffmpeg和opencv进行视频的解码播放

项目 

代码

using OpenCvSharp;
using Sdcb.FFmpeg.Codecs;
using Sdcb.FFmpeg.Formats;
using Sdcb.FFmpeg.Raw;
using Sdcb.FFmpeg.Toolboxs.Extensions;
using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sdcb.FFmpegDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CancellationTokenSource cts;

        ConcurrentQueue matQueue = new ConcurrentQueue();

        ///


        /// 播放
        ///

        ///
        ///
        private void button1_Click(object sender, EventArgs e)
        {
            cts = new CancellationTokenSource();

            //string rtsp_url = "test_car_person_1080P.mp4";
            string rtsp_url = "rtsp://192.168.3.62/ch1";

            Task.Run(() => DecodeRTSP(rtsp_url, cts.Token));

            Task.Run(() =>
            {
                Mat mat = new Mat();
                while (true)
                {
                    if (cts.IsCancellationRequested) break;
                    //Console.WriteLine("matQueue.Count:" + matQueue.Count);
                    if (matQueue.TryDequeue(out mat))
                    {
                        Cv2.ImShow(rtsp_url, mat);
                        Cv2.WaitKey(1);
                        mat.Dispose();
                    }
                }
                Cv2.DestroyAllWindows();
            });
        }

        void DecodeRTSP(string url, CancellationToken cancellationToken = default)
        {
            FormatContext fc = FormatContext.OpenInputUrl(url);
            fc.LoadStreamInfo();
            MediaStream videoStream = fc.GetVideoStream();

            CodecContext videoDecoder = new CodecContext(Codec.FindDecoderByName("h264"));
            videoDecoder.FillParameters(videoStream.Codecpar);
            videoDecoder.Open();

            foreach (Sdcb.FFmpeg.Utils.Frame frame in fc
                .ReadPackets(videoStream.Index)
                .DecodePackets(videoDecoder)
                .ConvertVideoFrames(() => (videoDecoder.Width, videoDecoder.Height), AVPixelFormat.Bgr24))
            {
                if (cancellationToken.IsCancellationRequested) break;

                try
                {
                    byte[] data = new byte[frame.Linesize[0] * frame.Height];
                    Marshal.Copy(frame.Data._0, data, 0, data.Length);
                    OpenCvSharp.Mat mat = new OpenCvSharp.Mat(frame.Height, frame.Width, MatType.CV_8UC3, data); ;
                    matQueue.Enqueue(mat);
                }
                finally
                {
                    frame.Unref();
                }
            }
            cts.Cancel();
            videoDecoder.Dispose();
            fc.Dispose();
        }


        ///


        /// 停止
        ///

        ///
        ///
        private void button2_Click(object sender, EventArgs e)
        {
            cts.Cancel();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Sdcb.FFmpeg.Utils.FFmpegLogger.LogWriter = (level, msg) => Console.WriteLine(msg);
        }
    }

}

  1. using OpenCvSharp;
  2. using Sdcb.FFmpeg.Codecs;
  3. using Sdcb.FFmpeg.Formats;
  4. using Sdcb.FFmpeg.Raw;
  5. using Sdcb.FFmpeg.Toolboxs.Extensions;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace Sdcb.FFmpegDemo
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. CancellationTokenSource cts;
  21. ConcurrentQueue<Mat> matQueue = new ConcurrentQueue<Mat>();
  22. /// <summary>
  23. /// 播放
  24. /// </summary>
  25. /// <param name="sender"></param>
  26. /// <param name="e"></param>
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. cts = new CancellationTokenSource();
  30. //string rtsp_url = "test_car_person_1080P.mp4";
  31. string rtsp_url = "rtsp://192.168.3.62/ch1";
  32. Task.Run(() => DecodeRTSP(rtsp_url, cts.Token));
  33. Task.Run(() =>
  34. {
  35. Mat mat = new Mat();
  36. while (true)
  37. {
  38. if (cts.IsCancellationRequested) break;
  39. //Console.WriteLine("matQueue.Count:" + matQueue.Count);
  40. if (matQueue.TryDequeue(out mat))
  41. {
  42. Cv2.ImShow(rtsp_url, mat);
  43. Cv2.WaitKey(1);
  44. mat.Dispose();
  45. }
  46. }
  47. Cv2.DestroyAllWindows();
  48. });
  49. }
  50. void DecodeRTSP(string url, CancellationToken cancellationToken = default)
  51. {
  52. FormatContext fc = FormatContext.OpenInputUrl(url);
  53. fc.LoadStreamInfo();
  54. MediaStream videoStream = fc.GetVideoStream();
  55. CodecContext videoDecoder = new CodecContext(Codec.FindDecoderByName("h264"));
  56. videoDecoder.FillParameters(videoStream.Codecpar);
  57. videoDecoder.Open();
  58. foreach (Sdcb.FFmpeg.Utils.Frame frame in fc
  59. .ReadPackets(videoStream.Index)
  60. .DecodePackets(videoDecoder)
  61. .ConvertVideoFrames(() => (videoDecoder.Width, videoDecoder.Height), AVPixelFormat.Bgr24))
  62. {
  63. if (cancellationToken.IsCancellationRequested) break;
  64. try
  65. {
  66. byte[] data = new byte[frame.Linesize[0] * frame.Height];
  67. Marshal.Copy(frame.Data._0, data, 0, data.Length);
  68. OpenCvSharp.Mat mat = new OpenCvSharp.Mat(frame.Height, frame.Width, MatType.CV_8UC3, data); ;
  69. matQueue.Enqueue(mat);
  70. }
  71. finally
  72. {
  73. frame.Unref();
  74. }
  75. }
  76. cts.Cancel();
  77. videoDecoder.Dispose();
  78. fc.Dispose();
  79. }
  80. /// <summary>
  81. /// 停止
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void button2_Click(object sender, EventArgs e)
  86. {
  87. cts.Cancel();
  88. }
  89. private void Form1_Load(object sender, EventArgs e)
  90. {
  91. Sdcb.FFmpeg.Utils.FFmpegLogger.LogWriter = (level, msg) => Console.WriteLine(msg);
  92. }
  93. }
  94. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/141176560"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top