1
mirror of https://github.com/n00mkrad/flowframes synced 2024-09-29 14:38:03 +02:00

Downscaling ("max video size") now also works for folder inputs

This commit is contained in:
N00MKRAD 2021-01-15 15:07:40 +01:00
parent 0810ee432a
commit 706859f6ea
9 changed files with 46 additions and 43 deletions

View File

@ -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");

View File

@ -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<Size> GetInputRes()
{
RefreshResolutions();
await RefreshResolutions();
return inputResolution;
}
public Size GetScaledRes()
public async Task<Size> 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);
}
}

View File

@ -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<Size> 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);

View File

@ -64,7 +64,7 @@ namespace Flowframes.Magick
FileInfo[] framePaths = IOUtils.GetFileInfosSorted(path, false, "*." + ext);
List<string> framesToDelete = new List<string>();
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<int> 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;

View File

@ -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)
{

View File

@ -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"))

View File

@ -302,9 +302,9 @@ namespace Flowframes.Main
Logger.Log("Message: " + msg, true);
}
public static Size GetOutputResolution (string inputPath, bool print)
public static async Task<Size> 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<bool> 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)

View File

@ -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()}";

View File

@ -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<Image> GetThumbnail (string path)
public static async Task<Image> GetThumbnail (string path)
{
string imgOnDisk = Path.Combine(Paths.GetDataPath(), "thumb-temp.jpg");
try