Ejemplo n.º 1
0
        private void SetStatus(ThumbnailProgressEntry entry, string text, double progress)
        {
            if (!this.CheckAccess())
            {
                this.Dispatcher.BeginInvoke(new Action(() => { SetStatus(entry, text, progress); }));
                return;
            }

            if (text != null)
            {
                entry.Status = text;
            }

            if (progress >= 0)
            {
                entry.Progress = progress;

                double totalProgress = Entries.Sum(e => Math.Min(1, Math.Max(0, e.Progress))) / Entries.Count;
                TaskbarItemInfo.ProgressValue = totalProgress;
            }
        }
Ejemplo n.º 2
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TaskbarItemInfo = new TaskbarItemInfo
            {
                ProgressState = TaskbarItemProgressState.Normal
            };

            var entries = Entries.ToList();

            string ffmpegexe = ViewModel.Settings.FfmpegPath;

            _processThread = new Thread(() =>
            {
                FrameConverterWrapper wrapper = new FrameConverterWrapper(ffmpegexe);
                _wrapper = wrapper;

                wrapper.Intervall = _settings.Intervall;
                wrapper.Width     = _settings.Width;
                wrapper.Height    = _settings.Height;

                ThumbnailProgressEntry currentEntry = null;

                wrapper.ProgressChanged += (s, progress) => { SetStatus(currentEntry, null, progress); };

                if (_settings.SkipIfExists)
                {
                    List <ThumbnailProgressEntry> skipped    = new List <ThumbnailProgressEntry>();
                    List <ThumbnailProgressEntry> nonskipped = new List <ThumbnailProgressEntry>();

                    foreach (var entry in entries)
                    {
                        if (_canceled)
                        {
                            return;
                        }

                        string thumbfile = Path.ChangeExtension(entry.FilePath, "thumbs");

                        if (File.Exists(thumbfile) && _settings.SkipIfExists)
                        {
                            SetStatus(entry, "Skipped", 1);
                            skipped.Add(entry);
                        }
                        else
                        {
                            nonskipped.Add(entry);
                        }
                    }

                    entries = skipped.Concat(nonskipped).ToList();

                    this.Dispatcher.Invoke(() => { Entries = entries; });
                }

                foreach (ThumbnailProgressEntry entry in entries)
                {
                    if (_canceled)
                    {
                        return;
                    }

                    if (entry.SkipThis)
                    {
                        continue;
                    }

                    currentEntry = entry;

                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        dataGrid.SelectedItem = currentEntry;
                        dataGrid.ScrollIntoView(currentEntry);
                    }));

                    string thumbfile = Path.ChangeExtension(currentEntry.FilePath, "thumbs");

                    if (File.Exists(thumbfile) && _settings.SkipIfExists)
                    {
                        SetStatus(currentEntry, "Skipped", 1);
                        continue;
                    }

                    SetStatus(currentEntry, "Extracting Frames", 0);

                    wrapper.VideoFile = currentEntry.FilePath;
                    wrapper.GenerateRandomOutputPath();
                    string tempPath = wrapper.OutputPath;
                    wrapper.Execute();

                    if (_canceled)
                    {
                        return;
                    }

                    SetStatus(currentEntry, "Saving Thumbnails", 1);

                    VideoThumbnailCollection thumbnails = new VideoThumbnailCollection();

                    List <string> usedFiles = new List <string>();

                    foreach (string file in Directory.EnumerateFiles(tempPath))
                    {
                        string number = Path.GetFileNameWithoutExtension(file);
                        int index     = int.Parse(number);

                        TimeSpan position = TimeSpan.FromSeconds(index * 10 - 5);

                        var frame = new BitmapImage();
                        frame.BeginInit();
                        frame.CacheOption = BitmapCacheOption.OnLoad;
                        frame.UriSource   = new Uri(file, UriKind.Absolute);
                        frame.EndInit();

                        thumbnails.Add(position, frame);
                        usedFiles.Add(file);
                    }

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

                    thumbnails.Dispose();

                    foreach (string tempFile in usedFiles)
                    {
                        File.Delete(tempFile);
                    }

                    Directory.Delete(tempPath);

                    SetStatus(currentEntry, "Done", 1);
                }

                _done = true;

                Dispatcher.BeginInvoke(new Action(() =>
                {
                    CloseButtonText = "Close";
                    if (_closeWhenDone)
                    {
                        Close();
                    }
                }));
            });

            _processThread.Start();
        }