From 9b20e74a5d79e06ad335479de5dd01c32059a7ac Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Wed, 23 Dec 2020 00:07:06 +0100 Subject: [PATCH] Fix consecutive scene change frames --- Code/AudioVideo/FFmpegCommands.cs | 1 + Code/Main/Interpolate.cs | 2 ++ Code/Main/InterpolateUtils.cs | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Code/AudioVideo/FFmpegCommands.cs b/Code/AudioVideo/FFmpegCommands.cs index 3f990f3..0cace9b 100644 --- a/Code/AudioVideo/FFmpegCommands.cs +++ b/Code/AudioVideo/FFmpegCommands.cs @@ -1,5 +1,6 @@ using Flowframes.Data; using Flowframes.IO; +using Flowframes.Main; using System; using System.Drawing; using System.Globalization; diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index fc2a204..09b56c4 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -83,6 +83,7 @@ namespace Flowframes Program.mainForm.SetStatus("Extracting frames from video..."); await FFmpegCommands.VideoToFrames(inPath, outPath, Config.GetInt("dedupMode") == 2, false, Utils.GetOutputResolution(inPath, true)); + Utils.FixConsecutiveSceneFrames(Path.Combine(current.tempFolder, Paths.scenesDir), current.framesFolder); if (extractAudio) { @@ -90,6 +91,7 @@ namespace Flowframes if (audioFile != null && !File.Exists(audioFile)) await FFmpegCommands.ExtractAudio(inPath, audioFile); } + if (!canceled && Config.GetBool("enableLoop") && Config.GetInt("timingMode") != 1) { string lastFrame = IOUtils.GetHighestFrameNumPath(outPath); diff --git a/Code/Main/InterpolateUtils.cs b/Code/Main/InterpolateUtils.cs index 699c8ff..7ad7b6d 100644 --- a/Code/Main/InterpolateUtils.cs +++ b/Code/Main/InterpolateUtils.cs @@ -5,6 +5,7 @@ using Flowframes.OS; using Flowframes.UI; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; @@ -315,5 +316,31 @@ namespace Flowframes.Main int b = a + 2; // Larger multiple return (n - a > b - n) ? b : a; // Return of closest of two } + + public static void FixConsecutiveSceneFrames (string sceneFramesPath, string sourceFramesPath) + { + Stopwatch sw = new Stopwatch(); + + List sceneFrames = IOUtils.GetFilesSorted(sceneFramesPath).Select(x => Path.GetFileNameWithoutExtension(x)).ToList(); + List sourceFrames = IOUtils.GetFilesSorted(sourceFramesPath).Select(x => Path.GetFileNameWithoutExtension(x)).ToList(); + List sceneFramesToDelete = new List(); + + foreach(string scnFrame in sceneFrames) + { + if (sceneFramesToDelete.Contains(scnFrame)) + continue; + + int sourceIndexForScnFrame = sourceFrames.IndexOf(scnFrame); // Get source index of scene frame + string followingFrame = sourceFrames[sourceIndexForScnFrame + 1]; // Get filename/timestamp of the next source frame + + if (sceneFrames.Contains(followingFrame)) // If next source frame is in scene folder, add to deletion list + sceneFramesToDelete.Add(followingFrame); + } + + foreach (string frame in sceneFramesToDelete) + IOUtils.TryDeleteIfExists(Path.Combine(sceneFramesPath, frame + ".png")); + + Logger.Log("Ran FixConsecutiveSceneFrames in " + FormatUtils.Time(sw.Elapsed)); + } } }