Move CLI stuff into own class, --console option

This commit is contained in:
N00MKRAD
2024-08-25 20:45:50 +02:00
parent 4ea676c405
commit 26f14876a7
5 changed files with 180 additions and 169 deletions

143
CodeLegacy/Cli.cs Normal file
View File

@@ -0,0 +1,143 @@
using Flowframes.Data;
using Flowframes.Extensions;
using Flowframes.IO;
using Flowframes.MiscUtils;
using Flowframes.Os;
using NDesk.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace Flowframes
{
public class Cli
{
public static bool ShowConsole = false;
public static bool DisablePython = false;
public static bool ShowMdlDownloader = false;
public static bool DontSaveConfig = false;
public static bool ExitWhenDone = false;
public static bool InterpStart = false;
public static float InterpFactor = -1f;
public static Implementations.Ai InterpAi = (Implementations.Ai)(-1);
public static Enums.Output.Format OutputFormat = Enums.Output.Format.Mp4;
public static Enums.Encoding.Encoder Encoder = (Enums.Encoding.Encoder)(-1);
public static Enums.Encoding.PixelFormat PixFmt = (Enums.Encoding.PixelFormat)(-1);
public static string InterpModel = "";
public static string OutputDir = "";
public static int MaxHeight = -1;
public static bool? Loop = false;
public static bool? FixSceneChanges = false;
public static float FixSceneChangeVal = -1f;
public static float MaxOutFps = -1f;
public static List<string> ValidFiles = new List<string>();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int FreeConsole();
public static void HandleCli()
{
string GetEnums<T>() => string.Join(", ", Enum.GetNames(typeof(T)));
var opts = new OptionSet
{
{
"c|console", "Show console",
v => ShowConsole = v != null
},
{
"np|no_python", "Disable Python implementations",
v => DisablePython = v != null
},
{
"md|open_model_downloader", "Open model downloader GUI on startup",
v => ShowMdlDownloader = v != null
},
{
"nc|no_config_save", "Do not save anything in config during this session",
v => DontSaveConfig = v != null
},
{
"e|exit", "Exit automatically after interpolation has finished",
v => ExitWhenDone = v != null
},
{
"s|start", "Start interpolation automatically if valid parameters are provided",
v => InterpStart = v != null
},
{
"f|factor=", "Interpolation factor",
v => InterpFactor = v.GetFloat()
},
{
"a|ai=", $"Interpolation AI implementation to use (Option: {GetEnums<Implementations.Ai>()})",
v => InterpAi = ParseUtils.GetEnum<Implementations.Ai>(v.Trim().Replace("_", ""))
},
{
"m|model=", "AI model to use",
v => InterpModel = v.Trim()
},
{
"vf|video_format=", $"Output video format to use (Options: {GetEnums<Enums.Output.Format>()})",
v => OutputFormat = ParseUtils.GetEnum<Enums.Output.Format>(v.Trim())
},
{
"ve|video_encoder=", $"Output video encoder to use (Options: {GetEnums<Enums.Encoding.Encoder>()})",
v => Encoder = ParseUtils.GetEnum<Enums.Encoding.Encoder>(v.Trim())
},
{
"pf|pixel_format=", $"Output pixel format to use (Options: {GetEnums<Enums.Encoding.PixelFormat>()})",
v => PixFmt = ParseUtils.GetEnum<Enums.Encoding.PixelFormat>(v.Trim())
},
{
"h|max_height=", $"Max video size (pixels height). Larger videos will be downscaled.",
v => MaxHeight = v.GetInt()
},
{
"l|loop", $"Enable loop output mode",
v => Loop = v != null ? true : (bool?)null
},
{
"scn|fix_scene_changes", $"Do not interpolate scene cuts to avoid artifacts",
v => FixSceneChanges = v != null ? true : (bool?)null
},
{
"scnv|scene_change_sensitivity=", $"Scene change sensitivity, lower is more sensitive (e.g. 0.18)",
v => FixSceneChangeVal = v.GetFloat()
},
{
"fps|max_fps=", $"Maximum FPS of output video, if the interpolation factor results in a higher FPS, it will be reduced to this value",
v => MaxOutFps = v.GetFloat()
},
{
"o|output_dir=", "Output folder to save the interpolated video in",
v => OutputDir = v.Trim()
},
{
"<>", "Input file(s)",
ValidFiles.Add
},
};
try
{
if (!opts.TryParseOptions(Environment.GetCommandLineArgs()))
return;
if (!ShowConsole)
FreeConsole();
ValidFiles = ValidFiles.Where(f => File.Exists(f) && Path.GetExtension(f).Lower() != ".exe").Distinct().ToList();
Python.DisablePython = DisablePython;
Config.NoWrite = DontSaveConfig;
}
catch (OptionException e)
{
Logger.Log($"Error parsing CLI options: {e.Message}", true);
}
}
}
}

View File

@@ -4,7 +4,7 @@ using System.Linq;
namespace Flowframes.Data namespace Flowframes.Data
{ {
class Implementations public class Implementations
{ {
public enum Ai public enum Ai
{ {

View File

@@ -6,7 +6,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{389E42DD-A163-49D7-9E0A-AE41090A07B3}</ProjectGuid> <ProjectGuid>{389E42DD-A163-49D7-9E0A-AE41090A07B3}</ProjectGuid>
<OutputType>WinExe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>Flowframes</RootNamespace> <RootNamespace>Flowframes</RootNamespace>
<AssemblyName>Flowframes</AssemblyName> <AssemblyName>Flowframes</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
@@ -341,6 +341,7 @@
<Reference Include="System.Management.Automation" /> <Reference Include="System.Management.Automation" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Cli.cs" />
<Compile Include="Data\AiInfo.cs" /> <Compile Include="Data\AiInfo.cs" />
<Compile Include="Data\AudioTrack.cs" /> <Compile Include="Data\AudioTrack.cs" />
<Compile Include="Data\EncoderInfo.cs" /> <Compile Include="Data\EncoderInfo.cs" />

View File

@@ -175,77 +175,77 @@ namespace Flowframes.Forms.Main
void HandleArgs() void HandleArgs()
{ {
if (Program.Cli.ValidFiles.Any()) if (Cli.ValidFiles.Any())
DragDropHandler(Program.Cli.ValidFiles.ToArray()); DragDropHandler(Cli.ValidFiles.ToArray());
if (Program.Cli.InterpAi != (Implementations.Ai)(-1)) if (Cli.InterpAi != (Implementations.Ai)(-1))
{ {
string name = Implementations.GetAi(Program.Cli.InterpAi).NameInternal; string name = Implementations.GetAi(Cli.InterpAi).NameInternal;
aiCombox.SelectedIndex = Implementations.NetworksAvailable.IndexOf(Implementations.NetworksAvailable.Where(ai => ai.NameInternal == name).FirstOrDefault()); aiCombox.SelectedIndex = Implementations.NetworksAvailable.IndexOf(Implementations.NetworksAvailable.Where(ai => ai.NameInternal == name).FirstOrDefault());
} }
if (Program.Cli.InterpFactor > 0) if (Cli.InterpFactor > 0)
{ {
interpFactorCombox.Text = Program.Cli.InterpFactor.ToString(); interpFactorCombox.Text = Cli.InterpFactor.ToString();
ValidateFactor(); ValidateFactor();
} }
if(Program.Cli.OutputFormat != (Enums.Output.Format)(-1)) if(Cli.OutputFormat != (Enums.Output.Format)(-1))
{ {
SetFormat(Program.Cli.OutputFormat); SetFormat(Cli.OutputFormat);
} }
if (Program.Cli.InterpModel.IsNotEmpty()) if (Cli.InterpModel.IsNotEmpty())
{ {
aiModel.SelectedIndex = aiModel.Items.Cast<string>().Select((item, index) => new { item, index }).FirstOrDefault(x => x.item.Contains(Program.Cli.InterpModel))?.index ?? -1; aiModel.SelectedIndex = aiModel.Items.Cast<string>().Select((item, index) => new { item, index }).FirstOrDefault(x => x.item.Contains(Cli.InterpModel))?.index ?? -1;
} }
if (Program.Cli.OutputDir.IsNotEmpty()) if (Cli.OutputDir.IsNotEmpty())
{ {
outputTbox.Text = Program.Cli.OutputDir; outputTbox.Text = Cli.OutputDir;
Directory.CreateDirectory(Program.Cli.OutputDir); Directory.CreateDirectory(Cli.OutputDir);
} }
if (Program.Cli.InterpModel.IsNotEmpty()) if (Cli.InterpModel.IsNotEmpty())
{ {
aiModel.SelectedIndex = aiModel.Items.Cast<string>().Select((item, index) => new { item, index }).FirstOrDefault(x => x.item.Contains(Program.Cli.InterpModel))?.index ?? -1; aiModel.SelectedIndex = aiModel.Items.Cast<string>().Select((item, index) => new { item, index }).FirstOrDefault(x => x.item.Contains(Cli.InterpModel))?.index ?? -1;
} }
if (Program.Cli.Encoder != (Enums.Encoding.Encoder)(-1)) if (Cli.Encoder != (Enums.Encoding.Encoder)(-1))
{ {
comboxOutputEncoder.SelectedIndex = comboxOutputEncoder.Items.Cast<string>().Select((item, index) => new { item, index }) comboxOutputEncoder.SelectedIndex = comboxOutputEncoder.Items.Cast<string>().Select((item, index) => new { item, index })
.FirstOrDefault(x => x.item == Strings.Encoder[Program.Cli.Encoder.ToString()])?.index ?? -1; .FirstOrDefault(x => x.item == Strings.Encoder[Cli.Encoder.ToString()])?.index ?? -1;
} }
if (Program.Cli.PixFmt != (Enums.Encoding.PixelFormat)(-1)) if (Cli.PixFmt != (Enums.Encoding.PixelFormat)(-1))
{ {
comboxOutputEncoder.SelectedIndex = comboxOutputEncoder.Items.Cast<string>().Select((item, index) => new { item, index }) comboxOutputEncoder.SelectedIndex = comboxOutputEncoder.Items.Cast<string>().Select((item, index) => new { item, index })
.FirstOrDefault(x => x.item == Strings.PixelFormat[Program.Cli.PixFmt.ToString()])?.index ?? -1; .FirstOrDefault(x => x.item == Strings.PixelFormat[Cli.PixFmt.ToString()])?.index ?? -1;
} }
if (Program.Cli.MaxHeight >= 64 && Program.Cli.MaxHeight <= 16384) if (Cli.MaxHeight >= 64 && Cli.MaxHeight <= 16384)
{ {
Config.Set(Config.Key.maxVidHeight, Program.Cli.MaxHeight.ToString()); Config.Set(Config.Key.maxVidHeight, Cli.MaxHeight.ToString());
} }
if (Program.Cli.Loop != null) if (Cli.Loop != null)
{ {
Config.Set(Config.Key.enableLoop, ((bool)Program.Cli.Loop).ToString()); Config.Set(Config.Key.enableLoop, ((bool)Cli.Loop).ToString());
} }
if (Program.Cli.FixSceneChanges != null) if (Cli.FixSceneChanges != null)
{ {
Config.Set(Config.Key.scnDetect, ((bool)Program.Cli.Loop).ToString()); Config.Set(Config.Key.scnDetect, ((bool)Cli.Loop).ToString());
} }
if (Program.Cli.FixSceneChangeVal > 0f) if (Cli.FixSceneChangeVal > 0f)
{ {
Config.Set(Config.Key.scnDetectValue, Program.Cli.FixSceneChangeVal.ToString()); Config.Set(Config.Key.scnDetectValue, Cli.FixSceneChangeVal.ToString());
} }
if (Program.Cli.MaxOutFps >= 1f) if (Cli.MaxOutFps >= 1f)
{ {
Config.Set(Config.Key.maxFps, Program.Cli.MaxOutFps.ToString()); Config.Set(Config.Key.maxFps, Cli.MaxOutFps.ToString());
} }
} }
@@ -373,7 +373,7 @@ namespace Flowframes.Forms.Main
public void CompletionAction() public void CompletionAction()
{ {
if (Program.Cli.ExitWhenDone) if (Cli.ExitWhenDone)
Application.Exit(); Application.Exit();
if (completionAction.SelectedIndex == 1) if (completionAction.SelectedIndex == 1)
@@ -648,7 +648,7 @@ namespace Flowframes.Forms.Main
{ {
if (Program.busy) return; if (Program.busy) return;
bool start = Program.initialRun && Program.Cli.InterpStart; bool start = Program.initialRun && Cli.InterpStart;
if (files.Length > 1) if (files.Length > 1)
{ {

View File

@@ -1,11 +1,7 @@
using Flowframes.Data; using Flowframes.Forms.Main;
using Flowframes.Extensions;
using Flowframes.Forms.Main;
using Flowframes.IO; using Flowframes.IO;
using Flowframes.MiscUtils;
using Flowframes.Os; using Flowframes.Os;
using Flowframes.Ui; using Flowframes.Ui;
using NDesk.Options;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@@ -33,28 +29,6 @@ namespace Flowframes
public static Queue<InterpSettings> batchQueue = new Queue<InterpSettings>(); public static Queue<InterpSettings> batchQueue = new Queue<InterpSettings>();
public static class Cli
{
public static bool DisablePython = false;
public static bool ShowMdlDownloader = false;
public static bool DontSaveConfig = false;
public static bool ExitWhenDone = false;
public static bool InterpStart = false;
public static float InterpFactor = -1f;
public static Implementations.Ai InterpAi = (Implementations.Ai)(-1);
public static Enums.Output.Format OutputFormat = Enums.Output.Format.Mp4;
public static Enums.Encoding.Encoder Encoder = (Enums.Encoding.Encoder)(-1);
public static Enums.Encoding.PixelFormat PixFmt = (Enums.Encoding.PixelFormat)(-1);
public static string InterpModel = "";
public static string OutputDir = "";
public static int MaxHeight = -1;
public static bool? Loop = false;
public static bool? FixSceneChanges = false;
public static float FixSceneChangeVal = -1f;
public static float MaxOutFps = -1f;
public static List<string> ValidFiles = new List<string>();
}
[STAThread] [STAThread]
static void Main() static void Main()
{ {
@@ -75,123 +49,16 @@ namespace Flowframes
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Paths.Init(); Paths.Init();
Cli.HandleCli();
Config.Init(); Config.Init();
Task.Run(() => DiskSpaceCheckLoop()); Task.Run(() => DiskSpaceCheckLoop());
args = Environment.GetCommandLineArgs().Where(a => a[0] == '-').Select(x => x.Trim().Substring(1).ToLowerInvariant()).ToArray(); args = Environment.GetCommandLineArgs().Where(a => a[0] == '-').Select(x => x.Trim().Substring(1).ToLowerInvariant()).ToArray();
Logger.Log($"Command Line: {Environment.CommandLine}", true); Logger.Log($"Command Line: {Environment.CommandLine}", true);
HandleCli();
LaunchGui(); LaunchGui();
} }
private static void HandleCli ()
{
string GetEnums<T>() => string.Join(", ", Enum.GetNames(typeof(T)));
var opts = new OptionSet
{
{
"np|no_python", "Disable Python implementations",
v => Cli.DisablePython = v != null
},
{
"md|open_model_downloader", "Open model downloader GUI on startup",
v => Cli.ShowMdlDownloader = v != null
},
{
"nc|no_config_save", "Do not save anything in config during this session",
v => Cli.DontSaveConfig = v != null
},
{
"e|exit", "Exit automatically after interpolation has finished",
v => Cli.ExitWhenDone = v != null
},
{
"s|start", "Start interpolation automatically if valid parameters are provided",
v => Cli.InterpStart = v != null
},
{
"f|factor=", "Interpolation factor",
v => Cli.InterpFactor = v.GetFloat()
},
{
"a|ai=", $"Interpolation AI implementation to use (Option: {GetEnums<Implementations.Ai>()})",
v => Cli.InterpAi = ParseUtils.GetEnum<Implementations.Ai>(v.Trim().Replace("_", ""))
},
{
"m|model=", "AI model to use",
v => Cli.InterpModel = v.Trim()
},
{
"vf|video_format=", $"Output video format to use (Options: {GetEnums<Enums.Output.Format>()})",
v => Cli.OutputFormat = ParseUtils.GetEnum<Enums.Output.Format>(v.Trim())
},
{
"ve|video_encoder=", $"Output video encoder to use (Options: {GetEnums<Enums.Encoding.Encoder>()})",
v => Cli.Encoder = ParseUtils.GetEnum<Enums.Encoding.Encoder>(v.Trim())
},
{
"pf|pixel_format=", $"Output pixel format to use (Options: {GetEnums<Enums.Encoding.PixelFormat>()})",
v => Cli.PixFmt = ParseUtils.GetEnum<Enums.Encoding.PixelFormat>(v.Trim())
},
{
"h|max_height=", $"Max video size (pixels height). Larger videos will be downscaled.",
v => Cli.MaxHeight = v.GetInt()
},
{
"l|loop", $"Enable loop output mode",
v => Cli.Loop = v != null ? true : (bool?)null
},
{
"scn|fix_scene_changes", $"Do not interpolate scene cuts to avoid artifacts",
v => Cli.FixSceneChanges = v != null ? true : (bool?)null
},
{
"scnv|scene_change_sensitivity=", $"Scene change sensitivity, lower is more sensitive (e.g. 0.18)",
v => Cli.FixSceneChangeVal = v.GetFloat()
},
{
"fps|max_fps=", $"Maximum FPS of output video, if the interpolation factor results in a higher FPS, it will be reduced to this value",
v => Cli.MaxOutFps = v.GetFloat()
},
{
"o|output_dir=", "Output folder to save the interpolated video in",
v => Cli.OutputDir = v.Trim()
},
{
"<>", "Input file(s)",
Cli.ValidFiles.Add
},
};
try
{
opts.Parse(Environment.GetCommandLineArgs());
}
catch (OptionException e)
{
Logger.Log($"Error parsing CLI option: {e.Message}", true);
}
try
{
if (!opts.TryParseOptions(Environment.GetCommandLineArgs()))
return;
Cli.ValidFiles = Cli.ValidFiles.Where(f => File.Exists(f) && Path.GetExtension(f).Lower() != ".exe").Distinct().ToList();
Logger.Log($"Parsed CLI: Start {Cli.InterpStart}, Exit {Cli.ExitWhenDone}, Factor {Cli.InterpFactor}, AI {Cli.InterpAi}, Model '{Cli.InterpModel}', " +
$"Video Format {Cli.OutputFormat}, Output Dir '{Cli.OutputDir}', No Python {Python.DisablePython}, MdlDl {Cli.ShowMdlDownloader}", true);
Python.DisablePython = Cli.DisablePython;
Config.NoWrite = Cli.DontSaveConfig;
}
catch (OptionException e)
{
Logger.Log($"Error parsing CLI options: {e.Message}", true);
}
}
private static void LaunchGui() private static void LaunchGui()
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
@@ -199,7 +66,7 @@ namespace Flowframes
bool showMdlDownloader = Cli.ShowMdlDownloader || args.Contains("show-model-downloader"); // The latter check may be needed for legacy reasons bool showMdlDownloader = Cli.ShowMdlDownloader || args.Contains("show-model-downloader"); // The latter check may be needed for legacy reasons
mainForm = new Form1() { ShowModelDownloader = showMdlDownloader }; mainForm = new Form1() { ShowModelDownloader = showMdlDownloader, Enabled = false };
Application.Run(mainForm); Application.Run(mainForm);
} }