Adds option for overwriting original files
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-07-18 14:07:51 +02:00
parent 972691db76
commit 87f5743a24
6 changed files with 40 additions and 1 deletions

View File

@ -62,6 +62,11 @@ public class ConfigKey<T> {
*/ */
public static final ConfigKey<Boolean> COPY_ATTACHED_IMAGES = createKey("copy-attached-images", Boolean.class, false); public static final ConfigKey<Boolean> 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<Boolean> OVERWRITE_FILES = createKey("overwrite-files", Boolean.class, false);
private final String configKey; private final String configKey;
private final T defaultValue; private final T defaultValue;
private final Class<T> type; private final Class<T> type;

View File

@ -23,6 +23,7 @@ public class Configuration {
private MinimalSubtitlePreference minimalSubtitlePreference; private MinimalSubtitlePreference minimalSubtitlePreference;
private boolean deInterlaceVideo; private boolean deInterlaceVideo;
private boolean copyAttachedImages; private boolean copyAttachedImages;
private boolean overwriteFiles;
/** /**
* Instantiates and loads a new configuration * Instantiates and loads a new configuration
@ -57,6 +58,7 @@ public class Configuration {
animeSubtitleLanguages = ConfigHelper.asStringList(configHandler.getValue(ConfigKey.SUBTITLE_LANGUAGES_ANIME)); animeSubtitleLanguages = ConfigHelper.asStringList(configHandler.getValue(ConfigKey.SUBTITLE_LANGUAGES_ANIME));
deInterlaceVideo = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.DE_INTERLACE_VIDEO)); deInterlaceVideo = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.DE_INTERLACE_VIDEO));
copyAttachedImages = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.COPY_ATTACHED_IMAGES)); copyAttachedImages = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.COPY_ATTACHED_IMAGES));
overwriteFiles = ConfigHelper.asBoolean(configHandler.getValue(ConfigKey.OVERWRITE_FILES));
try { try {
minimalSubtitlePreference = MinimalSubtitlePreference.valueOf(String.valueOf(configHandler.getValue( minimalSubtitlePreference = MinimalSubtitlePreference.valueOf(String.valueOf(configHandler.getValue(
ConfigKey.MINIMAL_SUBTITLE_PREFERENCE))); ConfigKey.MINIMAL_SUBTITLE_PREFERENCE)));
@ -171,4 +173,13 @@ public class Configuration {
return this.copyAttachedImages; return this.copyAttachedImages;
} }
/**
* Gets whether the original files should be overwritten after conversion has been finished
*
* @return <p>True if the original file should be overwritten</p>
*/
public boolean overwriteFiles() {
return this.overwriteFiles;
}
} }

View File

@ -85,6 +85,16 @@ public abstract class AbstractConverter implements Converter {
int exitCode = FFMpegHelper.runProcess(processBuilder, file.getParentFile(), "\n", true).exitCode(); int exitCode = FFMpegHelper.runProcess(processBuilder, file.getParentFile(), "\n", true).exitCode();
if (exitCode != 0) { if (exitCode != 0) {
handleError(ffMpegCommand, file, newPath); 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);
}
} }
} }

View File

@ -27,6 +27,13 @@ public enum StreamType {
*/ */
COVER_IMAGE, COVER_IMAGE,
/**
* Binary data
*
* <p>Binary data streams only cause problems, as they cannot, for example, be included in an MKV file.</p>
*/
DATA,
/** /**
* None of the above * None of the above
*/ */

View File

@ -314,6 +314,8 @@ public final class FFMpegHelper {
parsedStreams.add(new OtherStream(streamInfo, 0, true)); 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; return parsedStreams;
@ -342,6 +344,8 @@ public final class FFMpegHelper {
return StreamType.AUDIO; return StreamType.AUDIO;
case "subtitle": case "subtitle":
return StreamType.SUBTITLE; return StreamType.SUBTITLE;
case "data":
return StreamType.DATA;
default: default:
return StreamType.OTHER; return StreamType.OTHER;
} }

View File

@ -20,3 +20,5 @@ minimal-subtitle-preference=AVOID
de-interlace-video=false de-interlace-video=false
# Whether to copy attached cover images. FFMpeg sometimes throws errors when including attached images. # Whether to copy attached cover images. FFMpeg sometimes throws errors when including attached images.
copy-attached-images=false 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