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

2024-01-09

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

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

如果我执行ffplay tcp://192.168.1.1:5555它连接到视频流并显示带有视频的窗口。

如何在我的应用程序中获取该视频?就像一个简单的“框架”或“图像”填充了该内容?

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


您可以启动ffplay处理然后 PInvokeSetParent将播放器窗口放置在您的表单中并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);

然后你可以像这样使用这两个本地方法。

// 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 的参数中,您可以减少引发的事件数量,并允许您仅处理真正的故障。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C# 应用程序中显示 tcp 视频流(来自 FFPLAY / FFMPEG) 的相关文章

随机推荐