private MemoryStream GetThumbnailFromProcess(Process p, ref int width, ref int height)
{
Debug("Starting ffmpeg");
using (var thumb = new MemoryStream()) {
var pump = new StreamPump(p.StandardOutput.BaseStream, thumb, null, 4096);
if (!p.WaitForExit(20000)) {
p.Kill();
throw new ArgumentException("ffmpeg timed out");
}
if (p.ExitCode != 0) {
throw new ArgumentException("ffmpeg does not understand the stream");
}
Debug("Done ffmpeg");
if (!pump.Wait(2000)) {
throw new ArgumentException("stream reading timed out");
}
if (thumb.Length == 0) {
throw new ArgumentException("ffmpeg did not produce a result");
}
using (var img = Image.FromStream(thumb)) {
using (var scaled = ThumbnailMaker.ResizeImage(img, ref width, ref height)) {
var rv = new MemoryStream();
try {
scaled.Save(rv, ImageFormat.Jpeg);
return rv;
}
catch (Exception) {
rv.Dispose();
throw;
}
}
}
}
}