Cleanup n stuff

This commit is contained in:
N00MKRAD
2024-08-25 21:18:38 +02:00
parent 26f14876a7
commit 5a3a84bbdc
8 changed files with 19 additions and 97 deletions

View File

@@ -457,7 +457,6 @@
<Compile Include="Media\GetFrameCountCached.cs" />
<Compile Include="Media\GetMediaResolutionCached.cs" />
<Compile Include="Media\GetVideoInfo.cs" />
<Compile Include="MiscUtils\BackgroundTaskManager.cs" />
<Compile Include="MiscUtils\Benchmarker.cs" />
<Compile Include="MiscUtils\FrameRename.cs" />
<Compile Include="MiscUtils\ModelDownloadFormUtils.cs" />

View File

@@ -613,14 +613,11 @@ namespace Flowframes.Forms.Main
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!Program.busy && !BackgroundTaskManager.IsBusy())
if (!Program.busy)
return;
string reason = "";
if (BackgroundTaskManager.IsBusy())
reason = "Some background tasks have not finished yet.";
if (Program.busy)
reason = "The program is still busy.";

View File

@@ -157,10 +157,7 @@ namespace Flowframes.IO
/// </summary>
public static async Task<bool> DeleteContentsOfDirAsync(string path)
{
ulong taskId = BackgroundTaskManager.Add($"DeleteContentsOfDirAsync {path}");
bool returnVal = await Task.Run(async () => { return DeleteContentsOfDir(path); });
BackgroundTaskManager.Remove(taskId);
return returnVal;
return await Task.Run(() => DeleteContentsOfDir(path));
}
/// <summary>
@@ -442,10 +439,7 @@ namespace Flowframes.IO
path = renamedPath;
ulong taskId = BackgroundTaskManager.Add($"TryDeleteIfExistsAsync {path}");
bool returnVal = await Task.Run(async () => { return TryDeleteIfExists(path); });
BackgroundTaskManager.Remove(taskId);
return returnVal;
return await TryDeleteIfExistsAsync(path);
}
catch (Exception e)
{

View File

@@ -249,7 +249,7 @@ namespace Flowframes.Main
await MergeChunks(tempConcatFile, outPath, isBackup);
if (!isBackup)
Task.Run(async () => { await IoUtils.TryDeleteIfExistsAsync(IoUtils.FilenameSuffix(outPath, Paths.backupSuffix)); });
await IoUtils.TryDeleteIfExistsAsync(IoUtils.FilenameSuffix(outPath, Paths.backupSuffix));
}
}
catch (Exception e)

View File

@@ -308,8 +308,8 @@ namespace Flowframes.Media
string sizeStr = (size.Width > 1 && size.Height > 1) ? $"-s {size.Width}x{size.Height}" : "";
bool isPng = (Path.GetExtension(outPath).ToLowerInvariant() == ".png");
string comprArg = isPng ? pngCompr : "";
string pixFmt = "-pix_fmt " + (isPng ? $"rgb24 {comprArg}" : "yuvj420p");
string args = $"-i {inputFile.Wrap()} {comprArg} {sizeStr} {pixFmt} -vf {GetPadFilter()} {outPath.Wrap()}";
string pixFmt = isPng ? $"rgb24 {comprArg}" : "yuv420p -color_range full";
string args = $"-i {inputFile.Wrap()} {comprArg} {sizeStr} -pix_fmt {pixFmt} -vf {GetPadFilter()} {outPath.Wrap()}";
await RunFfmpeg(args, LogMode.Hidden);
}
@@ -317,8 +317,8 @@ namespace Flowframes.Media
{
bool isPng = (Path.GetExtension(outputPath).ToLowerInvariant() == ".png");
string comprArg = isPng ? pngCompr : "";
string pixFmt = "-pix_fmt " + (isPng ? $"rgb24 {comprArg}" : "yuvj420p");
string args = $"-i {inputFile.Wrap()} -vf \"select=eq(n\\,{frameNum})\" -vframes 1 {pixFmt} {outputPath.Wrap()}";
string pixFmt = isPng ? $"rgb24 {comprArg}" : "yuv420p -color_range full";
string args = $"-i {inputFile.Wrap()} -vf \"select=eq(n\\,{frameNum})\" -vframes 1 -update 1 -pix_fmt {pixFmt} {outputPath.Wrap()}";
await RunFfmpeg(args, LogMode.Hidden);
}
@@ -332,11 +332,11 @@ namespace Flowframes.Media
bool isPng = (Path.GetExtension(outputPath).ToLowerInvariant() == ".png");
string comprArg = isPng ? pngCompr : "";
string pixFmt = "-pix_fmt " + (isPng ? $"rgb24 {comprArg}" : "yuvj420p");
string pixFmt = isPng ? $"rgb24 {comprArg}" : "yuv420p -color_range full";
string sizeStr = (size.Width > 1 && size.Height > 1) ? $"-s {size.Width}x{size.Height}" : "";
string trim = QuickSettingsTab.trimEnabled ? $"-ss {QuickSettingsTab.GetTrimEndMinusOne()} -to {QuickSettingsTab.trimEnd}" : "";
string sseof = string.IsNullOrWhiteSpace(trim) ? "-sseof -1" : "";
string args = $"{sseof} -i {inputFile.Wrap()} -update 1 {pixFmt} {sizeStr} {trim} {outputPath.Wrap()}";
string args = $"{sseof} -i {inputFile.Wrap()} -update 1 -pix_fmt {pixFmt} {sizeStr} {trim} {outputPath.Wrap()}";
await RunFfmpeg(args, LogMode.Hidden);
}

View File

@@ -434,9 +434,6 @@ namespace Flowframes.Media
public static bool ContainerSupportsAllAudioFormats(Enums.Output.Format outFormat, List<string> codecs)
{
if (codecs.Count < 1)
Logger.Log($"Warning: ContainerSupportsAllAudioFormats() was called, but codec list has {codecs.Count} entries.", true, false, "ffmpeg");
foreach (string format in codecs)
{
if (!ContainerSupportsAudioFormat(outFormat, format))
@@ -538,17 +535,20 @@ namespace Flowframes.Media
{
containerExt = containerExt.Remove(".");
if (containerExt == "mp4" || containerExt == "mov") return "mov_text";
if (containerExt == "webm") return "webvtt";
if (containerExt == "mp4" || containerExt == "mov")
return "mov_text";
if (containerExt == "webm")
return "webvtt";
return "copy"; // Default: Copy subs
}
public static bool ContainerSupportsSubs(string containerExt, bool showWarningIfNotSupported = true)
{
containerExt = containerExt.Remove(".");
containerExt = containerExt.Lower().Remove(".");
bool supported = (containerExt == "mp4" || containerExt == "mkv" || containerExt == "webm" || containerExt == "mov");
Logger.Log($"Subtitles {(supported ? "are supported" : "not supported")} by {containerExt.ToUpper()}", true);
// Logger.Log($"Subtitles {(supported ? "are supported" : "not supported")} by {containerExt.ToUpper()}", true);
if (showWarningIfNotSupported && Config.GetBool(Config.Key.keepSubs) && !supported)
Logger.Log($"Warning: {containerExt.ToUpper()} exports do not include subtitles.");

View File

@@ -99,11 +99,11 @@ namespace Flowframes.Media
if (!noCache && filesize > 0 && CacheContains(hash, ref cmdCache))
{
Logger.Log($"GetVideoInfo: '{process.StartInfo.FileName} {process.StartInfo.Arguments}' cached, won't re-run.", true, false, "ffmpeg");
// Logger.Log($"GetVideoInfo: '{process.StartInfo.FileName} {process.StartInfo.Arguments}' cached, won't re-run.", true, false, "ffmpeg");
return GetFromCache(hash, ref cmdCache);
}
Logger.Log($"GetVideoInfo: '{process.StartInfo.FileName} {process.StartInfo.Arguments}' not cached, running.", true, false, "ffmpeg");
Logger.Log($"GetVideoInfo: '{process.StartInfo.FileName} {process.StartInfo.Arguments}' (not cached)", true, false, "ffmpeg");
string output = await OsUtils.GetOutputAsync(process);
cmdCache.Add(hash, output);
return output;

View File

@@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.MiscUtils
{
class BackgroundTaskManager
{
public static ulong currentId = 0;
public static List<RunningTask> runningTasks = new List<RunningTask>();
public class RunningTask
{
public NmkdStopwatch timer;
public string name;
public ulong id;
public int timeoutSeconds;
public RunningTask (string name, ulong id, int timeoutSeconds)
{
this.name = name;
this.id = id;
this.timeoutSeconds = timeoutSeconds;
timer = new NmkdStopwatch();
}
}
public static bool IsBusy ()
{
Logger.Log($"[BgTaskMgr] BackgroundTaskManager is busy - {runningTasks.Count} tasks running.", true);
return runningTasks.Count > 0;
}
public static void ClearExpired ()
{
foreach(RunningTask task in runningTasks)
{
if(task.timer.Sw.ElapsedMilliseconds > task.timeoutSeconds * 1000)
{
Logger.Log($"[BgTaskMgr] Task with ID {task.id} timed out, has been running for {task.timer}!", true);
runningTasks.Remove(task);
}
}
}
public static ulong Add(string name = "Unnamed Task", int timeoutSeconds = 120)
{
ulong id = currentId;
runningTasks.Add(new RunningTask(name, currentId, timeoutSeconds));
currentId++;
return id;
}
public static void Remove(ulong id)
{
foreach(RunningTask task in new List<RunningTask>(runningTasks))
{
if(task.id == id)
{
Logger.Log($"[BgTaskMgr] Task '{task.name}' has finished after {task.timer} (Timeout {task.timeoutSeconds}s)", true);
runningTasks.Remove(task);
}
}
}
}
}