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(); }
protected override void ProcessInternal(ThumbnailGeneratorSettings settings, GeneratorEntry entry) { try { entry.State = JobStates.Processing; _wrapper = new FrameConverterWrapper(FfmpegExePath) { Intervall = settings.Intervall, Width = settings.Width, Height = settings.Height }; _wrapper.ProgressChanged += (s, progress) => { entry.Update(null, progress); }; string thumbfile = Path.ChangeExtension(settings.VideoFile, "thumbs"); entry.Update("Extracting Frames", 0); _wrapper.VideoFile = settings.VideoFile; _wrapper.GenerateRandomOutputPath(); string tempPath = _wrapper.OutputPath; _wrapper.Execute(); if (_canceled) { return; } entry.Update("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); entry.DoneType = JobDoneTypes.Success; entry.Update("Done", 1); } catch (Exception) { entry.DoneType = JobDoneTypes.Failure; } finally { entry.State = JobStates.Done; if (_canceled) { entry.DoneType = JobDoneTypes.Cancelled; } } }