public void FinalizeReplace(EncodingCompletedEventArgs jobInfo) {
            EditVideoBusiness EditBusiness = new EditVideoBusiness();
            string RelativePath = jobInfo.OldFileName.StartsWith(Settings.NaturalGroundingFolder) ? jobInfo.OldFileName.Substring(Settings.NaturalGroundingFolder.Length) : jobInfo.OldFileName;
            Media EditVideo = EditBusiness.GetVideoByFileName(RelativePath);
            System.Threading.Thread.Sleep(200); // Give MPC time to release the file.
            string OriginalPath = Path.Combine(Path.GetDirectoryName(jobInfo.OldFileName), "Original", Path.GetFileName(jobInfo.OldFileName));
            string NewPath = Path.ChangeExtension(jobInfo.OldFileName, null) + Path.GetExtension(jobInfo.Settings.FinalFile);
            Directory.CreateDirectory(Path.GetDirectoryName(OriginalPath));
            PathManager.SafeMove(jobInfo.OldFileName, OriginalPath);
            PathManager.SafeMove(jobInfo.Settings.FinalFile, NewPath);
            jobInfo.Settings.FilePath = OriginalPath.Substring(Settings.NaturalGroundingFolder.Length);

            if (EditVideo != null) {
                EditVideo.FileName = NewPath.Substring(Settings.NaturalGroundingFolder.Length);
                EditVideo.Height = null;
                EditBusiness.Save();
            }
        }
 private EncodingCompletedEventArgs GetEncodingResults(MediaEncoderSettings settings, string finalFile, DateTime? startTime) {
     // Create encoding result object.
     EncodingCompletedEventArgs Result = null;
     if (finalFile == null || File.Exists(finalFile)) {
         Result = new EncodingCompletedEventArgs();
         Result.Settings = settings;
         Result.OldFileName = settings.FilePath;
         Result.NewFileName = settings.FinalFile;
         if (startTime.HasValue)
             Result.EncodingTime = DateTime.Now - startTime.Value;
         if (finalFile != null) {
             FileInfo FinalFileInfo = new FileInfo(finalFile);
             Result.NewFileSize = FinalFileInfo.Length;
             FinalFileInfo = new FileInfo(settings.FilePath);
             Result.OldFileSize = FinalFileInfo.Length;
         }
     }
     return Result;
 }
 private void business_EncodingCompleted(object sender, EncodingCompletedEventArgs e) {
     MediaEncodingCompletedWindow.Instance(e);
 }
 public void FinalizeKeep(EncodingCompletedEventArgs jobInfo) {
     string FinalFile = String.Format("{0} - Encoded.{1}", Path.ChangeExtension(jobInfo.OldFileName, null), jobInfo.Settings.Container);
     PathManager.SafeMove(jobInfo.NewFileName, FinalFile);
 }
        private void EncoderThread(object obj) {
            MediaEncoderSettings settings = obj as MediaEncoderSettings;
            settings.CompletionStatus = CompletionStatus.Success;
            DateTime StartTime = DateTime.Now;

            FFmpegConfig.UserInterfaceManager.Start(settings.JobIndex, "Processing Video");

            MediaEncoderSegments SegBusiness = PrepareExistingJob(settings);
            // If merging is completed, SegBusiness==null. If work is completed but not merged, SegBusiness.SegLeft = empty list.
            if (SegBusiness != null && SegBusiness.SegLeft.Count() > 0) {
                if (settings.Deshaker && (settings.DeshakerSettings.PrescanAction != PrescanType.Full || !settings.DeshakerSettings.PrescanCompleted)) {
                    settings.DeshakerSettings.PrescanAction = PrescanType.Full;
                    settings.CompletionStatus = GenerateDeshakerLog(settings, settings.InputFile);
                }

                if (settings.CompletionStatus == CompletionStatus.Success) {
                    // Encode audio stream
                    Task EncAudio = null;
                    if (settings.HasAudioOptions)
                        EncAudio = Task.Run(() => EncoderBusiness.EncodeAudio(settings));

                    // Encode video stream in segments
                    List<Task<CompletionStatus>> EncTasks = new List<Task<CompletionStatus>>();
                    if (settings.VideoAction != VideoAction.Copy) {
                        bool Cancel = false;
                        foreach (SegmentInfo seg in SegBusiness.SegLeft) {
                            MediaEncoderSettings EncSettings = settings.Clone();
                            EncSettings.ResumePos = seg.Start;
                            File.Delete(EncSettings.OutputFile);
                            EncTasks.Add(Task.Run(() => EncoderBusiness.EncodeVideo(EncSettings, seg.Length, SegBusiness.TotalFrames)));

                            // If there are more segments than max parallel instances, wait until some threads finish
                            if (EncTasks.Count >= settings.ParallelProcessing) {
                                Task.WaitAny(EncTasks.ToArray());
                                foreach (var item in EncTasks.ToArray()) {
                                    if (item.IsCompleted) {
                                        if (item.Result == CompletionStatus.Success)
                                            EncTasks.Remove(item);
                                        else {
                                            settings.CompletionStatus = item.Result;
                                            Cancel = true;
                                        }
                                    }
                                }
                            }
                            if (Cancel)
                                break;
                        }
                    }

                    EncAudio?.Wait();
                    Task.WaitAll(EncTasks.ToArray());
                }
            } else if (settings.CompletionStatus == CompletionStatus.None)
                settings.CompletionStatus = CompletionStatus.Success;

            if (FFmpegConfig.UserInterfaceManager.AppExited)
                return;

            // Check if encode is completed.
            EncodingCompletedEventArgs CompletedArgs = null;
            if (settings.CompletionStatus == CompletionStatus.Success) {
                CompletedArgs = FinalizeEncoding(settings, StartTime);
                if (settings.CompletionStatus == CompletionStatus.Success && CompletedArgs != null)
                    EncodingCompleted?.Invoke(this, CompletedArgs);

            }
            if (settings.CompletionStatus != CompletionStatus.Success) {
                CompletedArgs = GetEncodingResults(settings, null, StartTime);
                if (IsEncoding)
                    EncodingFailed?.Invoke(this, CompletedArgs);
            }

            if (IsEncoding || settings.CompletionStatus == CompletionStatus.Success)
                Application.Current.Dispatcher.Invoke(() => ProcessingQueue.Remove(settings));

            FFmpegConfig.UserInterfaceManager.Stop(settings.JobIndex);

            // Start next job.
            if (IsEncoding && ProcessingQueue.Count > 0)
                EncoderThread(ProcessingQueue.First());

            JobThread = null;
        }
 public static void Instance(EncodingCompletedEventArgs jobInfo) {
     MediaEncodingCompletedWindow NewForm = new MediaEncodingCompletedWindow();
     NewForm.jobInfo = jobInfo;
     NewForm.Show();
 }
 public void FinalizeKeep(EncodingCompletedEventArgs jobInfo) {
     string FinalFile = String.Format("{0} - Encoded.{1}",
         Path.Combine(Path.GetDirectoryName(Settings.NaturalGroundingFolder + jobInfo.OldFileName),
             Path.GetFileNameWithoutExtension(jobInfo.OldFileName)),
             jobInfo.Settings.EncodeMp4 ? "mp4" : "mkv");
     SafeMove(Settings.NaturalGroundingFolder + jobInfo.NewFileName, FinalFile);
 }
        public void FinalizeReplace(EncodingCompletedEventArgs jobInfo) {
            EditVideoBusiness EditBusiness = new EditVideoBusiness();
            Media EditVideo = EditBusiness.GetVideoByFileName(jobInfo.OldFileName);
            System.Threading.Thread.Sleep(200); // Give MPC time to release the file.
            string OriginalPath = Path.Combine(Path.GetDirectoryName(Settings.NaturalGroundingFolder + jobInfo.OldFileName), "Original", Path.GetFileName(jobInfo.OldFileName));
            string NewPath = Path.Combine(Path.GetDirectoryName(Settings.NaturalGroundingFolder + jobInfo.OldFileName), Path.GetFileNameWithoutExtension(jobInfo.OldFileName) + Path.GetExtension(jobInfo.Settings.FinalFile));
            Directory.CreateDirectory(Path.GetDirectoryName(OriginalPath));
            SafeMove(Settings.NaturalGroundingFolder + jobInfo.OldFileName, OriginalPath);
            SafeMove(jobInfo.Settings.FinalFile, NewPath);
            jobInfo.Settings.FileName = OriginalPath.Substring(Settings.NaturalGroundingFolder.Length);

            if (EditVideo != null) {
                EditVideo.FileName = NewPath.Substring(Settings.NaturalGroundingFolder.Length);
                EditVideo.Height = null;
                EditBusiness.Save();
            }
        }
 public async Task FinalizeReplaceAsync(EncodingCompletedEventArgs jobInfo) {
     await Task.Run(() => FinalizeReplace(jobInfo));
 }
 private EncodingCompletedEventArgs GetEncodingResults(MediaEncoderSettings settings, string finalFile, DateTime? startTime) {
     // Create encoding result object.
     EncodingCompletedEventArgs Result = null;
     if (File.Exists(finalFile)) {
         Result = new EncodingCompletedEventArgs();
         Result.OldFileName = settings.FileName;
         Result.NewFileName = settings.FinalFile.Substring(Settings.NaturalGroundingFolder.Length);
         if (startTime.HasValue)
             Result.EncodingTime = DateTime.Now - startTime.Value;
         FileInfo FinalFileInfo = new FileInfo(finalFile);
         Result.NewFileSize = FinalFileInfo.Length;
         FinalFileInfo = new FileInfo(Settings.NaturalGroundingFolder + settings.FileName);
         Result.OldFileSize = FinalFileInfo.Length;
         Result.Settings = settings;
     }
     return Result;
 }