diff --git a/pom.xml b/pom.xml index 747b179..fc17d43 100644 --- a/pom.xml +++ b/pom.xml @@ -144,6 +144,11 @@ jackson-databind 2.13.1 + + commons-cli + commons-cli + 1.5.0 + diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java index b030b48..3f36952 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java @@ -21,9 +21,9 @@ public class AttributeUpdaterKernel { @SneakyThrows public void execute(String path) { - List configPattern = ConfigUtil.loadConfig(); + List configPattern = ConfigUtil.getInstance().getAttributeConfig(); List allValidPaths = collector.loadFiles(path); - ExecutorService executor = Executors.newFixedThreadPool(ConfigUtil.getThreadCount()); + ExecutorService executor = Executors.newFixedThreadPool(ConfigUtil.getInstance().getThreadCount()); long beforeTimer = System.currentTimeMillis(); if(allValidPaths != null && configPattern != null){ diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java index ea645a3..78c1f2c 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java @@ -1,40 +1,66 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YamlInvalidContentException; import at.pcgamingfreaks.yaml.YamlKeyNotFoundException; import lombok.extern.log4j.Log4j2; +import org.apache.commons.cli.*; import java.io.File; import java.io.IOException; +import static java.lang.Integer.parseInt; + @Log4j2 public class Main { public static void main(String[] args) { - if(checkIfMKVToolNixIsValid()){ - AttributeUpdaterKernel kernel = new AttributeUpdaterKernel(); - kernel.execute(args[0]); - }else{ - log.error("MKVToolNix was not found! Please recheck path"); - } + System.out.println(String.join(", ", args)); + initConfig(args); + AttributeUpdaterKernel kernel = new AttributeUpdaterKernel(); + kernel.execute(ConfigUtil.getInstance().getLibraryPath()); } private static boolean checkIfMKVToolNixIsValid() { - try{ + try { String path = new YAML(new File("config.yaml")).getString("mkvtoolnixPath"); - if(! path.endsWith(File.separator)){ + if (!path.endsWith(File.separator)) { path += File.separator; } - if(System.getProperty("os.name").toLowerCase().contains("windows")){ + if (System.getProperty("os.name").toLowerCase().contains("windows")) { MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge.exe"); MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit.exe"); - }else{ + } else { MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge"); MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit"); } - }catch(YamlKeyNotFoundException | IOException | YamlInvalidContentException e){ + } catch (YamlKeyNotFoundException | IOException | YamlInvalidContentException e) { e.printStackTrace(); } return new File(MKVToolProperties.getInstance().getMkvmergePath()).isFile() && new File(MKVToolProperties.getInstance().getMkvpropeditPath()).isFile(); } + + 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"); + + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + try { + CommandLine cmd = parser.parse(options, args); + + ConfigUtil configUtil = ConfigUtil.getInstance(); + configUtil.loadConfig(cmd.getOptionValue("config", "config.yaml")); // use cmd input + configUtil.setLibraryPath(cmd.getOptionValue("library")); + if (cmd.hasOption("threads")) configUtil.setThreadCount(parseInt(cmd.getOptionValue("threads"))); + configUtil.isValid(); + } catch (ParseException e) { + log.error(e); + formatter.printHelp("java -jar MKVAudioSubtitlesChanger.jar -p ", options); + System.exit(1); + } + } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/AttributeConfig.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/AttributeConfig.java index 729e82a..200ea6c 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/AttributeConfig.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/AttributeConfig.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.extern.log4j.Log4j2; import java.util.List; +import java.util.stream.Collectors; @Log4j2 @Getter @@ -15,4 +16,13 @@ public class AttributeConfig { this.audio = audio; this.subtitle = subtitle; } + + @Override + public String toString() { + final StringBuffer sb = new StringBuffer("AttributeConfig{"); + sb.append("audio=").append(String.join(", ", audio)); + sb.append(", subtitle=").append(String.join(", ", subtitle)); + sb.append('}'); + return sb.toString(); + } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/intimpl/MkvFileCollector.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/intimpl/MkvFileCollector.java index 52e86be..3ef774f 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/intimpl/MkvFileCollector.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/intimpl/MkvFileCollector.java @@ -1,7 +1,8 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl; -import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.MkvToolNix; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.log4j.Log4j2; @@ -59,9 +60,9 @@ public class MkvFileCollector implements FileCollector { try { String command = ""; if (System.getProperty("os.name").toLowerCase().contains("windows")) { - command = "\"" + MKVToolProperties.getInstance().getMkvmergePath() + "\""; + command = "\"" + ConfigUtil.getInstance().getPathFor(MkvToolNix.MKV_MERGER) + "\""; } else { - command = MKVToolProperties.getInstance().getMkvmergePath(); + command = ConfigUtil.getInstance().getPathFor(MkvToolNix.MKV_MERGER); } String[] array = new String[]{ command, diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/ConfigUtil.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/ConfigUtil.java index 6aa7956..32ff212 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/ConfigUtil.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/ConfigUtil.java @@ -4,6 +4,9 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.AttributeConfig; import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YamlInvalidContentException; import at.pcgamingfreaks.yaml.YamlKeyNotFoundException; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; import lombok.extern.log4j.Log4j2; import java.io.File; @@ -12,40 +15,87 @@ import java.util.List; import java.util.stream.Collectors; @Log4j2 +@Getter +@Setter public class ConfigUtil { - public static List loadConfig() { - try(YAML yaml = new YAML(new File("config.yaml"))){ - return yaml.getKeysFiltered(".*audio.*").stream() - .sorted() - .map(elem -> elem.replace(".audio", "")) - .map(elem -> createAttributeConfig(elem, yaml)) - .collect(Collectors.toList()); - }catch(YamlInvalidContentException | IOException e){ - log.fatal("Config could not be loaded"); - e.printStackTrace(); + @Getter(AccessLevel.NONE) + @Setter(AccessLevel.NONE) + private static ConfigUtil configUtil = null; + + private List attributeConfig; + private int threadCount; + @Getter(AccessLevel.NONE) + private String mkvtoolnixPath; + private String libraryPath; + + public static ConfigUtil getInstance() { + if(configUtil == null) { + configUtil = new ConfigUtil(); } - return null; + return configUtil; } - private static AttributeConfig createAttributeConfig(String key, YAML yaml) { + public void isValid() throws RuntimeException{ + System.out.println(attributeConfig); + System.out.println(threadCount); + System.out.println(mkvtoolnixPath); + System.out.println(libraryPath); + if (attributeConfig != null && !attributeConfig.isEmpty() + && threadCount > 0 && !mkvtoolnixPath.isEmpty() + && new File(getPathFor(MkvToolNix.MKV_MERGER)).isFile() + && new File(getPathFor(MkvToolNix.MKV_PROP_EDIT)).isFile()) { + return; + } + throw new RuntimeException("Invalid configuration"); + } + + public void loadConfig(String configPath) { + try(YAML config = new YAML(new File(configPath))){ + setAttributeConfig(loadAttributeConfig(config)); + setThreadCount(loadThreadCount(config)); + setMkvtoolnixPath(loadMkvToolNixPath(config)); + }catch(YamlInvalidContentException | YamlKeyNotFoundException | IOException e){ + log.fatal("Config could not be loaded: {}", e.getMessage()); + } + } + + private List loadAttributeConfig(YAML config) { + return config.getKeysFiltered(".*audio.*").stream() + .sorted() + .map(elem -> elem.replace(".audio", "")) + .map(elem -> createAttributeConfig(elem, config)) + .collect(Collectors.toList()); + } + + private AttributeConfig createAttributeConfig(String key, YAML config) { try{ return new AttributeConfig( - yaml.getStringList(key + ".audio"), - yaml.getStringList(key + ".subtitle")); + config.getStringList(key + ".audio"), + config.getStringList(key + ".subtitle")); }catch(YamlKeyNotFoundException e){ e.printStackTrace(); return null; } } - public static int getThreadCount() { - try { - return Integer.parseInt(new YAML(new File("config.yaml")).getString("threadCount")); - } catch (YamlInvalidContentException | IOException e) { - e.printStackTrace(); - } catch (YamlKeyNotFoundException e) { - throw new RuntimeException(e); - } - return 1; + private int loadThreadCount(YAML config) throws YamlKeyNotFoundException{ + return config.isSet("threadCount") + ? Integer.parseInt(config.getString("threadCount")) + : 1; + } + + private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException { + return config.isSet("mkvtoolnixPath") ? config.getString("mkvtoolnixPath") : defaultMkvToolNixPath(); + } + + private String defaultMkvToolNixPath() { + return System.getProperty("os.name").toLowerCase().contains("windows") + ? "C:/Program Files/MKVToolNix/" + : "/usr/bin/"; + } + + public String getPathFor(MkvToolNix exe) { + return mkvtoolnixPath.endsWith("/") ? mkvtoolnixPath + exe : mkvtoolnixPath + "/" + exe; } } + diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/MkvToolNix.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/MkvToolNix.java new file mode 100644 index 0000000..cc2eda6 --- /dev/null +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/util/MkvToolNix.java @@ -0,0 +1,17 @@ +package at.pcgamingfreaks.mkvaudiosubtitlechanger.util; + +public enum MkvToolNix { + MKV_MERGER("mkvmerge.exe"), + MKV_PROP_EDIT("mkvpropedit.exe"); + + private final String file; + + MkvToolNix(String file) { + this.file = file; + } + + @Override + public String toString() { + return file; + } +}