public static async Task CopyLastFrame(int lastFrameNum) { if (I.canceled) { return; } try { lastFrameNum--; // We have to do this as extracted frames start at 0, not 1 bool frameFolderInput = IoUtils.IsPathDirectory(I.current.inPath); string targetPath = Path.Combine(I.current.framesFolder, lastFrameNum.ToString().PadLeft(Padding.inputFrames, '0') + I.current.framesExt); if (File.Exists(targetPath)) { return; } Size res = IoUtils.GetImage(IoUtils.GetFilesSorted(I.current.framesFolder, false).First()).Size; if (frameFolderInput) { string lastFramePath = IoUtils.GetFilesSorted(I.current.inPath, false).Last(); await FfmpegExtract.ExtractLastFrame(lastFramePath, targetPath, res); } else { await FfmpegExtract.ExtractLastFrame(I.current.inPath, targetPath, res); } } catch (Exception e) { Logger.Log("CopyLastFrame Error: " + e.Message); } }
static async Task RunEntry(InterpSettings entry) { SetBusy(true); Program.mainForm.SetWorking(true); if (!EntryIsValid(entry)) { Logger.Log("Queue: Skipping entry because it's invalid."); Program.batchQueue.Dequeue(); return; } string fname = Path.GetFileName(entry.inPath); if (IoUtils.IsPathDirectory(entry.inPath)) { fname = Path.GetDirectoryName(entry.inPath); } Logger.Log($"Queue: Processing {fname} ({entry.interpFactor}x {entry.ai.aiNameShort})."); Program.mainForm.LoadBatchEntry(entry); // Load entry into GUI Interpolate.current = entry; Program.mainForm.runBtn_Click(null, null); while (Program.busy) { await Task.Delay(500); } Program.batchQueue.Dequeue(); Program.mainForm.SetWorking(false); Logger.Log($"Queue: Done processing {fname} ({entry.interpFactor}x {entry.ai.aiNameShort})."); }
public void UpdatePaths(string inPathArg, string outPathArg) { inPath = inPathArg; outPath = outPathArg; tempFolder = InterpolateUtils.GetTempFolderLoc(inPath, outPath); framesFolder = Path.Combine(tempFolder, Paths.framesDir); interpFolder = Path.Combine(tempFolder, Paths.interpDir); inputIsFrames = IoUtils.IsPathDirectory(inPath); }
public static void DeleteSource(string path) { Logger.Log("[FFCmds] Deleting input file/dir: " + path, true); if (IoUtils.IsPathDirectory(path) && Directory.Exists(path)) { Directory.Delete(path, true); } if (!IoUtils.IsPathDirectory(path) && File.Exists(path)) { File.Delete(path); } }
public static async Task <int> GetFrameCountAsync(string path, int retryCount = 3) { Logger.Log($"Getting frame count ({path})", true); long filesize = IoUtils.GetFilesize(path); QueryInfo hash = new QueryInfo(path, filesize); if (filesize > 0 && CacheContains(hash)) { Logger.Log($"Cache contains this hash, using cached value.", true); return(GetFromCache(hash)); } else { Logger.Log($"Hash not cached, reading frame count.", true); } int frameCount; if (IoUtils.IsPathDirectory(path)) { frameCount = IoUtils.GetAmountOfFiles(path, false); } else { frameCount = await FfmpegCommands.GetFrameCountAsync(path); } if (frameCount > 0) { Logger.Log($"Adding hash with value {frameCount} to cache.", true); cache.Add(hash, frameCount); } else { if (retryCount > 0) { Logger.Log($"Got {frameCount} frames, retrying ({retryCount} left)", true); Clear(); frameCount = await GetFrameCountAsync(path, retryCount - 1); } else { Logger.Log($"Failed to get frames and out of retries ({frameCount} frames for {path})", true); } } return(frameCount); }
public static bool InputIsValid(string inDir, string outDir, Fraction fpsIn, float factor, I.OutMode outMode) { try { bool passes = true; bool isFile = !IoUtils.IsPathDirectory(inDir); float fpsOut = fpsIn.GetFloat() * factor; if ((passes && isFile && !IoUtils.IsFileValid(inDir)) || (!isFile && !IoUtils.IsDirValid(inDir))) { UiUtils.ShowMessageBox("Input path is not valid!"); passes = false; } if (passes && !IoUtils.IsDirValid(outDir)) { UiUtils.ShowMessageBox("Output path is not valid!"); passes = false; } if (passes && fpsOut < 1f || fpsOut > 1000f) { string imgSeqNote = isFile ? "" : "\n\nWhen using an image sequence as input, you always have to specify the frame rate manually."; UiUtils.ShowMessageBox($"Invalid output frame rate ({fpsOut}).\nMust be 1-1000.{imgSeqNote}"); passes = false; } string fpsLimitValue = Config.Get(Config.Key.maxFps); float fpsLimit = (fpsLimitValue.Contains("/") ? new Fraction(Config.Get(Config.Key.maxFps)).GetFloat() : fpsLimitValue.GetFloat()); if (outMode == I.OutMode.VidGif && fpsOut > 50 && !(fpsLimit > 0 && fpsLimit <= 50)) { Logger.Log($"Warning: GIF will be encoded at 50 FPS instead of {fpsOut} as the format doesn't support frame rates that high."); } if (!passes) { I.Cancel("Invalid settings detected.", true); } return(passes); } catch (Exception e) { Logger.Log($"Failed to run InputIsValid: {e.Message}\n{e.StackTrace}", true); return(false); } }
public static bool CheckPathValid(string path) { if (IoUtils.IsPathDirectory(path)) { if (!IoUtils.IsDirValid(path)) { UiUtils.ShowMessageBox("Input directory is not valid.\nMake sure it still exists and hasn't been renamed or moved!"); I.Cancel(); return(false); } } else { if (!IsVideoValid(path)) { UiUtils.ShowMessageBox("Input video file is not valid.\nMake sure it still exists and hasn't been renamed or moved!"); return(false); } } return(true); }
public InterpSettings(string inPathArg, string outPathArg, AI aiArg, Fraction inFpsDetectedArg, Fraction inFpsArg, float interpFactorArg, float itsScale, Interpolate.OutMode outModeArg, ModelCollection.ModelInfo modelArg) { inPath = inPathArg; outPath = outPathArg; ai = aiArg; inFpsDetected = inFpsDetectedArg; inFps = inFpsArg; interpFactor = interpFactorArg; outFps = inFpsArg * (double)interpFactorArg; outItsScale = itsScale; outMode = outModeArg; model = modelArg; alpha = false; stepByStep = false; framesExt = ""; interpExt = ""; try { tempFolder = InterpolateUtils.GetTempFolderLoc(inPath, outPath); framesFolder = Path.Combine(tempFolder, Paths.framesDir); interpFolder = Path.Combine(tempFolder, Paths.interpDir); inputIsFrames = IoUtils.IsPathDirectory(inPath); } catch { Logger.Log("Tried to create InterpSettings struct without an inpath. Can't set tempFolder, framesFolder and interpFolder.", true); tempFolder = ""; framesFolder = ""; interpFolder = ""; inputIsFrames = false; } _inputResolution = new Size(0, 0); _scaledResolution = new Size(0, 0); RefreshExtensions(); }
static bool EntryIsValid(InterpSettings entry) { if (entry.inPath == null || (IoUtils.IsPathDirectory(entry.inPath) && !Directory.Exists(entry.inPath)) || (!IoUtils.IsPathDirectory(entry.inPath) && !File.Exists(entry.inPath))) { Logger.Log("Queue: Can't process queue entry: Input path is invalid."); return(false); } if (entry.outPath == null || (!Directory.Exists(entry.outPath) && Config.GetInt("outFolderLoc") != 1)) { Logger.Log("Queue: Can't process queue entry: Output path is invalid."); return(false); } if (IoUtils.GetAmountOfFiles(Path.Combine(Paths.GetPkgPath(), entry.ai.pkgDir), true) < 1) { Logger.Log("Queue: Can't process queue entry: Selected AI is not available."); return(false); } return(true); }
public static async Task ExtractLastFrame(string inputFile, string outputPath, Size size) { if (QuickSettingsTab.trimEnabled) { return; } if (IoUtils.IsPathDirectory(outputPath)) { outputPath = Path.Combine(outputPath, "last.png"); } bool isPng = (Path.GetExtension(outputPath).ToLower() == ".png"); string comprArg = isPng ? pngCompr : ""; string pixFmt = "-pix_fmt " + (isPng ? $"rgb24 {comprArg}" : "yuvj420p"); string sizeStr = (size.Width > 1 && size.Height > 1) ? $"-s {size.Width}x{size.Height}" : ""; string trim = QuickSettingsTab.trimEnabled ? $"-ss {QuickSettingsTab.GetTrimEndMinusOne()} -to {QuickSettingsTab.trimEnd}" : ""; string sseof = string.IsNullOrWhiteSpace(trim) ? "-sseof -1" : ""; string args = $"{sseof} -i {inputFile.Wrap()} -update 1 {pixFmt} {sizeStr} {trim} {outputPath.Wrap()}"; await RunFfmpeg(args, LogMode.Hidden); }
public static async Task <Image> GetThumbnail(string path) { string imgOnDisk = Path.Combine(Paths.GetDataPath(), "thumb-temp.jpg"); try { if (!IoUtils.IsPathDirectory(path)) // If path is video - Extract first frame { await FfmpegExtract.ExtractSingleFrame(path, imgOnDisk, 1); return(IoUtils.GetImage(imgOnDisk)); } else // Path is frame folder - Get first frame { return(IoUtils.GetImage(IoUtils.GetFilesSorted(path)[0])); } } catch (Exception e) { Logger.Log("GetThumbnail Error: " + e.Message, true); return(null); } }
public InterpSettings(string serializedData) { inPath = ""; outPath = ""; ai = Implementations.networks[0]; inFpsDetected = new Fraction(); inFps = new Fraction(); interpFactor = 0; outFps = new Fraction(); outMode = Interpolate.OutMode.VidMp4; model = null; alpha = false; stepByStep = false; _inputResolution = new Size(0, 0); _scaledResolution = new Size(0, 0); framesExt = ""; interpExt = ""; Dictionary <string, string> entries = new Dictionary <string, string>(); foreach (string line in serializedData.SplitIntoLines()) { if (line.Length < 3) { continue; } string[] keyValuePair = line.Split('|'); entries.Add(keyValuePair[0], keyValuePair[1]); } foreach (KeyValuePair <string, string> entry in entries) { switch (entry.Key) { case "INPATH": inPath = entry.Value; break; case "OUTPATH": outPath = entry.Value; break; case "AI": ai = Implementations.GetAi(entry.Value); break; case "INFPSDETECTED": inFpsDetected = new Fraction(entry.Value); break; case "INFPS": inFps = new Fraction(entry.Value); break; case "OUTFPS": outFps = new Fraction(entry.Value); break; case "INTERPFACTOR": interpFactor = float.Parse(entry.Value); break; case "OUTMODE": outMode = (Interpolate.OutMode)Enum.Parse(typeof(Interpolate.OutMode), entry.Value); break; case "MODEL": model = AiModels.GetModelByName(ai, entry.Value); break; case "INPUTRES": _inputResolution = FormatUtils.ParseSize(entry.Value); break; case "OUTPUTRES": _scaledResolution = FormatUtils.ParseSize(entry.Value); break; case "ALPHA": alpha = bool.Parse(entry.Value); break; case "STEPBYSTEP": stepByStep = bool.Parse(entry.Value); break; case "FRAMESEXT": framesExt = entry.Value; break; case "INTERPEXT": interpExt = entry.Value; break; } } try { tempFolder = InterpolateUtils.GetTempFolderLoc(inPath, outPath); framesFolder = Path.Combine(tempFolder, Paths.framesDir); interpFolder = Path.Combine(tempFolder, Paths.interpDir); inputIsFrames = IoUtils.IsPathDirectory(inPath); } catch { Logger.Log("Tried to create InterpSettings struct without an inpath. Can't set tempFolder, framesFolder and interpFolder.", true); tempFolder = ""; framesFolder = ""; interpFolder = ""; inputIsFrames = false; } RefreshExtensions(); }