Add cli property for forced keywords

This commit is contained in:
2022-03-30 20:09:32 +02:00
parent 93f3542cf1
commit f7a2e4234a
4 changed files with 64 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
mkvtoolnixPath: C:\Program Files\MKVToolNix mkvtoolnixPath: C:\Program Files\MKVToolNix
# Only edit this value if you know what you are doing, otherwise it may impact your performance negatively # Only edit this value if you know what you are doing, otherwise it may impact your performance negatively
threadCount: 2 threads: 2
#forcedKeywords: ["forced", "signs"]
config: config:
1: 1:
audio: jpn audio: jpn

View File

@@ -3,9 +3,13 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import java.util.List;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
import static java.lang.Integer.parseInt; import static java.lang.Integer.parseInt;
@Log4j2 @Log4j2
@@ -18,11 +22,12 @@ public class Main {
private static void initConfig(String[] args) { private static void initConfig(String[] args) {
Options options = new Options(); Options options = new Options();
options.addOption("h", "help", false, "\"for help this is\" - Yoda"); options.addOption("h", HELP.toString(), false, "\"for help this is\" - Yoda");
options.addRequiredOption("l", "library", true, "path to library"); options.addRequiredOption("l", LIBRARY.toString(), true, "path to library");
options.addOption("c", "config", false, "path to config"); options.addOption("c", CONFIG.toString(), false, "path to config");
options.addOption("t", "threads", true, "thread count"); options.addOption("t", THREADS.toString(), true, "thread count");
options.addOption("s", "safe-mode", false, "Test run (no files will be changes)"); options.addOption("s", SAFE_MODE.toString(), false, "Test run (no files will be changes)");
options.addOption(create("k", FORCED_KEYWORDS.toString(),Option.UNLIMITED_VALUES,"Additional keywords to identify forced tracks"));
CommandLineParser parser = new DefaultParser(); CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter(); HelpFormatter formatter = new HelpFormatter();
@@ -35,10 +40,11 @@ public class Main {
} }
Config config = Config.getInstance(); Config config = Config.getInstance();
config.loadConfig(cmd.getOptionValue("config", "config.yaml")); // use cmd input config.loadConfig(cmd.getOptionValue(CONFIG.toString(), "config.yaml")); // use cmd input
config.setLibraryPath(cmd.getOptionValue("library")); config.setLibraryPath(cmd.getOptionValue("library"));
if (cmd.hasOption("threads")) config.setThreadCount(parseInt(cmd.getOptionValue("threads")));
config.setSafeMode(cmd.hasOption("safe-mode")); config.setSafeMode(cmd.hasOption("safe-mode"));
if (cmd.hasOption("threads")) config.setThreadCount(parseInt(cmd.getOptionValue("threads")));
if (cmd.hasOption(FORCED_KEYWORDS.toString())) config.getForcedKeywords().addAll(List.of(cmd.getOptionValues(FORCED_KEYWORDS.toString())));
config.isValid(); config.isValid();
} catch (ParseException e) { } catch (ParseException e) {
log.error(e); log.error(e);
@@ -46,4 +52,11 @@ public class Main {
System.exit(1); System.exit(1);
} }
} }
private static Option create(String opt, String longOpt, int args, String desc) {
Option option = new Option(opt, desc);
option.setLongOpt(longOpt);
option.setArgs(args);
return option;
}
} }

View File

@@ -1,6 +1,7 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config; package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException; import at.pcgamingfreaks.yaml.YamlInvalidContentException;
@@ -12,10 +13,12 @@ import lombok.extern.log4j.Log4j2;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
@Log4j2 @Log4j2
@Getter @Getter
@Setter @Setter
@@ -26,6 +29,7 @@ public class Config {
private List<AttributeConfig> attributeConfig; private List<AttributeConfig> attributeConfig;
private int threadCount; private int threadCount;
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs"));
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
private String mkvtoolnixPath; private String mkvtoolnixPath;
private String libraryPath; private String libraryPath;
@@ -68,6 +72,7 @@ public class Config {
setThreadCount(loadThreadCount(config)); setThreadCount(loadThreadCount(config));
setMkvtoolnixPath(loadMkvToolNixPath(config)); setMkvtoolnixPath(loadMkvToolNixPath(config));
setWindows(System.getProperty("os.name").toLowerCase().contains("windows")); setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
getForcedKeywords().addAll(loadForcedKeywords(config));
} catch (YamlInvalidContentException | YamlKeyNotFoundException | IOException e) { } catch (YamlInvalidContentException | YamlKeyNotFoundException | IOException e) {
log.fatal("Config could not be loaded: {}", e.getMessage()); log.fatal("Config could not be loaded: {}", e.getMessage());
} }
@@ -85,14 +90,18 @@ public class Config {
} }
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException { private int loadThreadCount(YAML config) throws YamlKeyNotFoundException {
return config.isSet("threadCount") return config.isSet(ConfigProperty.THREADS.toString())
? Integer.parseInt(config.getString("threadCount")) ? Integer.parseInt(config.getString(ConfigProperty.THREADS.toString()))
: 1; : 1;
} }
private List<String> loadForcedKeywords(YAML config) {
return config.getStringList(ConfigProperty.FORCED_KEYWORDS.toString(), new ArrayList<>());
}
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException { private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
return config.isSet("mkvtoolnixPath") return config.isSet(MKV_TOOL_NIX.toString())
? config.getString("mkvtoolnixPath") ? config.getString(MKV_TOOL_NIX.toString())
: defaultMkvToolNixPath(); : defaultMkvToolNixPath();
} }

View File

@@ -0,0 +1,22 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.model;
public enum ConfigProperty {
MKV_TOOL_NIX("mkvtoolnixPath"),
THREADS("threads"),
FORCED_KEYWORDS("forcedKeywords"),
CONFIG("config"),
LIBRARY("library"),
SAFE_MODE("safe-mode"),
HELP("help");
private String property;
ConfigProperty(String property) {
this.property = property;
}
@Override
public String toString() {
return property;
}
}