private CompletionStatus GenerateDeshakerLogSegment(MediaEncoderSettings settings, string inputFile, int segment, long jobStart, long frameStart, long frameEnd, ProcessStartOptions jobOptions) { // Write Deshaker Pass 1 script to file. string Script = MediaEncoderScript.GenerateDeshakerScript(settings, inputFile, segment, frameStart, frameEnd); File.WriteAllText(settings.DeshakerScript, Script); // Run pass. jobOptions.IsMainTask = true; jobOptions.Title = "Running Deshaker Prescan"; jobOptions.ResumePos = frameStart - jobStart; CompletionStatus Result = FFmpeg.MediaEncoder.ConvertToAvi(settings.DeshakerScript, settings.DeshakerTempOut, false, jobOptions); File.Delete(settings.DeshakerScript); File.Delete(settings.DeshakerTempOut); return(Result); }
public CompletionStatus GenerateDeshakerLog(MediaEncoderSettings settings, string inputFile) { // Prepare Deshaker settings. settings.DeshakerSettings.Pass = 1; settings.DeshakerSettings.LogFile = settings.DeshakerTempLog; settings.DeshakerSettings.SourcePixelAspectRatio = settings.SourceAspectRatio.Value; // settings.DeshakerSettings.AppendToFile = true; File.Delete(settings.DeshakerLog); // Start UI. CompletionStatus Result = CompletionStatus.Success; object JobId = "Deshaker"; FFmpegConfig.UserInterfaceManager.Start(JobId, "Running Deshaker Prescan"); ProcessStartOptions JobOptions = new ProcessStartOptions(JobId, "Getting Frame Count", false); // Get frame count. settings.CalculateSize(); int FrameStart = (int)((settings.DeshakerSettings.PrescanStart ?? 0) * settings.SourceFrameRate.Value); int FrameEnd = (int)((settings.DeshakerSettings.PrescanEnd ?? 0) * settings.SourceFrameRate.Value); string Script = MediaEncoderScript.GenerateDeshakerScript(settings, inputFile, 0, FrameStart, FrameEnd); File.WriteAllText(settings.DeshakerScript, Script); JobOptions.FrameCount = AvisynthTools.GetFrameCount(settings.DeshakerScript, JobOptions); // Pad file start. using (StreamWriter sw = new StreamWriter(File.Open(settings.DeshakerLog, FileMode.Create), Encoding.ASCII)) { for (int i = 0; i < FrameStart; i++) { sw.WriteLine((i + 1).ToString().PadLeft(7) + " skipped # 0.00 0.00 "); } } // Run segments. var Segments = settings.DeshakerSettings.Segments; if (JobOptions.FrameCount > 0) { for (int i = 0; i < Segments.Count; i++) { // Get start position of next segment. long NextSegmentStart = i < Segments.Count - 1 ? Segments[i + 1].FrameStart : 0; if (NextSegmentStart == 0 || NextSegmentStart > FrameStart) // Enforce PrescanStart for preview { long SegmentStart = Segments[i].FrameStart; long SegmentEnd = NextSegmentStart > 0 ? NextSegmentStart - 1 : 0; // Enforce PrescanEnd for preview if ((FrameEnd > 0 && SegmentStart > FrameEnd) || (SegmentEnd > 0 && SegmentStart > SegmentEnd)) { break; } if ((FrameStart > 0 && FrameStart > SegmentStart) || SegmentStart == 0) { SegmentStart = FrameStart; } if ((FrameEnd > 0 && FrameEnd < SegmentEnd) || SegmentEnd == 0) { SegmentEnd = FrameEnd; } Result = GenerateDeshakerLogSegment(settings, inputFile, i, FrameStart, SegmentStart, SegmentEnd, JobOptions); if (Result != CompletionStatus.Success) { break; } // Merge log segment into log file and set right frame numbers. using (StreamWriter sw = new StreamWriter(File.Open(settings.DeshakerLog, FileMode.Append), Encoding.ASCII)) { using (StreamReader sr = new StreamReader(File.OpenRead(settings.DeshakerTempLog), Encoding.ASCII)) { string LogLine, LogNum, LogNumField, LineOut; long NewLineNum = SegmentStart; LogLine = sr.ReadLine(); while (LogLine != null) { if (LogLine.Length > 7) { LogNum = LogLine.Substring(0, 7).Trim(); if (LogNum.Length > 0) { LogNumField = LogNum[LogNum.Length - 1].ToString(); if (LogNumField != "A" && LogNumField != "B") // For interlaced videos { LogNumField = ""; } NewLineNum++; // Log file starts at 1, not 0. LineOut = (NewLineNum.ToString() + LogNumField).PadLeft(7) + LogLine.Substring(7, LogLine.Length - 8); sw.WriteLine(LineOut); } } LogLine = sr.ReadLine(); } } } File.Delete(settings.DeshakerTempLog); } } } else { Result = CompletionStatus.Error; } // End UI. FFmpegConfig.UserInterfaceManager.Stop(JobId); return(Result); }