進程中運行一個讀取帶有FFMPEG的RTSP流的命令,並且我需要捕獲每個幀以將其顯示在一個picturebox中;我收到無效OnOutputDataReceived(對象發件人,DataReceivedEventArgs e)中的數據,但是一旦我將e.data轉換爲byte [],並生成應用程序崩潰的位圖。使用FFMPEG進程從rtsp
我的代碼:
private void Form1_Load_1(object sender, EventArgs e)
{
Process proc = new Process();
proc.StartInfo.FileName = "ffmpeg";
proc.StartInfo.Arguments = "-i rtsp://user:[email protected]: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的,但如果有任何視頻進行流進來,您收到多個幀。並且您似乎沒有做任何事情來檢測您的渲染部分的幀終止符 – fahadash