Rename existing outputs to ".old" instead of overwriting

This commit is contained in:
N00MKRAD
2021-05-09 20:35:42 +02:00
parent 219b4f0ce5
commit 3ba6a76b27
3 changed files with 52 additions and 1 deletions

View File

@@ -435,6 +435,54 @@ namespace Flowframes.IO
return false;
}
/// <summary>
/// Add ".old" suffix to existing files to avoid them being overwritten. If one already exists, it will be ".old.old" etc.
/// </summary>
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);
}
}
/// <summary>
/// Easily rename a file without needing to specify the full move path
/// </summary>
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<string> GetCurrentExportFilename(bool fpsLimit, bool withExt)
{
InterpSettings curr = Interpolate.current;

View File

@@ -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()}";

View File

@@ -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";