From 87f5743a24cfa8e0c6a967184e20a01e9fd232b1 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 18 Jul 2024 14:07:51 +0200 Subject: [PATCH] Adds option for overwriting original files --- .../knarcraft/ffmpegconverter/config/ConfigKey.java | 5 +++++ .../ffmpegconverter/config/Configuration.java | 11 +++++++++++ .../ffmpegconverter/converter/AbstractConverter.java | 10 ++++++++++ .../ffmpegconverter/property/StreamType.java | 7 +++++++ .../ffmpegconverter/utility/FFMpegHelper.java | 4 ++++ src/main/resources/conf/config.properties | 4 +++- 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/config/ConfigKey.java b/src/main/java/net/knarcraft/ffmpegconverter/config/ConfigKey.java index 31f5182..81c174d 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/config/ConfigKey.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/config/ConfigKey.java @@ -62,6 +62,11 @@ public class ConfigKey { */ public static final ConfigKey COPY_ATTACHED_IMAGES = createKey("copy-attached-images", Boolean.class, false); + /** + * The configuration key for whether to overwrite the original file when converting + */ + public static final ConfigKey OVERWRITE_FILES = createKey("overwrite-files", Boolean.class, false); + private final String configKey; private final T defaultValue; private final Class type; diff --git a/src/main/java/net/knarcraft/ffmpegconverter/config/Configuration.java b/src/main/java/net/knarcraft/ffmpegconverter/config/Configuration.java index 5632c03..814c8d6 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/config/Configuration.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/config/Configuration.java @@ -23,6 +23,7 @@ public class Configuration { private MinimalSubtitlePreference minimalSubtitlePreference; private boolean deInterlaceVideo; private boolean copyAttachedImages; + private boolean overwriteFiles; /** * Instantiates and loads a new configuration @@ -57,6 +58,7 @@ public class Configuration { animeSubtitleLanguages = ConfigHelper.asStringList(configHandler.getValue(ConfigKey.SUBTITLE_LANGUAGES_ANIME)); deInterlaceVideo = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.DE_INTERLACE_VIDEO)); copyAttachedImages = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.COPY_ATTACHED_IMAGES)); + overwriteFiles = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.OVERWRITE_FILES)); try { minimalSubtitlePreference = MinimalSubtitlePreference.valueOf(String.valueOf(configHandler.getValue( ConfigKey.MINIMAL_SUBTITLE_PREFERENCE))); @@ -171,4 +173,13 @@ public class Configuration { return this.copyAttachedImages; } + /** + * Gets whether the original files should be overwritten after conversion has been finished + * + * @return

True if the original file should be overwritten

+ */ + public boolean overwriteFiles() { + return this.overwriteFiles; + } + } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java index 4582468..9ef7b29 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java @@ -85,6 +85,16 @@ public abstract class AbstractConverter implements Converter { int exitCode = FFMpegHelper.runProcess(processBuilder, file.getParentFile(), "\n", true).exitCode(); if (exitCode != 0) { handleError(ffMpegCommand, file, newPath); + } else if (FFMpegConvert.getConfiguration().overwriteFiles() && + FileUtil.getExtension(newPath).equalsIgnoreCase(FileUtil.getExtension(file.getPath()))) { + File outputFile = new File(newPath); + if (!file.delete()) { + OutputUtil.println("Unable to remove original file."); + System.exit(1); + } else if (!outputFile.renameTo(file)) { + OutputUtil.println("Failed to re-name file after conversion!"); + System.exit(1); + } } } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/property/StreamType.java b/src/main/java/net/knarcraft/ffmpegconverter/property/StreamType.java index cf169fb..eb9af6a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/property/StreamType.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/property/StreamType.java @@ -27,6 +27,13 @@ public enum StreamType { */ COVER_IMAGE, + /** + * Binary data + * + *

Binary data streams only cause problems, as they cannot, for example, be included in an MKV file.

+ */ + DATA, + /** * None of the above */ diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java index 3934f6c..14cd758 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java @@ -314,6 +314,8 @@ public final class FFMpegHelper { parsedStreams.add(new OtherStream(streamInfo, 0, true)); } } + case DATA -> OutputUtil.print("A binary stream was found. Those are ignored they will generally " + + "cause the conversion to fail."); } } return parsedStreams; @@ -342,6 +344,8 @@ public final class FFMpegHelper { return StreamType.AUDIO; case "subtitle": return StreamType.SUBTITLE; + case "data": + return StreamType.DATA; default: return StreamType.OTHER; } diff --git a/src/main/resources/conf/config.properties b/src/main/resources/conf/config.properties index 3f4d8ca..ab39b5d 100644 --- a/src/main/resources/conf/config.properties +++ b/src/main/resources/conf/config.properties @@ -19,4 +19,6 @@ minimal-subtitle-preference=AVOID # The preference for whether video streams should be de-interlaced. It is recommended to only enable this when you notice that a video file is interlaced. de-interlace-video=false # Whether to copy attached cover images. FFMpeg sometimes throws errors when including attached images. -copy-attached-images=false \ No newline at end of file +copy-attached-images=false +# Whether to overwrite original files after conversion. Note that if enabled, the original files are lost, which is troublesome if the conversion arguments are incorrect. +overwrite-files=false \ No newline at end of file