2021-02-02 12:56:48 +01:00
|
|
|
|
using Flowframes.Media;
|
2020-12-23 16:13:04 +01:00
|
|
|
|
using Flowframes.Data;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
using Flowframes.Main;
|
2021-02-01 22:48:22 +01:00
|
|
|
|
using Flowframes.MiscUtils;
|
2021-01-15 15:07:40 +01:00
|
|
|
|
using Flowframes.UI;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-12-22 23:45:07 +01:00
|
|
|
|
using System.Drawing;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
using System.IO;
|
2021-01-26 20:46:05 +01:00
|
|
|
|
using System.Linq;
|
2021-01-15 15:07:40 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Flowframes
|
|
|
|
|
|
{
|
|
|
|
|
|
public struct InterpSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
public string inPath;
|
|
|
|
|
|
public string outPath;
|
|
|
|
|
|
public AI ai;
|
2021-04-02 14:36:08 +02:00
|
|
|
|
public Fraction inFps;
|
|
|
|
|
|
public Fraction outFps;
|
2021-02-01 18:05:50 +01:00
|
|
|
|
public float interpFactor;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
public Interpolate.OutMode outMode;
|
2021-01-13 23:05:23 +01:00
|
|
|
|
public string model;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
|
|
|
|
|
|
public string tempFolder;
|
|
|
|
|
|
public string framesFolder;
|
|
|
|
|
|
public string interpFolder;
|
|
|
|
|
|
public bool inputIsFrames;
|
2020-12-22 23:45:07 +01:00
|
|
|
|
public Size inputResolution;
|
|
|
|
|
|
public Size scaledResolution;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
|
2021-01-18 15:32:45 +01:00
|
|
|
|
public bool alpha;
|
2021-02-01 21:50:05 +01:00
|
|
|
|
public bool stepByStep;
|
2021-01-18 15:32:45 +01:00
|
|
|
|
|
2021-04-02 14:36:08 +02:00
|
|
|
|
public InterpSettings(string inPathArg, string outPathArg, AI aiArg, Fraction inFpsArg, int interpFactorArg, Interpolate.OutMode outModeArg, string modelArg)
|
2020-12-17 11:32:45 +01:00
|
|
|
|
{
|
|
|
|
|
|
inPath = inPathArg;
|
|
|
|
|
|
outPath = outPathArg;
|
|
|
|
|
|
ai = aiArg;
|
|
|
|
|
|
inFps = inFpsArg;
|
|
|
|
|
|
interpFactor = interpFactorArg;
|
|
|
|
|
|
outFps = inFpsArg * interpFactorArg;
|
|
|
|
|
|
outMode = outModeArg;
|
2021-01-13 23:05:23 +01:00
|
|
|
|
model = modelArg;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
|
2021-01-18 15:32:45 +01:00
|
|
|
|
alpha = false;
|
2021-02-01 21:50:05 +01:00
|
|
|
|
stepByStep = false;
|
2021-01-18 15:32:45 +01:00
|
|
|
|
|
2020-12-18 00:19:08 +01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2020-12-22 23:45:07 +01:00
|
|
|
|
|
|
|
|
|
|
inputResolution = new Size(0, 0);
|
|
|
|
|
|
scaledResolution = new Size(0, 0);
|
2020-12-18 00:19:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-01 18:05:50 +01:00
|
|
|
|
public InterpSettings (string serializedData)
|
|
|
|
|
|
{
|
2021-02-01 22:48:22 +01:00
|
|
|
|
inPath = "";
|
|
|
|
|
|
outPath = "";
|
|
|
|
|
|
ai = Networks.networks[0];
|
2021-04-02 14:36:08 +02:00
|
|
|
|
inFps = new Fraction();
|
2021-02-01 22:48:22 +01:00
|
|
|
|
interpFactor = 0;
|
2021-04-02 14:36:08 +02:00
|
|
|
|
outFps = new Fraction();
|
2021-02-01 22:48:22 +01:00
|
|
|
|
outMode = Interpolate.OutMode.VidMp4;
|
|
|
|
|
|
model = "";
|
|
|
|
|
|
alpha = false;
|
|
|
|
|
|
stepByStep = false;
|
|
|
|
|
|
inputResolution = new Size(0, 0);
|
|
|
|
|
|
scaledResolution = new Size(0, 0);
|
|
|
|
|
|
|
2021-02-01 18:05:50 +01:00
|
|
|
|
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 = Networks.GetAi(entry.Value); break;
|
2021-04-02 14:36:08 +02:00
|
|
|
|
case "INFPS": inFps = new Fraction(entry.Value); break;
|
|
|
|
|
|
case "OUTFPS": outFps = new Fraction(entry.Value); break;
|
2021-02-01 18:05:50 +01:00
|
|
|
|
case "INTERPFACTOR": interpFactor = float.Parse(entry.Value); break;
|
|
|
|
|
|
case "OUTMODE": outMode = (Interpolate.OutMode)Enum.Parse(typeof(Interpolate.OutMode), entry.Value); break;
|
|
|
|
|
|
case "MODEL": model = entry.Value; break;
|
2021-02-01 22:48:22 +01:00
|
|
|
|
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;
|
2021-02-01 18:05:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-01 22:48:22 +01:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2021-02-01 18:05:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-18 00:19:08 +01:00
|
|
|
|
public void UpdatePaths (string inPathArg, string outPathArg)
|
|
|
|
|
|
{
|
|
|
|
|
|
inPath = inPathArg;
|
|
|
|
|
|
outPath = outPathArg;
|
2020-12-17 11:32:45 +01:00
|
|
|
|
tempFolder = InterpolateUtils.GetTempFolderLoc(inPath, outPath);
|
|
|
|
|
|
framesFolder = Path.Combine(tempFolder, Paths.framesDir);
|
|
|
|
|
|
interpFolder = Path.Combine(tempFolder, Paths.interpDir);
|
|
|
|
|
|
inputIsFrames = IOUtils.IsPathDirectory(inPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-15 15:07:40 +01:00
|
|
|
|
public async Task<Size> GetInputRes()
|
2020-12-17 11:32:45 +01:00
|
|
|
|
{
|
2021-01-15 15:07:40 +01:00
|
|
|
|
await RefreshResolutions();
|
2020-12-22 23:45:07 +01:00
|
|
|
|
return inputResolution;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-15 15:07:40 +01:00
|
|
|
|
public async Task<Size> GetScaledRes()
|
2020-12-22 23:45:07 +01:00
|
|
|
|
{
|
2021-01-15 15:07:40 +01:00
|
|
|
|
await RefreshResolutions();
|
2020-12-22 23:45:07 +01:00
|
|
|
|
return scaledResolution;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-15 15:07:40 +01:00
|
|
|
|
async Task RefreshResolutions ()
|
2020-12-22 23:45:07 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (inputResolution.IsEmpty || scaledResolution.IsEmpty)
|
|
|
|
|
|
{
|
2021-01-15 15:07:40 +01:00
|
|
|
|
inputResolution = await IOUtils.GetVideoOrFramesRes(inPath);
|
2020-12-22 23:45:07 +01:00
|
|
|
|
scaledResolution = InterpolateUtils.GetOutputResolution(inputResolution, false);
|
|
|
|
|
|
}
|
2020-12-17 11:32:45 +01:00
|
|
|
|
}
|
2021-01-13 22:50:34 +01:00
|
|
|
|
|
2021-02-01 18:05:50 +01:00
|
|
|
|
public int GetTargetFrameCount(string overrideInputDir = "", float overrideFactor = -1)
|
2021-01-13 22:50:34 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (framesFolder == null || !Directory.Exists(framesFolder))
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
string framesDir = (!string.IsNullOrWhiteSpace(overrideInputDir)) ? overrideInputDir : framesFolder;
|
|
|
|
|
|
int frames = IOUtils.GetAmountOfFiles(framesDir, false, "*.png");
|
2021-02-01 18:05:50 +01:00
|
|
|
|
float factor = (overrideFactor > 0) ? overrideFactor : interpFactor;
|
|
|
|
|
|
int targetFrameCount = ((frames * factor) - (interpFactor - 1)).RoundToInt();
|
2021-01-13 22:50:34 +01:00
|
|
|
|
return targetFrameCount;
|
|
|
|
|
|
}
|
2021-01-26 20:46:05 +01:00
|
|
|
|
|
|
|
|
|
|
public void RefreshAlpha ()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-01-27 20:59:29 +01:00
|
|
|
|
bool alphaEnabled = Config.GetBool("enableAlpha", false);
|
|
|
|
|
|
bool outputSupportsAlpha = (outMode == Interpolate.OutMode.ImgPng || outMode == Interpolate.OutMode.VidGif);
|
2021-01-30 01:42:32 +01:00
|
|
|
|
string ext = inputIsFrames ? Path.GetExtension(IOUtils.GetFilesSorted(inPath).First()).ToLower() : Path.GetExtension(inPath).ToLower();
|
|
|
|
|
|
alpha = (alphaEnabled && outputSupportsAlpha && (ext == ".gif" || ext == ".png" || ext == ".apng"));
|
2021-01-26 20:46:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Log("RefreshAlpha Error: " + e.Message, true);
|
|
|
|
|
|
alpha = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-01 18:05:50 +01:00
|
|
|
|
|
|
|
|
|
|
public string Serialize ()
|
|
|
|
|
|
{
|
|
|
|
|
|
string s = $"INPATH|{inPath}\n";
|
|
|
|
|
|
s += $"OUTPATH|{outPath}\n";
|
|
|
|
|
|
s += $"AI|{ai.aiName}\n";
|
2021-04-02 14:36:08 +02:00
|
|
|
|
s += $"INFPS|{inFps}\n";
|
|
|
|
|
|
s += $"OUTFPS|{outFps}\n";
|
2021-02-01 18:05:50 +01:00
|
|
|
|
s += $"INTERPFACTOR|{interpFactor}\n";
|
|
|
|
|
|
s += $"OUTMODE|{outMode}\n";
|
|
|
|
|
|
s += $"MODEL|{model}\n";
|
2021-02-01 22:48:22 +01:00
|
|
|
|
s += $"INPUTRES|{inputResolution.Width}x{inputResolution.Height}\n";
|
|
|
|
|
|
s += $"OUTPUTRES|{scaledResolution.Width}x{scaledResolution.Height}\n";
|
2021-02-01 18:05:50 +01:00
|
|
|
|
s += $"ALPHA|{alpha}\n";
|
2021-02-01 22:48:22 +01:00
|
|
|
|
s += $"STEPBYSTEP|{stepByStep}\n";
|
2021-02-01 18:05:50 +01:00
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
}
|
2020-12-17 11:32:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|