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

Rob*_*Rob 5 c# video ffmpeg stream

我尝试让我的Parrot AR Drone 2.0与windows机器配合使用.

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

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

我怎么能在我的应用程序中获得此视频?比如,一个简单的"框架"或"图像"充满了这些内容?

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

Fra*_*ser 5

您可以启动该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);
Run Code Online (Sandbox Code Playgroud)

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

// 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);
Run Code Online (Sandbox Code Playgroud)

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