2020-11-29 16:10:31 +01:00
|
|
|
|
using Flowframes.Data;
|
|
|
|
|
|
using Flowframes.IO;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flowframes.Main
|
|
|
|
|
|
{
|
|
|
|
|
|
using static Interpolate;
|
|
|
|
|
|
|
|
|
|
|
|
class InterpolateSteps
|
|
|
|
|
|
{
|
|
|
|
|
|
public enum Step { ExtractScnChanges, ExtractFrames, Interpolate, CreateVid, Reset }
|
|
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
//public static string current.inPath;
|
|
|
|
|
|
//public static string currentOutPath;
|
|
|
|
|
|
//public static string current.interpFolder;
|
|
|
|
|
|
//public static AI currentAi;
|
|
|
|
|
|
//public static OutMode currentOutMode;
|
2020-11-29 16:10:31 +01:00
|
|
|
|
|
|
|
|
|
|
public static async Task Run(string step)
|
|
|
|
|
|
{
|
|
|
|
|
|
canceled = false;
|
|
|
|
|
|
Program.mainForm.SetWorking(true);
|
2020-12-17 11:32:45 +01:00
|
|
|
|
current = Program.mainForm.GetCurrentSettings();
|
2020-11-29 16:10:31 +01:00
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
if (!InterpolateUtils.InputIsValid(current.inPath, current.outPath, current.outFps, current.interpFactor, current.tilesize)) return; // General input checks
|
2020-12-02 23:11:27 +01:00
|
|
|
|
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (step.Contains("Extract Scene Changes"))
|
2020-12-02 14:36:41 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
if (!current.inputIsFrames) // Input is video - extract frames first
|
2020-12-02 14:36:41 +01:00
|
|
|
|
await ExtractSceneChanges();
|
|
|
|
|
|
else
|
|
|
|
|
|
InterpolateUtils.ShowMessage("Scene changes can only be extracted from videos, not frames!", "Error");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:53:27 +01:00
|
|
|
|
if (step.Contains("Extract Frames"))
|
2020-12-02 14:36:41 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
if (!current.inputIsFrames) // Input is video - extract frames first
|
2020-12-02 14:36:41 +01:00
|
|
|
|
await ExtractVideoFrames();
|
|
|
|
|
|
else
|
2020-12-17 11:32:45 +01:00
|
|
|
|
await FFmpegCommands.ImportImages(current.inPath, current.framesFolder);
|
2020-12-02 14:36:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (step.Contains("Run Interpolation"))
|
|
|
|
|
|
await DoInterpolate();
|
2020-12-02 14:36:41 +01:00
|
|
|
|
|
2020-12-02 23:53:27 +01:00
|
|
|
|
if (step.Contains("Export"))
|
2020-11-29 16:10:31 +01:00
|
|
|
|
await CreateOutputVid();
|
2020-12-02 14:36:41 +01:00
|
|
|
|
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (step.Contains("Cleanup"))
|
|
|
|
|
|
await Reset();
|
|
|
|
|
|
|
|
|
|
|
|
Program.mainForm.SetWorking(false);
|
|
|
|
|
|
Logger.Log("Done running this step.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:11:27 +01:00
|
|
|
|
public static async Task ExtractSceneChanges()
|
2020-11-29 16:10:31 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
string scenesPath = Path.Combine(current.tempFolder, Paths.scenesDir);
|
2020-12-02 00:44:43 +01:00
|
|
|
|
if (!IOUtils.TryDeleteIfExists(scenesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
InterpolateUtils.ShowMessage("Failed to delete existing scenes folder - Make sure no file is opened in another program!", "Error");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-11-29 16:10:31 +01:00
|
|
|
|
Program.mainForm.SetStatus("Extracting scenes from video...");
|
2020-12-17 11:32:45 +01:00
|
|
|
|
await FFmpegCommands.ExtractSceneChanges(current.inPath, scenesPath);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
await Task.Delay(10);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:11:27 +01:00
|
|
|
|
public static async Task ExtractVideoFrames()
|
2020-11-29 16:10:31 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
if (!IOUtils.TryDeleteIfExists(current.framesFolder))
|
2020-12-02 00:44:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
InterpolateUtils.ShowMessage("Failed to delete existing frames folder - Make sure no file is opened in another program!", "Error");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-12-15 14:46:33 +01:00
|
|
|
|
|
2020-12-07 00:41:07 +01:00
|
|
|
|
AiProcess.filenameMap.Clear();
|
2020-11-29 16:10:31 +01:00
|
|
|
|
bool extractAudio = true;
|
|
|
|
|
|
Program.mainForm.SetStatus("Extracting frames from video...");
|
2020-12-22 23:45:07 +01:00
|
|
|
|
await FFmpegCommands.VideoToFrames(current.inPath, current.framesFolder, Config.GetInt("dedupMode") == 2, false, InterpolateUtils.GetOutputResolution(current.inPath, true));
|
2020-12-15 14:46:33 +01:00
|
|
|
|
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (extractAudio)
|
|
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
string audioFile = Path.Combine(current.tempFolder, "audio.m4a");
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (audioFile != null && !File.Exists(audioFile))
|
2020-12-17 11:32:45 +01:00
|
|
|
|
await FFmpegCommands.ExtractAudio(current.inPath, audioFile);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (!canceled && Config.GetBool("enableLoop") && Config.GetInt("timingMode") != 1)
|
|
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
string lastFrame = IOUtils.GetHighestFrameNumPath(current.framesFolder);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
int newNum = Path.GetFileName(lastFrame).GetInt() + 1;
|
2020-12-02 17:23:24 +01:00
|
|
|
|
string newFilename = Path.Combine(lastFrame.GetParentDir(), newNum.ToString().PadLeft(Padding.inputFrames, '0') + ".png");
|
2020-12-17 11:32:45 +01:00
|
|
|
|
string firstFrame = new DirectoryInfo(current.framesFolder).GetFiles("*.png")[0].FullName;
|
2020-11-29 16:10:31 +01:00
|
|
|
|
File.Copy(firstFrame, newFilename);
|
|
|
|
|
|
Logger.Log("Copied loop frame.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:11:27 +01:00
|
|
|
|
public static async Task DoInterpolate()
|
2020-11-29 16:10:31 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
current.framesFolder = Path.Combine(current.tempFolder, Paths.framesDir);
|
|
|
|
|
|
if (!Directory.Exists(current.framesFolder) || IOUtils.GetAmountOfFiles(current.framesFolder, false, "*.png") < 2)
|
2020-12-02 00:44:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
InterpolateUtils.ShowMessage("There are no extracted frames that can be interpolated!\nDid you run the extraction step?", "Error");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-12-17 11:32:45 +01:00
|
|
|
|
if (!IOUtils.TryDeleteIfExists(current.interpFolder))
|
2020-12-02 00:44:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
InterpolateUtils.ShowMessage("Failed to delete existing frames folder - Make sure no file is opened in another program!", "Error");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
currentInputFrameCount = InterpolateUtils.GetInputFrameCount(current.inPath);
|
2020-12-07 00:41:07 +01:00
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
foreach (string ini in Directory.GetFiles(current.tempFolder, "*.ini", SearchOption.TopDirectoryOnly))
|
2020-12-01 16:54:12 +01:00
|
|
|
|
IOUtils.TryDeleteIfExists(ini);
|
|
|
|
|
|
|
2020-12-02 00:44:43 +01:00
|
|
|
|
IOUtils.ReverseRenaming(AiProcess.filenameMap, true); // Get timestamps back
|
2020-12-07 00:41:07 +01:00
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
// TODO: Check if this works lol, remove if it does
|
|
|
|
|
|
//if (Config.GetBool("sbsAllowAutoEnc"))
|
|
|
|
|
|
// nextOutPath = Path.Combine(currentOutPath, Path.GetFileNameWithoutExtension(current.inPath) + IOUtils.GetAiSuffix(current.ai, current.interpFactor) + InterpolateUtils.GetExt(current.outMode));
|
2020-12-07 12:34:12 +01:00
|
|
|
|
|
2020-11-30 20:52:43 +01:00
|
|
|
|
await PostProcessFrames(true);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
int frames = IOUtils.GetAmountOfFiles(current.framesFolder, false, "*.png");
|
|
|
|
|
|
int targetFrameCount = frames * current.interpFactor;
|
2020-12-20 21:25:34 +01:00
|
|
|
|
InterpolateUtils.GetProgressByFrameAmount(current.interpFolder, targetFrameCount);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
if (canceled) return;
|
|
|
|
|
|
Program.mainForm.SetStatus("Running AI...");
|
2020-12-17 11:32:45 +01:00
|
|
|
|
int tilesize = current.ai.supportsTiling ? Config.GetInt($"tilesize_{current.ai.aiName}") : 512;
|
|
|
|
|
|
await RunAi(current.interpFolder, targetFrameCount, tilesize, current.ai, true);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
Program.mainForm.SetProgress(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:11:27 +01:00
|
|
|
|
public static async Task CreateOutputVid()
|
2020-11-29 16:10:31 +01:00
|
|
|
|
{
|
2020-12-22 19:52:37 +01:00
|
|
|
|
string[] outFrames = IOUtils.GetFilesSorted(current.interpFolder, $"*.{InterpolateUtils.GetOutExt()}");
|
2020-12-07 12:34:12 +01:00
|
|
|
|
if (outFrames.Length > 0 && !IOUtils.CheckImageValid(outFrames[0]))
|
|
|
|
|
|
{
|
|
|
|
|
|
InterpolateUtils.ShowMessage("Invalid frame files detected!\n\nIf you used Auto-Encode, this is normal, and you don't need to run " +
|
|
|
|
|
|
"this step as the video was already created in the \"Interpolate\" step.", "Error");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-17 11:32:45 +01:00
|
|
|
|
string outPath = Path.Combine(current.outPath, Path.GetFileNameWithoutExtension(current.inPath) + IOUtils.GetAiSuffix(current.ai, current.interpFactor) + InterpolateUtils.GetExt(current.outMode));
|
|
|
|
|
|
await CreateVideo.Export(current.interpFolder, outPath, current.outMode);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-02 23:11:27 +01:00
|
|
|
|
public static async Task Reset()
|
2020-11-29 16:10:31 +01:00
|
|
|
|
{
|
2020-12-17 11:32:45 +01:00
|
|
|
|
Cleanup(current.interpFolder, true);
|
2020-11-29 16:10:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|