From 3ba6a76b27bb282228d595ad4798f9ba0d760f88 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Sun, 9 May 2021 20:35:42 +0200 Subject: [PATCH] Rename existing outputs to ".old" instead of overwriting --- Code/IO/IOUtils.cs | 48 ++++++++++++++++++++++++++++++++++++ Code/Media/FfmpegCommands.cs | 2 ++ Code/Media/FfmpegEncode.cs | 3 ++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 93d7a58..4e0ff03 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -435,6 +435,54 @@ namespace Flowframes.IO return false; } + /// + /// Add ".old" suffix to existing files to avoid them being overwritten. If one already exists, it will be ".old.old" etc. + /// + public static void RenameExistingFile(string path) + { + if (!File.Exists(path)) + return; + + try + { + string ext = Path.GetExtension(path); + string renamedPath = path; + + while (File.Exists(renamedPath)) + renamedPath = Path.ChangeExtension(renamedPath, null) + ".old" + ext; + + File.Move(path, renamedPath); + } + catch(Exception e) + { + Logger.Log($"RenameExistingFile: Failed to rename '{path}': {e.Message}", true); + } + } + + /// + /// Easily rename a file without needing to specify the full move path + /// + public static bool RenameFile (string path, string newName, bool alsoRenameExtension = false) + { + try + { + string dir = Path.GetDirectoryName(path); + string ext = Path.GetExtension(path); + string movePath = Path.Combine(dir, newName); + + if (!alsoRenameExtension) + movePath += ext; + + File.Move(path, movePath); + return true; + } + catch(Exception e) + { + Logger.Log($"Failed to rename '{path}' to '{newName}': {e.Message}", true); + return false; + } + } + public static async Task GetCurrentExportFilename(bool fpsLimit, bool withExt) { InterpSettings curr = Interpolate.current; diff --git a/Code/Media/FfmpegCommands.cs b/Code/Media/FfmpegCommands.cs index d06a54b..f9a6519 100644 --- a/Code/Media/FfmpegCommands.cs +++ b/Code/Media/FfmpegCommands.cs @@ -38,6 +38,8 @@ namespace Flowframes { Logger.Log($"ConcatVideos('{Path.GetFileName(concatFile)}', '{outPath}', {looptimes})", true, false, "ffmpeg"); Logger.Log($"Merging videos...", false, Logger.GetLastLine().Contains("frame")); + + IOUtils.RenameExistingFile(outPath); string loopStr = (looptimes > 0) ? $"-stream_loop {looptimes}" : ""; string vfrFilename = Path.GetFileName(concatFile); string args = $" {loopStr} -vsync 1 -f concat -i {vfrFilename} -c copy -movflags +faststart -fflags +genpts {outPath.Wrap()}"; diff --git a/Code/Media/FfmpegEncode.cs b/Code/Media/FfmpegEncode.cs index d9c8335..40c9925 100644 --- a/Code/Media/FfmpegEncode.cs +++ b/Code/Media/FfmpegEncode.cs @@ -21,7 +21,8 @@ namespace Flowframes.Media { if (logMode != LogMode.Hidden) Logger.Log((resampleFps.GetFloat() <= 0) ? "Encoding video..." : $"Encoding video resampled to {resampleFps.GetString()} FPS..."); - + + IOUtils.RenameExistingFile(outPath); Directory.CreateDirectory(outPath.GetParentDir()); string encArgs = Utils.GetEncArgs(Utils.GetCodec(outMode)); if (!isChunk && outMode == Interpolate.OutMode.VidMp4) encArgs += $" -movflags +faststart";