I want to extract the current image from the FFMPEG standard output and show it on a C# form. The stream source itself is a h264 raw data which converted into image and piped to the standard output. Here is my code, but I have no idea how to process the output (maybe MemoryStream):
public Process ffproc = new Process();
private void xxxFFplay()
{
ffproc.StartInfo.FileName = "ffmpeg.exe";
ffproc.StartInfo.Arguments = "-y -i udp://127.0.0.1:8888/ -q:v 1 -huffman optimal -update 1 -f mjpeg -";
ffproc.StartInfo.CreateNoWindow = true;
ffproc.StartInfo.RedirectStandardOutput = true;
ffproc.StartInfo.UseShellExecute = false;
ffproc.EnableRaisingEvents = true;
ffproc.OutputDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
fproc.ErrorDataReceived += (o, e) => Debug.WriteLine(e.Data ?? "NULL", "ffplay");
ffproc.Exited += (o, e) => Debug.WriteLine("Exited", "ffplay");
ffproc.Start();
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
var internalWorker = sender as BackgroundWorker;
Process p = e.Argument as Process;
buffer = new MemoryStream();
bufferWriter = new BinaryWriter(buffer);
using (var reader = new BinaryReader(p.StandardOutput.BaseStream))
{
while (true)
{
bufferWriter.Write(1);
var img = (Bitmap)Image.FromStream(buffer);
pictureBox1.Image = img;
//get the jpg image
}
}
}
catch (Exception ex)
{
// Log the error, continue processing the live stream
}
}
Any help would be appreciated!