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

打印 上一主题 下一主题

主题 558|帖子 558|积分 1674


目录
说明
效果
项目 
代码
下载

说明

利用周杰大佬的开源项目 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<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.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();
        }

        /// <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);
        }
    }
}
  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. }
复制代码
下载

源码下载

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表