c# - 在 C# 应用程序中显示 tcp 视频流(来自 FFPLAY/FFMPEG)

标签 c# video stream ffmpeg

我正在尝试让我的 Parrot AR Drone 2.0 在 Windows 机器上运行。

我有一个简单的 C# 应用程序来控制它 - 但现在我想要我的应用程序内部的视频流。

如果我执行 ffplay tcp://192.168.1.1:5555,它会连接到视频流并显示一个包含视频的窗口。

如何在我的应用程序中获取此视频?比如,一个简单的“框架”或“图片”,里面填满了该内容?

我从来没有用 C# 工作过那么多,所以任何帮助都会很棒。

最佳答案

您可以启动 ffplay处理然后 PInvoke SetParent将播放器窗口放在您的表单中,然后 MoveWindow对其进行定位。

为此,您需要定义以下内容。

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

然后就可以像这样使用这两个native方法了。

// start ffplay 
var ffplay = new Process
    {
        StartInfo =
            {
                FileName = "ffplay",
                Arguments = "tcp://192.168.1.1:5555",
                // hides the command window
                CreateNoWindow = true, 
                // redirect input, output, and error streams..
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false    
            }
    };

ffplay.EnableRaisingEvents = true;
ffplay.OutputDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.ErrorDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffplay.Exited += (o, e) => Debug.WriteLine("Exited", "ffplay");
ffplay.Start();

Thread.Sleep(200); // you need to wait/check the process started, then...

// child, new parent
// make 'this' the parent of ffmpeg (presuming you are in scope of a Form or Control)
SetParent(ffplay.MainWindowHandle, this.Handle);

// window, x, y, width, height, repaint
// move the ffplayer window to the top-left corner and set the size to 320x280
MoveWindow(ffplay.MainWindowHandle, 0, 0, 320, 280, true);

ffplay 的标准输出过程中,您通常在命令窗口中看到的文本是通过 ErrorDataReceived 处理的.设置 -loglevel类似于 fatal在传递给 ffplay 的参数中允许您减少引发的事件数量并允许您仅处理真正的失败。

关于c# - 在 C# 应用程序中显示 tcp 视频流(来自 FFPLAY/FFMPEG),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201894/

相关文章:

c# - Performance Monitor .NET CLR Networking 4.0.0.0 实例命名

c# - 如何将UTC时间转换为C#中任何其他时区的时间

audio - .MKV 到 .MP4 选择音频和字幕?

javascript - HTML5 - 在 View 播放器中打开 YouTube 视频

JavaScript window.URL 在函数中未定义

java - 在 java 中创建一个独立的 gui 控制台,扩展基于提示的应用程序

c# - 不支持关键字 : 'attachdbfilename' - MDF Database File (C#)

c# - OWIN MVC - 多个 LoginProvider,当前 LoginProvider

c# - 如何打开进程并使整个类(class)都可以使用 Steam 读/写器

algorithm - 如何从次线性空间/时间中的字符流计算回文?