从重定向进程中读取字节数组
Read a byte array from a redirected process
我在 c#
中使用进程 object
我也在用FFMPEG
我正在尝试从重定向输出中读取字节。我知道数据是图像,但是当我使用以下代码时,我没有得到图像字节数组。
这是我的代码:
var process = new Process();
process.StartInfo.FileName = @"C:\bin\ffmpeg.exe";
process.StartInfo.Arguments = @" -i rtsp://admin:admin@192.168.0.8:554/video_1 -an -f image2 -s 360x240 -vframes 1 -";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
var output = process.StandardOutput.ReadToEnd();
byte[] bytes = Encoding.ASCII.GetBytes(output);
第一个字节不是 jpeg 的 header?
我认为将输出作为文本流处理在这里不是正确的做法。像这样的东西对我有用,直接从输出管道读取数据,不需要转换。
var process = new Process();
process.StartInfo.FileName = @"C:\bin\ffmpeg.exe";
// take frame at 17 seconds
process.StartInfo.Arguments = @" -i c:\temp\input.mp4 -an -f image2 -vframes 1 -ss 00:00:17 pipe:1";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
FileStream baseStream = process.StandardOutput.BaseStream as FileStream;
byte[] imageBytes = null;
int lastRead = 0;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[4096];
do
{
lastRead = baseStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, lastRead);
} while (lastRead > 0);
imageBytes = ms.ToArray();
}
using (FileStream s = new FileStream(@"c:\temp\singleFrame.jpeg", FileMode.Create))
{
s.Write(imageBytes, 0, imageBytes.Length);
}
Console.ReadKey();
我在 c#
中使用进程 object我也在用FFMPEG
我正在尝试从重定向输出中读取字节。我知道数据是图像,但是当我使用以下代码时,我没有得到图像字节数组。
这是我的代码:
var process = new Process();
process.StartInfo.FileName = @"C:\bin\ffmpeg.exe";
process.StartInfo.Arguments = @" -i rtsp://admin:admin@192.168.0.8:554/video_1 -an -f image2 -s 360x240 -vframes 1 -";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
var output = process.StandardOutput.ReadToEnd();
byte[] bytes = Encoding.ASCII.GetBytes(output);
第一个字节不是 jpeg 的 header?
我认为将输出作为文本流处理在这里不是正确的做法。像这样的东西对我有用,直接从输出管道读取数据,不需要转换。
var process = new Process();
process.StartInfo.FileName = @"C:\bin\ffmpeg.exe";
// take frame at 17 seconds
process.StartInfo.Arguments = @" -i c:\temp\input.mp4 -an -f image2 -vframes 1 -ss 00:00:17 pipe:1";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
FileStream baseStream = process.StandardOutput.BaseStream as FileStream;
byte[] imageBytes = null;
int lastRead = 0;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[4096];
do
{
lastRead = baseStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, lastRead);
} while (lastRead > 0);
imageBytes = ms.ToArray();
}
using (FileStream s = new FileStream(@"c:\temp\singleFrame.jpeg", FileMode.Create))
{
s.Write(imageBytes, 0, imageBytes.Length);
}
Console.ReadKey();