使用FFMPEG进程从rtsp获得所有帧
本文关键字:rtsp FFMPEG 进程 使用 | 更新日期: 2023-09-27 18:16:56
进程运行一个命令,读取一个RTSP流与FFMPEG,我需要捕捉每一帧显示在一个图片框;我在void OnOutputDataReceived(对象发送者,DataReceivedEventArgs e)中接收数据,但是一旦我将e.c ata转换为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;
}
字节事故
如果你想处理来自相机的图像文件,使用FFMPEG不是最好的方法,因为你在设备和。net代码之间添加了一个独立的层。
你需要使用AFORGE。NET,它是专门为此构建的。我为FOSCAM相机做了两个应用程序(一个是Windows桌面相机,一个是WinPhone 7x相机),AFORGE是他们最好的选择。不幸的是,我没有代码了,但尝试AFORGE而不是坚持使用FFMPEG(我猜AFORGE有导出到FFMPEG的示例)。
看这里:http://www.aforgenet.com/framework/