mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Add property for file exclusion
This commit is contained in:
@@ -14,10 +14,12 @@ import me.tongfei.progressbar.ProgressBarStyle;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Log4j2
|
||||
public class AttributeUpdaterKernel {
|
||||
@@ -37,7 +39,13 @@ public class AttributeUpdaterKernel {
|
||||
statistic.startTimer();
|
||||
|
||||
try (ProgressBar progressBar = pbBuilder().build()) {
|
||||
List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath());
|
||||
List<File> excludedFiles = Config.getInstance().getExcludedDirectories().stream()
|
||||
.map(collector::loadFiles)
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath()).stream()
|
||||
.filter(file -> !excludedFiles.contains(file))
|
||||
.collect(Collectors.toList());
|
||||
progressBar.maxHint(files.size());
|
||||
files.forEach(file -> executor.submit(() -> process(file, progressBar)));
|
||||
executor.shutdown();
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@Setter
|
||||
public class MKVToolProperties {
|
||||
private String mkvmergePath;
|
||||
private String mkvpropeditPath;
|
||||
|
||||
private static MKVToolProperties instance = null;
|
||||
|
||||
private MKVToolProperties() {
|
||||
}
|
||||
|
||||
public static MKVToolProperties getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new MKVToolProperties();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.apache.commons.cli.*;
|
||||
import java.util.List;
|
||||
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
@Log4j2
|
||||
@@ -20,14 +21,7 @@ public class Main {
|
||||
}
|
||||
|
||||
private static void initConfig(String[] args) {
|
||||
Options options = new Options();
|
||||
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"));
|
||||
|
||||
Options options = initOptions();
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
try {
|
||||
@@ -39,12 +33,14 @@ public class Main {
|
||||
}
|
||||
|
||||
Config config = Config.getInstance();
|
||||
config.loadConfig(cmd.getOptionValue(CONFIG.toString(), "config.yaml")); // use cmd input
|
||||
config.loadConfig(cmd.getOptionValue(CONFIG_PATH.prop(), "config.yaml")); // use cmd input
|
||||
config.setLibraryPath(cmd.getOptionValue("library"));
|
||||
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())));
|
||||
if (cmd.hasOption(FORCED_KEYWORDS.prop()))
|
||||
config.getForcedKeywords().addAll(List.of(cmd.getOptionValues(FORCED_KEYWORDS.prop())));
|
||||
if (cmd.hasOption(EXCLUDE_DIRECTORY.prop()))
|
||||
config.getExcludedDirectories().addAll(List.of(cmd.getOptionValues(EXCLUDE_DIRECTORY.prop())));
|
||||
config.isValid();
|
||||
} catch (ParseException e) {
|
||||
log.error(e);
|
||||
@@ -53,10 +49,15 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
private static Options initOptions() {
|
||||
Options options = new Options();
|
||||
options.addOption(optionOf(HELP, "h", false));
|
||||
options.addOption(optionOf(LIBRARY, "l", true, true));
|
||||
options.addOption(optionOf(CONFIG_PATH, "c", false));
|
||||
options.addOption(optionOf(THREADS, "t", true));
|
||||
options.addOption(optionOf(SAFE_MODE, "s", false));
|
||||
options.addOption(optionOf(FORCED_KEYWORDS, "k", Option.UNLIMITED_VALUES, false));
|
||||
options.addOption(optionOf(EXCLUDE_DIRECTORY, "e", Option.UNLIMITED_VALUES, false));
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ public class Config {
|
||||
private List<AttributeConfig> attributeConfig;
|
||||
private int threadCount;
|
||||
private Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs"));
|
||||
private Set<String> excludedDirectories = new HashSet<>();
|
||||
@Getter(AccessLevel.NONE)
|
||||
private String mkvtoolnixPath;
|
||||
private String libraryPath;
|
||||
@@ -73,6 +74,7 @@ public class Config {
|
||||
setMkvtoolnixPath(loadMkvToolNixPath(config));
|
||||
setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
|
||||
getForcedKeywords().addAll(loadForcedKeywords(config));
|
||||
getExcludedDirectories().addAll(loadExcludeDirectories(config));
|
||||
} catch (YamlInvalidContentException | YamlKeyNotFoundException | IOException e) {
|
||||
log.fatal("Config could not be loaded: {}", e.getMessage());
|
||||
}
|
||||
@@ -90,18 +92,22 @@ public class Config {
|
||||
}
|
||||
|
||||
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException {
|
||||
return config.isSet(ConfigProperty.THREADS.toString())
|
||||
? Integer.parseInt(config.getString(ConfigProperty.THREADS.toString()))
|
||||
return config.isSet(ConfigProperty.THREADS.prop())
|
||||
? Integer.parseInt(config.getString(ConfigProperty.THREADS.prop()))
|
||||
: 1;
|
||||
}
|
||||
|
||||
private List<String> loadForcedKeywords(YAML config) {
|
||||
return config.getStringList(ConfigProperty.FORCED_KEYWORDS.toString(), new ArrayList<>());
|
||||
return config.getStringList(ConfigProperty.FORCED_KEYWORDS.prop(), new ArrayList<>());
|
||||
}
|
||||
|
||||
private List<String> loadExcludeDirectories(YAML config) {
|
||||
return config.getStringList(ConfigProperty.EXCLUDE_DIRECTORY.prop(), new ArrayList<>());
|
||||
}
|
||||
|
||||
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
|
||||
return config.isSet(MKV_TOOL_NIX.toString())
|
||||
? config.getString(MKV_TOOL_NIX.toString())
|
||||
return config.isSet(MKV_TOOL_NIX.prop())
|
||||
? config.getString(MKV_TOOL_NIX.prop())
|
||||
: defaultMkvToolNixPath();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ public class MkvFileCollector implements FileCollector {
|
||||
public List<File> loadFiles(String path) {
|
||||
File file = new File(path);
|
||||
if (file.isFile() && file.getAbsolutePath().endsWith(".mkv")) {
|
||||
return new ArrayList<File>() {{
|
||||
return new ArrayList<>() {{
|
||||
add(file);
|
||||
}};
|
||||
} else if (file.isDirectory()) {
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
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");
|
||||
MKV_TOOL_NIX("mkvtoolnixPath", "Path to mkv tool nix installation"),
|
||||
THREADS("threads", "thread count"),
|
||||
FORCED_KEYWORDS("forcedKeywords", "Additional keywords to identify forced tracks\""),
|
||||
CONFIG_PATH("config", "path to config"),
|
||||
LIBRARY("library", "path to library"),
|
||||
SAFE_MODE("safe-mode", "Test run (no files will be changes)"),
|
||||
HELP("help", "\"for help this is\" - Yoda"),
|
||||
EXCLUDE_DIRECTORY("exclude-directories", "Directories to exclude");
|
||||
|
||||
private final String property;
|
||||
private final String description;
|
||||
|
||||
ConfigProperty(String property) {
|
||||
ConfigProperty(String property, String description) {
|
||||
this.property = property;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
public String desc() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String prop() {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
|
||||
import org.apache.commons.cli.Option;
|
||||
|
||||
public class CommandLineOptionsUtil {
|
||||
public static Option optionOf(ConfigProperty property, String opt, boolean hasArg) {
|
||||
return optionOf(property, opt, hasArg ? 1 : 0, false);
|
||||
}
|
||||
|
||||
public static Option optionOf(ConfigProperty property, String opt, boolean hasArg, boolean required) {
|
||||
return optionOf(property, opt, hasArg ? 1 : 0, required);
|
||||
}
|
||||
|
||||
public static Option optionOf(ConfigProperty property, String opt, int args, boolean required) {
|
||||
Option option = new Option(opt, property.desc());
|
||||
option.setArgs(args);
|
||||
option.setLongOpt(property.prop());
|
||||
option.setRequired(required);
|
||||
return option;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user