c# - 使用 FFMPEG 进程从 rtsp 获取所有帧

标签 c# .net winforms ffmpeg rtsp

运行使用 FFMPEG 读取 RTSP 流的命令的过程,我需要捕获每一帧以将其显示在图片框中;我在 void OnOutputDataReceived(object sender, DataReceivedEventArgs e) 中接收数据,但是一旦我将 e.data 转换为 byte[] 并生成位图,应用程序就会崩溃。

我的代码:

private void Form1_Load_1(object sender, EventArgs e)
    {
        Process proc = new Process();
        proc.StartInfo.FileName = "ffmpeg";
        proc.StartInfo.Arguments = "-i rtsp://user:foscam@50.197.211.181:9826/videoMain -an -vcodec mjpeg -s 640x480 -threads 1 -aspect 16:9 -q:v 2 -updatefirst 1 -b:v 64k -f -";
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.OutputDataReceived += OnOutputDataReceived;
        if (!proc.Start())
        {
            Console.WriteLine("Error starting");
            return;
        }
        proc.BeginOutputReadLine();
        StreamReader reader = proc.StandardError;
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
        proc.Close();
    }

    void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        byte[] bytes = new byte[e.Data.Length * sizeof(char)];
        System.Buffer.BlockCopy(e.Data.ToCharArray(), 0, bytes, 0, bytes.Length);
        Bitmap bmp;
        using (var ms = new MemoryStream(bytes))
        {
    // CRASH SITE

            bmp = new Bitmap(ms);
        }
        pictureBox1.Image = bmp;
    }

Bytes

Crash

最佳答案

如果您想处理来自相机的图像文件,使用 FFMPEG 并不是最好的方法,因为您在设备和 .NET 代码之间添加了一个独立的层。

您需要使用专门为此而构建的 AFORGE.NET。我为 FOSCAM 相机制作了两个应用程序(一个 Windows 桌面应用程序和一个 WinPhone 7x),AFORGE 是他们的最佳选择。不幸的是,我没有代码了,但尝试使用 AFORGE 代替坚持 FFMPEG(我猜 AFORGE 有用于导出到 FFMPEG 的示例)。

看这里:
http://www.aforgenet.com/framework/

关于c# - 使用 FFMPEG 进程从 rtsp 获取所有帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048758/

相关文章:

c# - 录制一段固定时间的喊话

c# - 有什么方法可以将迭代的 id 传递给 jquery?

c# - Linq 查询中的 Select 操作何时变为投影操作?

c# - WinForm 设置未保存

.net - 如何设计结构相同的窗体?

c# - 如何计算在我的窗口窗体中按下了多少个按钮?

c# - Monogame 内容管道已停止工作

c# - .Net Core CLI 可以从 C# 应用程序中运行吗

c# - 有没有一种方法可以序列化实现 ICollection 的类的公共(public)属性

c# - 如何在从组合框中选择任何值时连接到数据库