From c14cacc7d0c3e86e2178d982cb63102c1807b9ad Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Thu, 10 Dec 2020 16:37:33 +0100 Subject: [PATCH] Fixed JPEG support with new RIFE code and AutoEnc --- Code/Flowframes.csproj | 2 +- Code/Magick/MagickDedupe.cs | 2 +- Code/Main/AutoEncode.cs | 6 +++--- Code/Main/CreateVideo.cs | 6 +++--- Code/Main/FrameTiming.cs | 13 +++++++------ Code/Main/Interpolate.cs | 2 +- Code/Main/InterpolateSteps.cs | 2 +- Code/Main/InterpolateUtils.cs | 7 ++++++- Code/OS/AiProcess.cs | 22 ++++++++++------------ 9 files changed, 33 insertions(+), 29 deletions(-) diff --git a/Code/Flowframes.csproj b/Code/Flowframes.csproj index 4c5ba32..5623f06 100644 --- a/Code/Flowframes.csproj +++ b/Code/Flowframes.csproj @@ -234,7 +234,7 @@ - + diff --git a/Code/Magick/MagickDedupe.cs b/Code/Magick/MagickDedupe.cs index 3413e33..54a37bd 100644 --- a/Code/Magick/MagickDedupe.cs +++ b/Code/Magick/MagickDedupe.cs @@ -211,7 +211,7 @@ namespace Flowframes.Magick if (currentMode == Mode.None) return; - string ext = InterpolateUtils.lastExt; + string ext = InterpolateUtils.GetExt(); string dupeInfoFile = Path.Combine(Interpolate.currentTempDir, "dupes.ini"); if (!File.Exists(dupeInfoFile)) return; diff --git a/Code/Main/AutoEncode.cs b/Code/Main/AutoEncode.cs index fa495d8..2650136 100644 --- a/Code/Main/AutoEncode.cs +++ b/Code/Main/AutoEncode.cs @@ -48,8 +48,8 @@ namespace Flowframes.Main continue; } - IOUtils.ZeroPadDir(Directory.GetFiles(interpFramesFolder, $"*.{InterpolateUtils.lastExt}").ToList(), Padding.interpFrames, encodedFrames); - string[] interpFrames = Directory.GetFiles(interpFramesFolder, $"*.{InterpolateUtils.lastExt}"); + IOUtils.ZeroPadDir(Directory.GetFiles(interpFramesFolder, $"*.{InterpolateUtils.GetExt()}").ToList(), Padding.interpFrames, encodedFrames); + string[] interpFrames = Directory.GetFiles(interpFramesFolder, $"*.{InterpolateUtils.GetExt()}"); unencodedFrames = interpFrames.ToList().Except(encodedFrames).ToList(); Directory.CreateDirectory(videoChunksFolder); @@ -114,7 +114,7 @@ namespace Flowframes.Main static int GetInterpFramesAmount() { - return IOUtils.GetAmountOfFiles(interpFramesFolder, false, $"*.{InterpolateUtils.lastExt}"); + return IOUtils.GetAmountOfFiles(interpFramesFolder, false, $"*.{InterpolateUtils.GetExt()}"); } } } diff --git a/Code/Main/CreateVideo.cs b/Code/Main/CreateVideo.cs index 7df1ac4..6a8e904 100644 --- a/Code/Main/CreateVideo.cs +++ b/Code/Main/CreateVideo.cs @@ -38,9 +38,9 @@ namespace Flowframes.Main } //Logger.Log("zero-padding " + path); - //IOUtils.ZeroPadDir(path, $"*.{InterpolateUtils.lastExt}", Padding.interpFrames); + //IOUtils.ZeroPadDir(path, $"*.{InterpolateUtils.GetExt()}", Padding.interpFrames); - if (IOUtils.GetAmountOfFiles(path, false, $"*.{InterpolateUtils.lastExt}") <= 1) + if (IOUtils.GetAmountOfFiles(path, false, $"*.{InterpolateUtils.GetExt()}") <= 1) { i.Cancel("Output folder does not contain frames - An error must have occured during interpolation!", AiProcess.hasShownError); return; @@ -181,7 +181,7 @@ namespace Flowframes.Main //Logger.Log("minLength: " + minLength); int minFrameCount = (minLength * i.currentOutFps).RoundToInt(); //Logger.Log("minFrameCount: " + minFrameCount); - //int outFrames = new DirectoryInfo(framesOutPath).GetFiles($"*.{InterpolateUtils.lastExt}", SearchOption.TopDirectoryOnly).Length; + //int outFrames = new DirectoryInfo(framesOutPath).GetFiles($"*.{InterpolateUtils.GetExt()}", SearchOption.TopDirectoryOnly).Length; int outFrames = i.currentInputFrameCount * i.lastInterpFactor; //Logger.Log("outFrames: " + outFrames); if (outFrames / i.currentOutFps < minLength) diff --git a/Code/Main/FrameTiming.cs b/Code/Main/FrameTiming.cs index 6166f60..eddf8f1 100644 --- a/Code/Main/FrameTiming.cs +++ b/Code/Main/FrameTiming.cs @@ -30,8 +30,9 @@ namespace Flowframes.Main Logger.Log("Timestamps are disabled, using static frame rate."); bool sceneDetection = true; + string ext = InterpolateUtils.GetExt(); - FileInfo[] frameFiles = new DirectoryInfo(framesPath).GetFiles("*.png"); + FileInfo[] frameFiles = new DirectoryInfo(framesPath).GetFiles($"*.png"); string vfrFile = Path.Combine(framesPath.GetParentDir(), $"vfr-{interpFactor}x.ini"); string fileContent = ""; @@ -83,14 +84,14 @@ namespace Flowframes.Main int lastNum = totalFileCount; // Logger.Log($"Writing frame {totalFileCount} [Discarding Next]", true); - fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n"; + fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.{ext}'\nduration {durationStr}\n"; totalFileCount++; // Logger.Log("Discarding interp frames with out num " + totalFileCount); for (int dupeCount = 1; dupeCount < interpFramesAmount; dupeCount++) { // Logger.Log($"Writing frame {totalFileCount} which is actually repeated frame {lastNum}"); - fileContent += $"file '{interpPath}/{lastNum.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n"; + fileContent += $"file '{interpPath}/{lastNum.ToString().PadLeft(Padding.interpFrames, '0')}.{ext}'\nduration {durationStr}\n"; totalFileCount++; } @@ -99,7 +100,7 @@ namespace Flowframes.Main else { //Logger.Log($"Writing frame {totalFileCount}", true, false); - fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n"; + fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.{ext}'\nduration {durationStr}\n"; totalFileCount++; } } @@ -110,7 +111,7 @@ namespace Flowframes.Main //Logger.Log($"Writing last frame: {totalFileCount}", true, false); string durationStrLast = ((100f / interpFactor / 1000f) * 1).ToString("0.00000", CultureInfo.InvariantCulture); - fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStrLast}\n"; + fileContent += $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.{ext}'\nduration {durationStrLast}\n"; totalFileCount++; File.WriteAllText(vfrFile, fileContent); @@ -121,7 +122,7 @@ namespace Flowframes.Main { int lastFileNumber = frameFiles.Last().Name.GetInt(); lastFileNumber += lastFrameDuration; - string loopFrameTargetPath = Path.Combine(frameFiles.First().FullName.GetParentDir(), lastFileNumber.ToString().PadLeft(Padding.inputFrames, '0') + ".png"); + string loopFrameTargetPath = Path.Combine(frameFiles.First().FullName.GetParentDir(), lastFileNumber.ToString().PadLeft(Padding.inputFrames, '0') + ".{ext}"); if (File.Exists(loopFrameTargetPath)) return; File.Copy(frameFiles.First().FullName, loopFrameTargetPath); diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index 5decf84..a99dae8 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -213,7 +213,7 @@ namespace Flowframes if (firstProgUpd && Program.mainForm.IsInFocus()) Program.mainForm.SetTab("preview"); firstProgUpd = false; - string[] frames = Directory.GetFiles(outdir, $"*.{Utils.lastExt}"); + string[] frames = Directory.GetFiles(outdir, $"*.{Utils.GetExt()}"); if (frames.Length > 1) Utils.UpdateInterpProgress(frames.Length, target, frames[frames.Length - 1]); await Task.Delay(Utils.GetProgressWaitTime(frames.Length)); diff --git a/Code/Main/InterpolateSteps.cs b/Code/Main/InterpolateSteps.cs index 86d7f96..e18e871 100644 --- a/Code/Main/InterpolateSteps.cs +++ b/Code/Main/InterpolateSteps.cs @@ -173,7 +173,7 @@ namespace Flowframes.Main public static async Task CreateOutputVid() { - string[] outFrames = Directory.GetFiles(currentInterpFramesDir, $"*.{InterpolateUtils.lastExt}"); + string[] outFrames = Directory.GetFiles(currentInterpFramesDir, $"*.{InterpolateUtils.GetExt()}"); if (outFrames.Length > 0 && !IOUtils.CheckImageValid(outFrames[0])) { InterpolateUtils.ShowMessage("Invalid frame files detected!\n\nIf you used Auto-Encode, this is normal, and you don't need to run " + diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 5a98adc..1197c98 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -19,7 +19,12 @@ namespace Flowframes.Main public static PictureBox preview; public static BigPreviewForm bigPreviewForm; - public static string lastExt = "png"; + public static string GetExt () + { + if (Config.GetBool("jpegInterps")) + return "jpg"; + return "png"; + } public static void UpdateInterpProgress(int frames, int target, string latestFramePath = "") { diff --git a/Code/OS/AiProcess.cs b/Code/OS/AiProcess.cs index e8782c4..e01da1e 100644 --- a/Code/OS/AiProcess.cs +++ b/Code/OS/AiProcess.cs @@ -26,11 +26,9 @@ namespace Flowframes public static Dictionary filenameMap = new Dictionary(); // TODO: Store on disk instead for crashes? - static void AiStarted (Process proc, int startupTimeMs, string defaultExt = "png") + static void AiStarted (Process proc, int startupTimeMs) { lastStartupTimeMs = startupTimeMs; - InterpolateUtils.lastExt = defaultExt; - if (Config.GetBool("jpegInterps")) InterpolateUtils.lastExt = "jpg"; processTime.Restart(); currentAiProcess = proc; hasShownError = false; @@ -53,7 +51,7 @@ namespace Flowframes string dainDir = Path.Combine(Paths.GetPkgPath(), Path.GetFileNameWithoutExtension(Packages.dainNcnn.fileName)); Process dain = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd()); AiStarted(dain, 1500); - dain.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {dainDir.Wrap()} & dain-ncnn-vulkan.exe {args} -f {InterpolateUtils.lastExt} -j {GetNcnnThreads()}"; + dain.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {dainDir.Wrap()} & dain-ncnn-vulkan.exe {args} -f {InterpolateUtils.GetExt()} -j {GetNcnnThreads()}"; Logger.Log("Running DAIN...", false); Logger.Log("cmd.exe " + dain.StartInfo.Arguments, true); if (!OSUtils.ShowHiddenCmd()) @@ -73,7 +71,7 @@ namespace Flowframes if (Interpolate.canceled) return; if (!Interpolate.currentlyUsingAutoEnc) - IOUtils.ZeroPadDir(outPath, InterpolateUtils.lastExt, Padding.interpFrames); + IOUtils.ZeroPadDir(outPath, InterpolateUtils.GetExt(), Padding.interpFrames); AiFinished("DAIN"); } @@ -115,7 +113,7 @@ namespace Flowframes if (Interpolate.canceled) return; if (!Interpolate.currentlyUsingAutoEnc) - IOUtils.ZeroPadDir(outPath, InterpolateUtils.lastExt, Padding.interpFrames); + IOUtils.ZeroPadDir(outPath, InterpolateUtils.GetExt(), Padding.interpFrames); AiFinished("CAIN"); } @@ -126,7 +124,7 @@ namespace Flowframes string cainExe = "cain-ncnn-vulkan.exe"; Process cain = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd()); AiStarted(cain, 1500); - cain.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {cainDir.Wrap()} & {cainExe} {args} -f {InterpolateUtils.lastExt} -j {GetNcnnThreads()}"; + cain.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {cainDir.Wrap()} & {cainExe} {args} -f {InterpolateUtils.GetExt()} -j {GetNcnnThreads()}"; Logger.Log("cmd.exe " + cain.StartInfo.Arguments, true); if (!OSUtils.ShowHiddenCmd()) { @@ -159,9 +157,9 @@ namespace Flowframes } Process rifePy = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd()); - AiStarted(rifePy, 3500, "png"); + AiStarted(rifePy, 3500); rifePy.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeCuda).Wrap()} & " + - $"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Pytorch.GetPyCmd()} {script} {args} --imgformat {InterpolateUtils.lastExt} --output {Paths.interpDir}"; + $"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Pytorch.GetPyCmd()} {script} {args} --imgformat {InterpolateUtils.GetExt()} --output {Paths.interpDir}"; Logger.Log($"Running RIFE ({script})...", false); Logger.Log("cmd.exe " + rifePy.StartInfo.Arguments, true); if (!OSUtils.ShowHiddenCmd()) @@ -225,8 +223,8 @@ namespace Flowframes if (!Interpolate.currentlyUsingAutoEnc) { - Logger.Log($"zero padding {outPath} with ext \"{InterpolateUtils.lastExt}\" to length {Padding.interpFrames}"); - IOUtils.ZeroPadDir(outPath, InterpolateUtils.lastExt, Padding.interpFrames); + Logger.Log($"zero padding {outPath} with ext \"{InterpolateUtils.GetExt()}\" to length {Padding.interpFrames}"); + IOUtils.ZeroPadDir(outPath, InterpolateUtils.GetExt(), Padding.interpFrames); } AiFinished("RIFE"); @@ -236,7 +234,7 @@ namespace Flowframes { Process rifeNcnn = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd()); AiStarted(rifeNcnn, 1500); - rifeNcnn.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeNcnn).Wrap()} & rife-ncnn-vulkan.exe {args} -g {Config.Get("ncnnGpus")} -f {InterpolateUtils.lastExt} -j {GetNcnnThreads()}"; + rifeNcnn.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeNcnn).Wrap()} & rife-ncnn-vulkan.exe {args} -g {Config.Get("ncnnGpus")} -f {InterpolateUtils.GetExt()} -j {GetNcnnThreads()}"; Logger.Log("cmd.exe " + rifeNcnn.StartInfo.Arguments, true); if (!OSUtils.ShowHiddenCmd()) {