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

目录

说明

效果

项目 

代码

下载


说明

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

}

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);
        }
    }

}

下载

源码下载

  • 15
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天代码码天天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值