Inheritance: NMaier.SimpleDlna.Utilities.Logging
Ejemplo n.º 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;
                        }
                    }
                }
            }
        }
Ejemplo n.º 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;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public MemoryStream GetThumbnail(object item, ref int width, ref int height)
        {
            Image img = null;

            if (item is Stream)
            {
                img = Image.FromStream(item as Stream);
            }
            else
            if (item is FileInfo)
            {
                img = Image.FromFile((item as FileInfo).FullName);
            }
            else
            {
                throw new NotSupportedException();
            }
            using (img) {
                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;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private MemoryStream GetThumbnailInternal(FileInfo file, ref int width,
                                                  ref int height)
        {
            if (file.Extension == ".mkv" && MKVTools.Loaded)
            {
                var thumbIdx = MKVTools.FindThumbnail(file.FullName);
                if (thumbIdx != MKVTools.NoThumbnail)
                {
                    var origThumb = MKVTools.ExtractThumbnail(file.FullName, thumbIdx);
                    var origImage = Image.FromStream(origThumb);
                    var scaled    = ThumbnailMaker.ResizeImage(origImage, width, height, ThumbnailMakerBorder.Bordered);
                    var rv        = new MemoryStream();
                    scaled.Save(rv, ImageFormat.Jpeg);
                    return(rv);
                }
            }

            Exception last = null;

            for (var best = IdentifyBestCapturePosition(file);
                 best >= 0;
                 best -= Math.Max(best / 2, 5))
            {
                try
                {
                    using (var p = new Process())
                    {
                        var sti = p.StartInfo;
#if !DEBUG
                        sti.CreateNoWindow = true;
#endif
                        sti.UseShellExecute        = false;
                        sti.FileName               = FFmpeg.FFmpegExecutable;
                        sti.Arguments              = $"-v quiet -ss {best} -i \"{file.FullName}\" -an -frames:v 1 -f image2 pipe:";
                        sti.LoadUserProfile        = false;
                        sti.RedirectStandardOutput = true;
                        p.Start();

                        DebugFormat("Running: {0} {1}", sti.FileName, sti.Arguments);
                        return(GetThumbnailFromProcess(p, ref width, ref height));
                    }
                }
                catch (Exception ex)
                {
                    last = ex;
                }
            }
            throw last ?? new Exception("Not reached");
        }
Ejemplo n.º 5
0
        public MemoryStream GetThumbnail(object item, ref int width,
                                         ref int height)
        {
            Image img;
            var   stream = item as Stream;

            if (stream != null)
            {
                img = Image.FromStream(stream);
            }
            else
            {
                var fi = item as FileInfo;
                if (fi != null)
                {
                    img = Image.FromFile(fi.FullName);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            using (img)
            {
                using (var scaled = ThumbnailMaker.ResizeImage(
                           img, width, height, ThumbnailMakerBorder.Borderless))
                {
                    width  = scaled.Width;
                    height = scaled.Height;
                    var rv = new MemoryStream();
                    try
                    {
                        scaled.Save(rv, ImageFormat.Jpeg);
                        return(rv);
                    }
                    catch (Exception)
                    {
                        rv.Dispose();
                        throw;
                    }
                }
            }
        }