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
# 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:
1:
audio: jpn

View File

@@ -3,9 +3,13 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.cli.*;
import java.util.List;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
import static java.lang.Integer.parseInt;
@Log4j2
@@ -18,11 +22,12 @@ public class Main {
private static void initConfig(String[] args) {
Options options = new Options();
options.addOption("h", "help", false, "\"for help this is\" - Yoda");
options.addRequiredOption("l", "library", true, "path to library");
options.addOption("c", "config", false, "path to config");
options.addOption("t", "threads", true, "thread count");
options.addOption("s", "safe-mode", false, "Test run (no files will be changes)");
options.addOption("h", HELP.toString(), false, "\"for help this is\" - Yoda");
options.addRequiredOption("l", LIBRARY.toString(), true, "path to library");
options.addOption("c", CONFIG.toString(), false, "path to config");
options.addOption("t", THREADS.toString(), true, "thread count");
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();
HelpFormatter formatter = new HelpFormatter();
@@ -35,10 +40,11 @@ public class Main {
}
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"));
if (cmd.hasOption("threads")) config.setThreadCount(parseInt(cmd.getOptionValue("threads")));
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();
} catch (ParseException e) {
log.error(e);
@@ -46,4 +52,11 @@ public class Main {
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;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
@@ -12,10 +13,12 @@ import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
@Log4j2
@Getter
@Setter
@@ -26,6 +29,7 @@ public class Config {
private List<AttributeConfig> attributeConfig;
private int threadCount;
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs"));
@Getter(AccessLevel.NONE)
private String mkvtoolnixPath;
private String libraryPath;
@@ -33,13 +37,13 @@ public class Config {
private boolean isWindows;
public static Config getInstance() {
if(config == null) {
if (config == null) {
config = new Config();
}
return config;
}
public void isValid() throws RuntimeException{
public void isValid() throws RuntimeException {
boolean isValid = true;
if (attributeConfig == null || attributeConfig.isEmpty()
|| !attributeConfig.stream().allMatch(AttributeConfig::isValid)) {
@@ -63,17 +67,18 @@ public class Config {
}
public void loadConfig(String configPath) {
try(YAML config = new YAML(new File(configPath))){
try (YAML config = new YAML(new File(configPath))) {
setAttributeConfig(loadAttributeConfig(config));
setThreadCount(loadThreadCount(config));
setMkvtoolnixPath(loadMkvToolNixPath(config));
setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
}catch(YamlInvalidContentException | YamlKeyNotFoundException | IOException e){
getForcedKeywords().addAll(loadForcedKeywords(config));
} catch (YamlInvalidContentException | YamlKeyNotFoundException | IOException e) {
log.fatal("Config could not be loaded: {}", e.getMessage());
}
}
private List<AttributeConfig> loadAttributeConfig(YAML config){
private List<AttributeConfig> loadAttributeConfig(YAML config) {
Function<String, String> audio = key -> config.getString(key + ".audio", null);
Function<String, String> subtitle = key -> config.getString(key + ".subtitle", null);
@@ -84,15 +89,19 @@ public class Config {
.collect(Collectors.toList());
}
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException{
return config.isSet("threadCount")
? Integer.parseInt(config.getString("threadCount"))
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException {
return config.isSet(ConfigProperty.THREADS.toString())
? Integer.parseInt(config.getString(ConfigProperty.THREADS.toString()))
: 1;
}
private List<String> loadForcedKeywords(YAML config) {
return config.getStringList(ConfigProperty.FORCED_KEYWORDS.toString(), new ArrayList<>());
}
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
return config.isSet("mkvtoolnixPath")
? config.getString("mkvtoolnixPath")
return config.isSet(MKV_TOOL_NIX.toString())
? config.getString(MKV_TOOL_NIX.toString())
: 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;
}
}