圆咕噜咕噜 发表于 2024-8-19 18:20:10

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

https://i-blog.csdnimg.cn/blog_migrate/6f6ed654723e15eb1d46a4f97fb32404.jpeg
目录
说明
效果
项目 
代码
下载
说明

利用周杰大佬的开源项目 Sdcb.FFmpeg
项目地址:https://github.com/sdcb/Sdcb.FFmpeg/
效果

https://i-blog.csdnimg.cn/direct/63fe228d3a604e988ecf7ef7b690a995.png
   C#利用ffmpeg和opencv进行视频的解码播放
项目 

https://i-blog.csdnimg.cn/direct/fef8fbdb27824d06ab4dbfd1fe438fdc.png
代码

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<Mat> matQueue = new ConcurrentQueue<Mat>();
        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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.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();
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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);
        }
    }
}
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<Mat> matQueue = new ConcurrentQueue<Mat>();

      /// <summary>
      /// 播放
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      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.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();
      }


      /// <summary>
      /// 停止
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      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);
      }
    }

}
下载

源码下载

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C#利用ffmpeg和opencv进行视频的解码播放