Compress python runtime, fix autoencode problems with dupes

This commit is contained in:
N00MKRAD
2021-01-04 14:27:34 +01:00
parent 1cf2c54ca6
commit 33f8a2b7c9
10 changed files with 127 additions and 21 deletions

View File

@@ -29,13 +29,8 @@ namespace Flowframes
{
lastOutputFfmpeg = "";
currentLogMode = logMode;
Process ffmpeg = new Process();
Process ffmpeg = OSUtils.NewProcess(true);
lastProcess = ffmpeg;
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.CreateNoWindow = true;
ffmpeg.StartInfo.FileName = "cmd.exe";
if(!string.IsNullOrWhiteSpace(workingDir))
ffmpeg.StartInfo.Arguments = $"{GetCmdArg()} cd /D {workingDir.Wrap()} & {Path.Combine(GetAvDir(), "ffmpeg.exe").Wrap()} -hide_banner -loglevel warning -y -stats {args}";
else

View File

@@ -38,7 +38,6 @@ namespace Flowframes
await VideoToFrames(inputFile, frameFolderPath, deDupe, delSrc, new Size(), timecodes);
}
//public enum TimecodeMode { None, Consecutive, Realtime }
public static async Task VideoToFrames(string inputFile, string frameFolderPath, bool deDupe, bool delSrc, Size size, bool timecodes, bool sceneDetect = false)
{
if (!sceneDetect) Logger.Log("Extracting video frames from input video...");

View File

@@ -270,7 +270,7 @@
<Compile Include="Magick\Dedupe.cs" />
<Compile Include="OS\NvApi.cs" />
<Compile Include="OS\OSUtils.cs" />
<Compile Include="OS\Pytorch.cs" />
<Compile Include="OS\Python.cs" />
<Compile Include="OS\Updater.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -63,6 +63,7 @@ namespace Flowframes
GetWebInfo.LoadNews(newsLabel);
GetWebInfo.LoadPatronListCsv(patronsLabel);
Updater.AsyncUpdateCheck();
Python.CheckCompression();
}
catch (Exception e)
{
@@ -231,7 +232,7 @@ namespace Flowframes
Logger.Log($"Warning: {GetAi().aiName.Replace("_", "-")} doesn't natively support 4x/8x and will run multiple times for {guiInterpFactor}x. Auto-Encode will only work on the last run.");
}
public void SetWorking(bool state)
public void SetWorking(bool state, bool allowCancel = true)
{
Control[] controlsToDisable = new Control[] { runBtn, runStepBtn, stepSelector, settingsBtn, installerBtn };
Control[] controlsToHide = new Control[] { runBtn, runStepBtn, stepSelector };
@@ -241,6 +242,8 @@ namespace Flowframes
c.Enabled = !state;
foreach (Control c in controlsToHide)
c.Visible = !state;
if (!allowCancel)
cancelBtn.Enabled = false;
Program.busy = state;
Program.mainForm.UpdateStepByStepControls(false);
}

View File

@@ -94,7 +94,7 @@ namespace Flowframes.Forms
pkgList.SetItemChecked(i, true);
if (pkgList.Items[i].ToString() == "Python" && IsFirstRun())
pkgList.SetItemChecked(i, !Pytorch.IsPytorchReady()); // Enable if system pytorch was not detected
pkgList.SetItemChecked(i, !Python.IsPytorchReady()); // Enable if system pytorch was not detected
}
}

View File

@@ -660,6 +660,31 @@ namespace Flowframes.IO
}
}
public static long GetDirSize(string path, bool recursive, string[] includedExtensions = null)
{
long size = 0;
// Add file sizes.
string[] files;
StringComparison ignCase = StringComparison.OrdinalIgnoreCase;
if (includedExtensions == null)
files = Directory.GetFiles(path);
else
files = Directory.GetFiles(path).Where(file => includedExtensions.Any(x => file.EndsWith(x, ignCase))).ToArray();
foreach (string file in files)
size += new FileInfo(file).Length;
if (!recursive)
return size;
// Add subdirectory sizes.
DirectoryInfo[] dis = new DirectoryInfo(path).GetDirectories();
foreach (DirectoryInfo di in dis)
size += GetDirSize(di.FullName, true, includedExtensions);
return size;
}
public static long GetFilesize(string path)
{
return new FileInfo(path).Length;

View File

@@ -62,6 +62,31 @@ namespace Flowframes
}
}
public static void WriteToFile (string content, bool append, string filename)
{
if (string.IsNullOrWhiteSpace(filename))
filename = defaultLogName;
if (Path.GetExtension(filename) != ".txt")
filename = Path.ChangeExtension(filename, "txt");
file = Path.Combine(Paths.GetLogPath(), filename);
string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second;
try
{
if (append)
File.AppendAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
else
File.WriteAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
}
catch
{
}
}
public static void Clear ()
{
textbox.Text = "";

View File

@@ -59,10 +59,12 @@ namespace Flowframes.Main
//unencodedFrameLines = interpFramesLines.Select(x => x.GetInt()).ToList().Except(encodedFrameLines).ToList();
Logger.Log($"{unencodedFrameLines.Count} unencoded frame lines, {encodedFrameLines.Count} encoded frame lines", true, false, "ffmpeg");
unencodedFrameLines.Clear();
for(int vfrLine = 0; vfrLine < interpFramesLines.Length; vfrLine++)
{
if (!encodedFrameLines.Contains(vfrLine))
if (File.Exists(Path.Combine(interpFramesPath, interpFramesLines[vfrLine])) && !encodedFrameLines.Contains(vfrLine))
unencodedFrameLines.Add(vfrLine);
}
@@ -75,6 +77,7 @@ namespace Flowframes.Main
busy = true;
List<int> frameLinesToEncode = aiRunning ? unencodedFrameLines.Take(chunkSize).ToList() : unencodedFrameLines; // Take all remaining frames if process is done
Logger.Log($"{unencodedFrameLines.Count} unencoded frame lines, {IOUtils.GetAmountOfFiles(interpFramesFolder, false)} frames in interp folder", true, false, "ffmpeg");
Logger.Log($"Encoding Chunk #{videoIndex} using {Path.GetFileName(interpFramesLines[frameLinesToEncode.First()])} through {Path.GetFileName(Path.GetFileName(interpFramesLines[frameLinesToEncode.Last()]))}", true, false, "ffmpeg");
//IOUtils.ZeroPadDir(framesToEncode, Padding.interpFrames); // Zero-pad frames before encoding to make sure filenames match with VFR file

View File

@@ -164,7 +164,7 @@ namespace Flowframes
Process rifePy = OSUtils.NewProcess(!OSUtils.ShowHiddenCmd());
AiStarted(rifePy, 3500, Interpolate.current.interpFactor);
rifePy.StartInfo.Arguments = $"{OSUtils.GetCmdArg()} cd /D {PkgUtils.GetPkgFolder(Packages.rifeCuda).Wrap()} & " +
$"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Pytorch.GetPyCmd()} {script} {args}";
$"set CUDA_VISIBLE_DEVICES={Config.Get("torchGpus")} & {Python.GetPyCmd()} {script} {args}";
Logger.Log($"Running RIFE {(uhd ? "(UHD Mode)" : "")} ({script})...".TrimWhitespaces(), false);
Logger.Log("cmd.exe " + rifePy.StartInfo.Arguments, true);
if (!OSUtils.ShowHiddenCmd())
@@ -270,7 +270,7 @@ namespace Flowframes
{
hasShownError = true;
InterpolateUtils.ShowMessage($"A python module is missing.\nCheck {logFilename} for details.\n\n{line}\n\nIf you don't want to install it yourself, use the Python package from the Package Installer.", "Error");
if(!Pytorch.HasEmbeddedPyFolder())
if(!Python.HasEmbeddedPyFolder())
Process.Start("https://github.com/n00mkrad/flowframes/blob/main/PythonDependencies.md");
}

View File

@@ -1,28 +1,73 @@
using Flowframes.IO;
using Flowframes.MiscUtils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Flowframes.OS
{
class Pytorch
class Python
{
static bool hasCheckedSysPy = false;
static bool sysPyInstalled = false;
public static string compactOutput;
public static async Task CheckCompression ()
{
if(HasEmbeddedPyFolder() && (Config.Get("compressedPyVersion") != Updater.GetInstalledVer().ToString()))
{
Program.mainForm.SetWorking(true, false);
Stopwatch sw = new Stopwatch();
sw.Restart();
try
{
bool shownPatienceMsg = false;
Logger.Log("Compressing python runtime. This only needs to be done once.");
compactOutput = "";
Process compact = OSUtils.NewProcess(true);
compact.StartInfo.Arguments = $"/C compact /C /S:{GetPyFolder().Wrap()} /EXE:LZX";
compact.OutputDataReceived += new DataReceivedEventHandler(CompactOutputHandler);
compact.ErrorDataReceived += new DataReceivedEventHandler(CompactOutputHandler);
compact.Start();
compact.BeginOutputReadLine();
compact.BeginErrorReadLine();
while (!compact.HasExited)
{
await Task.Delay(500);
if(sw.ElapsedMilliseconds > 10000)
{
Logger.Log($"This can take up to a few minutes... (Elapsed: {FormatUtils.Time(sw.Elapsed)})", false, shownPatienceMsg);
shownPatienceMsg = true;
await Task.Delay(500);
}
}
Config.Set("compressedPyVersion", Updater.GetInstalledVer().ToString());
Logger.Log("Done compressing python runtime.");
Logger.WriteToFile(compactOutput, true, "compact");
}
catch { }
Program.mainForm.SetWorking(false);
}
}
static void CompactOutputHandler (object sendingProcess, DataReceivedEventArgs outLine)
{
if (outLine == null || outLine.Data == null)
return;
string line = outLine.Data;
compactOutput = compactOutput + line + "\n";
}
public static string GetPyCmd ()
{
if (HasEmbeddedPyFolder())
{
Logger.Log("Using embedded Python runtime.");
string pyPkgDir = Path.Combine(Paths.GetPkgPath(), "py-amp");
if (!Directory.Exists(pyPkgDir))
pyPkgDir = Path.Combine(Paths.GetPkgPath(), "py-tu");
if (!Directory.Exists(pyPkgDir))
return "";
return Path.Combine(pyPkgDir, "python.exe").Wrap();
return Path.Combine(GetPyFolder(), "python.exe").Wrap();
}
else
{
@@ -41,7 +86,18 @@ namespace Flowframes.OS
public static bool HasEmbeddedPyFolder ()
{
return Directory.Exists(Path.Combine(Paths.GetPkgPath(), "py-tu")) || Directory.Exists(Path.Combine(Paths.GetPkgPath(), "py-ampt"));
return Directory.Exists(GetPyFolder());
}
public static string GetPyFolder ()
{
if (Directory.Exists(Path.Combine(Paths.GetPkgPath(), "py-amp")))
return Path.Combine(Paths.GetPkgPath(), "py-amp");
if (Directory.Exists(Path.Combine(Paths.GetPkgPath(), "py-tu")))
return Path.Combine(Paths.GetPkgPath(), "py-tu");
return "";
}
public static bool IsPytorchReady ()