我正在尝试让我的 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/