Skip to content

Commit

Permalink
Move CLI stuff into own class, --console option
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Aug 25, 2024
1 parent 4ea676c commit 26f1487
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 169 deletions.
143 changes: 143 additions & 0 deletions CodeLegacy/Cli.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
2 changes: 1 addition & 1 deletion CodeLegacy/Data/Implementations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Flowframes.Data
{
class Implementations
public class Implementations
{
public enum Ai
{
Expand Down
3 changes: 2 additions & 1 deletion CodeLegacy/Flowframes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{389E42DD-A163-49D7-9E0A-AE41090A07B3}</ProjectGuid>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>Flowframes</RootNamespace>
<AssemblyName>Flowframes</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
Expand Down Expand Up @@ -341,6 +341,7 @@
<Reference Include="System.Management.Automation" />
</ItemGroup>
<ItemGroup>
<Compile Include="Cli.cs" />
<Compile Include="Data\AiInfo.cs" />
<Compile Include="Data\AudioTrack.cs" />
<Compile Include="Data\EncoderInfo.cs" />
Expand Down
62 changes: 31 additions & 31 deletions CodeLegacy/Forms/Main/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,77 +175,77 @@ async Task Checks()
void HandleArgs()
{

if (Program.Cli.ValidFiles.Any())
DragDropHandler(Program.Cli.ValidFiles.ToArray());
if (Cli.ValidFiles.Any())
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());
}

if (Program.Cli.InterpFactor > 0)
if (Cli.InterpFactor > 0)
{
interpFactorCombox.Text = Program.Cli.InterpFactor.ToString();
interpFactorCombox.Text = Cli.InterpFactor.ToString();
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;
Directory.CreateDirectory(Program.Cli.OutputDir);
outputTbox.Text = 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 })
.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 })
.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());
}
}

Expand Down Expand Up @@ -373,7 +373,7 @@ public void InterpolationDone()

public void CompletionAction()
{
if (Program.Cli.ExitWhenDone)
if (Cli.ExitWhenDone)
Application.Exit();

if (completionAction.SelectedIndex == 1)
Expand Down Expand Up @@ -648,7 +648,7 @@ public void DragDropHandler(string[] files)
{
if (Program.busy) return;

bool start = Program.initialRun && Program.Cli.InterpStart;
bool start = Program.initialRun && Cli.InterpStart;

if (files.Length > 1)
{
Expand Down
Loading

0 comments on commit 26f1487

Please sign in to comment.