예제 #1
0
        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");
                }

                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;
                        }
                    }
                }
            }
        }
예제 #2
0
        private static MemoryStream GetThumbnailFromProcess(Process p,
                                                            ref int width,
                                                            ref int height)
        {
            var lastPosition = 0L;

            using (var thumb = StreamManager.GetStream())
            {
                using (var pump = new StreamPump(
                           p.StandardOutput.BaseStream, thumb, 4096))
                {
                    pump.Pump(null);
                    while (!p.WaitForExit(20000))
                    {
                        if (lastPosition != thumb.Position)
                        {
                            lastPosition = thumb.Position;
                            continue;
                        }

                        p.Kill();
                        throw new ArgumentException("ffmpeg timed out");
                    }

                    if (p.ExitCode != 0)
                    {
                        throw new ArgumentException("ffmpeg does not understand the stream");
                    }
                    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, width, height,
                                                                       ThumbnailMakerBorder.Bordered))
                        {
                            width  = scaled.Width;
                            height = scaled.Height;
                            var rv = new MemoryStream();
                            try
                            {
                                scaled.Save(rv, ImageFormat.Jpeg);
                                return(rv);
                            }
                            catch (Exception)
                            {
                                rv.Dispose();
                                throw;
                            }
                        }
                    }
                }
            }
        }