Пример #1
0
        protected override GeneratorResult ProcessInternal(HeatmapGeneratorSettings settings, GeneratorEntry entry)
        {
            try
            {
                entry.State = JobStates.Processing;

                _wrapper = new FfmpegWrapper(FfmpegExePath);

                var videoInfo = _wrapper.GetVideoInfo(settings.VideoFile);

                if (videoInfo.Duration <= TimeSpan.Zero)
                {
                    entry.State    = JobStates.Done;
                    entry.DoneType = JobDoneTypes.Failure;
                    entry.Update("Failed", 1);
                    return(GeneratorResult.Failed());
                }

                TimeSpan duration = videoInfo.Duration;

                //TODO

                string script  = ViewModel.GetScriptFile(settings.VideoFile);
                var    actions = ViewModel.LoadScriptActions(script, null);

                if (actions == null || actions.Count == 0)
                {
                    entry.State    = JobStates.Done;
                    entry.DoneType = JobDoneTypes.Failure;
                    entry.Update("Failed", 1);
                    return(GeneratorResult.Failed());
                }

                List <TimeSpan> timeStamps = ViewModel.FilterDuplicates(actions.ToList()).Select(s => s.TimeStamp).ToList();
                Brush           heatmap    = HeatMapGenerator.Generate2(timeStamps, TimeSpan.FromSeconds(10), TimeSpan.Zero, duration);

                DrawingVisual visual = new DrawingVisual();
                using (DrawingContext context = visual.RenderOpen())
                {
                    var rect = new Rect(0, 0, settings.Width, settings.Height);
                    context.DrawRectangle(heatmap, null, rect);

                    if (settings.AddShadow)
                    {
                        LinearGradientBrush shadow = new LinearGradientBrush();
                        shadow.StartPoint = new Point(0.5, 0);
                        shadow.EndPoint   = new Point(0.5, 1);

                        shadow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF - 0x20, 0, 0, 0), 0));
                        shadow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF - 0xcc, 0, 0, 0), 0.98));
                        shadow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF - 0x50, 0, 0, 0), 0.98));
                        shadow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF - 0x50, 0, 0, 0), 1));

                        context.DrawRectangle(shadow, null, rect);
                    }
                }

                RenderTargetBitmap bitmap = new RenderTargetBitmap(settings.Width, settings.Height, 96, 96, PixelFormats.Pbgra32);
                bitmap.Render(visual);

                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));

                using (FileStream stream = new FileStream(settings.OutputFile, FileMode.Create))
                    encoder.Save(stream);

                entry.DoneType = JobDoneTypes.Success;
                entry.Update("Done", 1);

                return(GeneratorResult.Succeeded(settings.OutputFile));
            }
            catch (Exception)
            {
                entry.Update("Failed", 1);
                entry.DoneType = JobDoneTypes.Failure;

                return(GeneratorResult.Failed());
            }
            finally
            {
                entry.State = JobStates.Done;

                if (_canceled)
                {
                    entry.DoneType = JobDoneTypes.Cancelled;
                    entry.Update("Cancelled", 1);
                }
            }
        }
Пример #2
0
        protected override GeneratorResult ProcessInternal(ThumbnailGeneratorSettings settings, GeneratorEntry entry)
        {
            try
            {
                entry.State = JobStates.Processing;

                _wrapper = new FfmpegWrapper(FfmpegExePath);

                double intervall = settings.Intervall;

                if (settings.Intervall < 1)
                {
                    var info = _wrapper.GetVideoInfo(settings.VideoFile);
                    if (!info.IsGoodEnough())
                    {
                        entry.Update("Failed", 1);
                        entry.DoneType = JobDoneTypes.Failure;
                        return(GeneratorResult.Failed());
                    }

                    const double targetFrameCount = 500.0;

                    intervall = info.Duration.TotalSeconds / targetFrameCount;
                    intervall = Math.Min(Math.Max(1, intervall), 10);
                }

                FrameConverterArguments arguments = new FrameConverterArguments
                {
                    StatusUpdateHandler = (progress) => { entry.Update(null, progress); },
                    InputFile           = settings.VideoFile,
                    OutputDirectory     = FfmpegWrapper.CreateRandomTempDirectory(),
                    Intervall           = intervall,
                    Width    = settings.Width,
                    Height   = settings.Height,
                    ClipLeft = settings.ClipLeft,
                    DeLense  = settings.ClipLeft
                };

                string thumbfile = settings.OutputFile; // Path.ChangeExtension(settings.VideoFile, "thumbs");
                entry.Update("Extracting Frames", 0);

                var frames = _wrapper.ExtractFrames(arguments);

                if (_canceled)
                {
                    return(GeneratorResult.Failed());
                }

                entry.Update("Saving Thumbnails", 1);

                VideoThumbnailCollection thumbnails = new VideoThumbnailCollection();
                foreach (var frame in frames)
                {
                    thumbnails.Add(frame.Item1, frame.Item2);
                }

                using (FileStream stream = new FileStream(thumbfile, FileMode.Create, FileAccess.Write))
                {
                    thumbnails.Save(stream);
                }

                thumbnails.Dispose();

                entry.DoneType = JobDoneTypes.Success;
                entry.Update("Done", 1);

                return(GeneratorResult.Succeeded(thumbfile));
            }
            catch (Exception)
            {
                entry.DoneType = JobDoneTypes.Failure;
                return(GeneratorResult.Failed());
            }
            finally
            {
                entry.State = JobStates.Done;

                if (_canceled)
                {
                    entry.DoneType = JobDoneTypes.Cancelled;
                }
            }
        }
        protected override GeneratorResult ProcessInternal(ThumbnailBannerGeneratorSettings settings, GeneratorEntry entry)
        {
            try
            {
                entry.State = JobStates.Processing;

                _wrapper = new FfmpegWrapper(FfmpegExePath);

                var videoInfo = _wrapper.GetVideoInfo(settings.VideoFile);

                if (!videoInfo.IsGoodEnough())
                {
                    entry.State    = JobStates.Done;
                    entry.DoneType = JobDoneTypes.Failure;
                    entry.Update("Failed", 1);
                    return(GeneratorResult.Failed());
                }

                TimeSpan duration  = videoInfo.Duration;
                TimeSpan intervall = duration.Divide(settings.Columns * settings.Rows + 1);

                var frameArguments = new FrameConverterArguments
                {
                    Width               = 800,
                    Intervall           = intervall.TotalSeconds,
                    StatusUpdateHandler = (progress) => { entry.Update(null, progress); },
                    InputFile           = settings.VideoFile,
                    OutputDirectory     = FfmpegWrapper.CreateRandomTempDirectory()
                };

                entry.Update("Extracting Frames", 0);

                var frames = _wrapper.ExtractFrames(frameArguments);

                if (_canceled)
                {
                    return(GeneratorResult.Failed());
                }

                entry.Update("Saving Thumbnails", 1);

                List <ThumbnailBannerGeneratorImage> images = new List <ThumbnailBannerGeneratorImage>();

                foreach (var frame in frames)
                {
                    images.Add(new ThumbnailBannerGeneratorImage
                    {
                        Image    = frame.Item2,
                        Position = frame.Item1
                    });
                }

                ThumbnailBannerGeneratorData data = new ThumbnailBannerGeneratorData();
                data.Settings  = settings;
                data.Images    = images.ToArray();
                data.VideoName = settings.VideoFile;
                data.VideoInfo = videoInfo;
                data.FileSize  = new FileInfo(settings.VideoFile).Length;

                var result = CreateBanner(data);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create((BitmapSource)result));
                using (FileStream stream = new FileStream(settings.OutputFile, FileMode.Create))
                    encoder.Save(stream);

                Directory.Delete(frameArguments.OutputDirectory);

                entry.DoneType = JobDoneTypes.Success;
                entry.Update("Done", 1);

                return(GeneratorResult.Succeeded(settings.OutputFile));
            }
            catch (Exception)
            {
                entry.Update("Failed", 1);
                entry.DoneType = JobDoneTypes.Failure;

                return(GeneratorResult.Failed());
            }
            finally
            {
                entry.State = JobStates.Done;

                if (_canceled)
                {
                    entry.DoneType = JobDoneTypes.Cancelled;
                    entry.Update("Cancelled", 1);
                }
            }
        }