Skip to content

Commit

Permalink
Reduce log spam, minor refactor stuff, disable GUI when using autorun
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Aug 25, 2024
1 parent 5a3a84b commit 70841fe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 44 deletions.
13 changes: 4 additions & 9 deletions CodeLegacy/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public 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 bool AutoRun = false;
public static float InterpFactor = -1f;
public static Implementations.Ai InterpAi = (Implementations.Ai)(-1);
public static Enums.Output.Format OutputFormat = Enums.Output.Format.Mp4;
Expand Down Expand Up @@ -60,19 +59,15 @@ public static void HandleCli()
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
"a|autorun", "Start interpolation automatically if valid parameters are provided and exit afterwards",
v => AutoRun = v != null
},
{
"f|factor=", "Interpolation factor",
v => InterpFactor = v.GetFloat()
},
{
"a|ai=", $"Interpolation AI implementation to use (Option: {GetEnums<Implementations.Ai>()})",
"ai=", $"Interpolation AI implementation to use (Option: {GetEnums<Implementations.Ai>()})",
v => InterpAi = ParseUtils.GetEnum<Implementations.Ai>(v.Trim().Replace("_", ""))
},
{
Expand Down
4 changes: 2 additions & 2 deletions CodeLegacy/Forms/Main/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void InterpolationDone()

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

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

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

if (files.Length > 1)
{
Expand Down
19 changes: 6 additions & 13 deletions CodeLegacy/IO/IoUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,47 +368,40 @@ public static Fraction GetVideoFramerateForDir(string path)

public static async Task<Size> GetVideoOrFramesRes(string path)
{
Size res = new Size();

try
{
if (!IsPathDirectory(path)) // If path is video
{
res = GetVideoRes(path);
return GetVideoRes(path);
}
else // Path is frame folder
{
Image thumb = await MainUiFunctions.GetThumbnail(path);
res = new Size(thumb.Width, thumb.Height);
return new Size(thumb.Width, thumb.Height);
}
}
catch (Exception e)
{
Logger.Log("GetVideoOrFramesRes Error: " + e.Message);
Logger.Log($"GetVideoOrFramesRes Error: {e.Message}");
return new Size();
}

return res;
}

public static Size GetVideoRes(string path)
{
Size size = new Size(0, 0);

try
{
if (path.IsConcatFile())
path = ReadFileFirstLine(path).Split('\'')[1].Split('\'')[0];

size = FfmpegCommands.GetSize(path);
Logger.Log($"Detected video size of {Path.GetFileName(path)} as {size.Width}x{size.Height}", true);
return FfmpegCommands.GetSize(path);
}
catch (Exception ex)
{
Logger.Log("Failed to read video size!");
Logger.Log(ex.ToString(), true);
return new Size();
}

return size;
}

/// <summary>
Expand Down
27 changes: 8 additions & 19 deletions CodeLegacy/Media/GetMediaResolutionCached.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Flowframes.Data;
using Flowframes.IO;
Expand All @@ -12,49 +14,36 @@ class GetMediaResolutionCached

public static async Task<Size> GetSizeAsync(string path)
{
Logger.Log($"Getting media resolution ({path})", true);

long filesize = IoUtils.GetPathSize(path);
QueryInfo hash = new QueryInfo(path, filesize);

if (filesize > 0 && CacheContains(hash))
{
Logger.Log($"Cache contains this hash, using cached value.", true);
return GetFromCache(hash);
}
else
{
Logger.Log($"Hash not cached, reading resolution.", true);
Size cachedVal = GetFromCache(hash);
Logger.Log($"Resolution of '{Path.GetFileName(path)}': {cachedVal.Width}x{cachedVal.Height} [Cached]", true);
return cachedVal;
}

Size size;
size = await IoUtils.GetVideoOrFramesRes(path);

if(size.Width > 0 && size.Height > 0)
{
Logger.Log($"Adding hash with value {size} to cache.", true);
cache.Add(hash, size);
}

Logger.Log($"Resolution of '{Path.GetFileName(path)}': {size.Width}x{size.Height}", true);
return size;
}

private static bool CacheContains(QueryInfo hash)
{
foreach (KeyValuePair<QueryInfo, Size> entry in cache)
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
return true;

return false;
return cache.Any(entry => entry.Key.path == hash.path && entry.Key.filesize == hash.filesize);
}

private static Size GetFromCache(QueryInfo hash)
{
foreach (KeyValuePair<QueryInfo, Size> entry in cache)
if (entry.Key.path == hash.path && entry.Key.filesize == hash.filesize)
return entry.Value;

return new Size();
return cache.Where(entry => entry.Key.path == hash.path && entry.Key.filesize == hash.filesize).Select(entry => entry.Value).FirstOrDefault();
}

public static void Clear()
Expand Down
2 changes: 1 addition & 1 deletion CodeLegacy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void LaunchGui()

bool showMdlDownloader = Cli.ShowMdlDownloader || args.Contains("show-model-downloader"); // The latter check may be needed for legacy reasons

mainForm = new Form1() { ShowModelDownloader = showMdlDownloader, Enabled = false };
mainForm = new Form1() { ShowModelDownloader = showMdlDownloader, Enabled = !Cli.AutoRun };
Application.Run(mainForm);
}

Expand Down

0 comments on commit 70841fe

Please sign in to comment.