我在c#
中使用过程对象我也在使用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的标题?
答案 0 :(得分:4)
我认为将输出视为文本流不是正确的做法。这样的东西对我有用,只是直接从输出管道读取数据,它不需要转换。
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();