コード例 #1
0
        public List <Tuple <TimeSpan, BitmapSource> > ExtractFrames(FrameConverterArguments arguments)
        {
            bool isTempDir = false;

            if (string.IsNullOrWhiteSpace(arguments.OutputDirectory))
            {
                isTempDir = true;
                arguments.OutputDirectory = CreateRandomTempDirectory();
            }

            FrameConverterWrapper wrapper = new FrameConverterWrapper(arguments, FfmpegExePath);

            ExecuteWrapper(wrapper);

            List <Tuple <TimeSpan, BitmapSource> > result = new List <Tuple <TimeSpan, BitmapSource> >();

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

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

                TimeSpan position = TimeSpan.FromSeconds((index - 0.5) * arguments.Intervall);

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

                result.Add(new Tuple <TimeSpan, BitmapSource>(position, frame));
                usedFiles.Add(file);
            }

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

            if (isTempDir)
            {
                Directory.Delete(arguments.OutputDirectory);
            }

            return(result);
        }
コード例 #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();
        }
コード例 #3
0
        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;
                }
            }
        }