From 706859f6ea5c885bafa9774fb9f80b79ba73ef2e Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Fri, 15 Jan 2021 15:07:40 +0100 Subject: [PATCH] Downscaling ("max video size") now also works for folder inputs --- Code/AudioVideo/FFmpegCommands.cs | 11 +++-------- Code/Data/InterpSettings.cs | 14 ++++++++------ Code/IO/IOUtils.cs | 16 ++++++++++++++++ Code/Magick/Dedupe.cs | 6 +++--- Code/Main/Interpolate.cs | 4 ++-- Code/Main/InterpolateSteps.cs | 2 +- Code/Main/InterpolateUtils.cs | 8 ++++---- Code/OS/AiProcess.cs | 8 ++++---- Code/UI/MainUiFunctions.cs | 20 +++++--------------- 9 files changed, 46 insertions(+), 43 deletions(-) diff --git a/Code/AudioVideo/FFmpegCommands.cs b/Code/AudioVideo/FFmpegCommands.cs index 0f6cbab..0cfa03a 100644 --- a/Code/AudioVideo/FFmpegCommands.cs +++ b/Code/AudioVideo/FFmpegCommands.cs @@ -60,7 +60,7 @@ namespace Flowframes DeleteSource(inputFile); } - public static async Task ImportImages(string inpath, string outpath, bool delSrc = false, bool showLog = true) + public static async Task ImportImages(string inpath, string outpath, Size size, bool delSrc = false, bool showLog = true) { if (showLog) Logger.Log("Importing images..."); Logger.Log($"Importing images from {inpath} to {outpath}."); @@ -71,19 +71,14 @@ namespace Flowframes concatFileContent += $"file '{img.Replace(@"\", "/")}'\n"; File.WriteAllText(concatFile, concatFileContent); - string args = $" -loglevel panic -f concat -safe 0 -i {concatFile.Wrap()} {pngComprArg} -pix_fmt rgb24 -vsync 0 -vf {divisionFilter} \"{outpath}/%{Padding.inputFrames}d.png\""; + string sizeStr = (size.Width > 1 && size.Height > 1) ? $"-s {size.Width}x{size.Height}" : ""; + string args = $" -loglevel panic -f concat -safe 0 -i {concatFile.Wrap()} {pngComprArg} {sizeStr} -pix_fmt rgb24 -vsync 0 -vf {divisionFilter} \"{outpath}/%{Padding.inputFrames}d.png\""; AvProcess.LogMode logMode = IOUtils.GetAmountOfFiles(inpath, false) > 50 ? AvProcess.LogMode.OnlyLastLine : AvProcess.LogMode.Hidden; await AvProcess.RunFfmpeg(args, logMode, AvProcess.TaskType.ExtractFrames); if (delSrc) DeleteSource(inpath); } - public static async Task ExtractSingleFrame(string inputFile, int frameNum) - { - string outPath = $"{inputFile}-frame{frameNum}.png"; - await ExtractSingleFrame(inputFile, outPath, frameNum); - } - public static async Task ExtractSingleFrame(string inputFile, string outputPath, int frameNum) { bool isPng = (Path.GetExtension(outputPath).ToLower() == ".png"); diff --git a/Code/Data/InterpSettings.cs b/Code/Data/InterpSettings.cs index b07d9b6..0292157 100644 --- a/Code/Data/InterpSettings.cs +++ b/Code/Data/InterpSettings.cs @@ -2,10 +2,12 @@ using Flowframes.Data; using Flowframes.IO; using Flowframes.Main; +using Flowframes.UI; using System; using System.Collections.Generic; using System.Drawing; using System.IO; +using System.Threading.Tasks; namespace Flowframes { @@ -72,23 +74,23 @@ namespace Flowframes outFilename = Path.Combine(outPath, Path.GetFileNameWithoutExtension(inPath) + IOUtils.GetExportSuffix(interpFactor, ai, model) + FFmpegUtils.GetExt(outMode)); } - public Size GetInputRes () + public async Task GetInputRes() { - RefreshResolutions(); + await RefreshResolutions(); return inputResolution; } - public Size GetScaledRes() + public async Task GetScaledRes() { - RefreshResolutions(); + await RefreshResolutions(); return scaledResolution; } - void RefreshResolutions () + async Task RefreshResolutions () { if (inputResolution.IsEmpty || scaledResolution.IsEmpty) { - inputResolution = IOUtils.GetVideoRes(inPath); + inputResolution = await IOUtils.GetVideoOrFramesRes(inPath); scaledResolution = InterpolateUtils.GetOutputResolution(inputResolution, false); } } diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 2b279d6..11939da 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -2,6 +2,7 @@ using Flowframes.Data; using Flowframes.Main; using Flowframes.MiscUtils; +using Flowframes.UI; using Force.Crc32; using Microsoft.WindowsAPICodePack.Shell; using Standart.Hash.xxHash; @@ -338,6 +339,21 @@ namespace Flowframes.IO return fps; } + public static async Task GetVideoOrFramesRes (string path) + { + Size res = new Size(); + if (!IsPathDirectory(path)) // If path is video + { + res = GetVideoRes(path); + } + else // Path is frame folder + { + Image thumb = await MainUiFunctions.GetThumbnail(path); + res = new Size(thumb.Width, thumb.Height); + } + return res; + } + public static Size GetVideoRes (string path) { Size size = new Size(0, 0); diff --git a/Code/Magick/Dedupe.cs b/Code/Magick/Dedupe.cs index b1ab3b6..7958c51 100644 --- a/Code/Magick/Dedupe.cs +++ b/Code/Magick/Dedupe.cs @@ -64,7 +64,7 @@ namespace Flowframes.Magick FileInfo[] framePaths = IOUtils.GetFileInfosSorted(path, false, "*." + ext); List framesToDelete = new List(); - int bufferSize = GetBufferSize(); + int bufferSize = await GetBufferSize(); int currentOutFrame = 1; int currentDupeCount = 0; @@ -189,9 +189,9 @@ namespace Flowframes.Magick return errPercent; } - static int GetBufferSize () + static async Task GetBufferSize () { - Size res = Interpolate.current.GetScaledRes(); + Size res = await Interpolate.current.GetScaledRes(); long pixels = res.Width * res.Height; // 4K = 8294400, 1440p = 3686400, 1080p = 2073600, 720p = 921600, 540p = 518400, 360p = 230400 int bufferSize = 100; if (pixels < 518400) bufferSize = 2200; diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index e7e7d25..093dc87 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -48,7 +48,7 @@ namespace Flowframes if (!current.inputIsFrames) // Input is video - extract frames first await ExtractFrames(current.inPath, current.framesFolder); else - await FFmpegCommands.ImportImages(current.inPath, current.framesFolder); + await FFmpegCommands.ImportImages(current.inPath, current.framesFolder, await Utils.GetOutputResolution(current.inPath, true)); if (canceled) return; sw.Restart(); await Task.Delay(10); @@ -79,7 +79,7 @@ namespace Flowframes Program.mainForm.SetStatus("Extracting frames from video..."); bool mpdecimate = Config.GetInt("dedupMode") == 2; - await FFmpegCommands.VideoToFrames(inPath, outPath, current.inFps, mpdecimate, false, Utils.GetOutputResolution(inPath, true), false); + await FFmpegCommands.VideoToFrames(inPath, outPath, current.inFps, mpdecimate, false, await Utils.GetOutputResolution(inPath, true), false); if (mpdecimate) { diff --git a/Code/Main/InterpolateSteps.cs b/Code/Main/InterpolateSteps.cs index b41aaf6..346adc5 100644 --- a/Code/Main/InterpolateSteps.cs +++ b/Code/Main/InterpolateSteps.cs @@ -45,7 +45,7 @@ namespace Flowframes.Main if (!current.inputIsFrames) // Input is video - extract frames first await ExtractVideoFrames(); else - await FFmpegCommands.ImportImages(current.inPath, current.framesFolder); + await FFmpegCommands.ImportImages(current.inPath, current.framesFolder, await InterpolateUtils.GetOutputResolution(current.inPath, true)); } if (step.Contains("Run Interpolation")) diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 9585e05..68c0b8c 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -302,9 +302,9 @@ namespace Flowframes.Main Logger.Log("Message: " + msg, true); } - public static Size GetOutputResolution (string inputPath, bool print) + public static async Task GetOutputResolution (string inputPath, bool print) { - Size resolution = IOUtils.GetVideoRes(inputPath); + Size resolution = await IOUtils.GetVideoOrFramesRes(inputPath); return GetOutputResolution(resolution, print); } @@ -365,9 +365,9 @@ namespace Flowframes.Main return true; } - public static bool UseUHD () + public static async Task UseUHD () { - return GetOutputResolution(i.current.inPath, false).Height >= Config.GetInt("uhdThresh"); + return (await GetOutputResolution(i.current.inPath, false)).Height >= Config.GetInt("uhdThresh"); } public static void FixConsecutiveSceneFrames (string sceneFramesPath, string sourceFramesPath) diff --git a/Code/OS/AiProcess.cs b/Code/OS/AiProcess.cs index 6e07307..b393e72 100644 --- a/Code/OS/AiProcess.cs +++ b/Code/OS/AiProcess.cs @@ -74,7 +74,7 @@ namespace Flowframes string rifeDir = Path.Combine(Paths.GetPkgPath(), Path.GetFileNameWithoutExtension(Packages.rifeCuda.fileName)); string script = "inference_video.py"; - string uhdStr = InterpolateUtils.UseUHD() ? "--UHD" : ""; + string uhdStr = await InterpolateUtils.UseUHD() ? "--UHD" : ""; string args = $" --input {framesPath.Wrap()} --model {mdl} --exp {(int)Math.Log(interpFactor, 2)} {uhdStr} --imgformat {InterpolateUtils.GetOutExt()} --output {Paths.interpDir}"; if (!File.Exists(Path.Combine(rifeDir, script))) @@ -87,7 +87,7 @@ namespace Flowframes AiStarted(rifePy, 3500, Interpolate.current.interpFactor); rifePy.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeCuda).Wrap()} & " + $"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Python.GetPyCmd()} {script} {args}"; - Logger.Log($"Running RIFE {(InterpolateUtils.UseUHD() ? "(UHD Mode)" : "")} ({script})...".TrimWhitespaces(), false); + Logger.Log($"Running RIFE {(await InterpolateUtils.UseUHD() ? "(UHD Mode)" : "")} ({script})...".TrimWhitespaces(), false); Logger.Log("cmd.exe " + rifePy.StartInfo.Arguments, true); if (!OSUtils.ShowHiddenCmd()) { @@ -107,7 +107,7 @@ namespace Flowframes public static async Task RunRifeNcnnMulti(string framesPath, string outPath, int factor, string mdl) { processTimeMulti.Restart(); - Logger.Log($"Running RIFE{(InterpolateUtils.UseUHD() ? " (UHD Mode)" : "")}...", false); + Logger.Log($"Running RIFE{(await InterpolateUtils.UseUHD() ? " (UHD Mode)" : "")}...", false); bool useAutoEnc = Interpolate.currentlyUsingAutoEnc; if(factor > 2) @@ -158,7 +158,7 @@ namespace Flowframes Process rifeNcnn = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd()); AiStarted(rifeNcnn, 1500, 2, inPath); - string uhdStr = InterpolateUtils.UseUHD() ? "-u" : ""; + string uhdStr = await InterpolateUtils.UseUHD() ? "-u" : ""; rifeNcnn.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeNcnn).Wrap()} & rife-ncnn-vulkan.exe " + $" -v -i {inPath.Wrap()} -o {outPath.Wrap()} -m {mdl.ToLower()} {uhdStr} -g {Config.Get("ncnnGpus")} -f {InterpolateUtils.GetOutExt()} -j {GetNcnnThreads()}"; diff --git a/Code/UI/MainUiFunctions.cs b/Code/UI/MainUiFunctions.cs index d6675e2..64f9c78 100644 --- a/Code/UI/MainUiFunctions.cs +++ b/Code/UI/MainUiFunctions.cs @@ -84,27 +84,17 @@ namespace Flowframes.UI static async Task PrintResolution (string path) { Size res = new Size(); + if(path == Interpolate.current.inPath) - { - res = Interpolate.current.GetInputRes(); - } + res = await Interpolate.current.GetInputRes(); else - { - if (!IOUtils.IsPathDirectory(path)) // If path is video - { - res = FFmpegCommands.GetSize(path); - } - else // Path is frame folder - { - Image thumb = await GetThumbnail(path); - res = new Size(thumb.Width, thumb.Height); - } - } + res = await IOUtils.GetVideoOrFramesRes(path); + if (res.Width > 1 && res.Height > 1) Logger.Log($"Input Resolution: {res.Width}x{res.Height}"); } - static async Task GetThumbnail (string path) + public static async Task GetThumbnail (string path) { string imgOnDisk = Path.Combine(Paths.GetDataPath(), "thumb-temp.jpg"); try