From f928cb035e43ef146a48824b7e9d3e17c80abcfc Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Mon, 10 Apr 2023 21:44:43 +0200 Subject: [PATCH 1/4] Update config template --- config-template.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config-template.yaml b/config-template.yaml index 2cc5d38..df5b473 100644 --- a/config-template.yaml +++ b/config-template.yaml @@ -1,7 +1,7 @@ mkvtoolnix: C:\Program Files\MKVToolNix library: X:/Files -config: +attribute-config: 1: audio: ger subtitle: OFF @@ -14,6 +14,7 @@ config: #forced-keywords: ["forced", "signs"] #commentary-keywords: ["commentary", "director"] +#preferred-subtitles: ["unstyled"] #exclude-directories: # - "D:/Path/To/File.mkv" @@ -22,7 +23,12 @@ config: # If pattern is negated, can be used to exclude files #include-pattern: "regex" +# Only files newer than +#filter-date: 20.03.2021-10:11:12 + safe-mode: #coherent: +#force-coherent: +#only-new-files: From e19f780ff0aa4ea274b3c809e1a4a90c80d4f1c7 Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Thu, 13 Apr 2023 21:57:59 +0200 Subject: [PATCH 2/4] Update readme --- README.md | 35 +++++++++++-------- .../config/ConfigLoader.java | 4 +-- .../impl/FileProcessor.java | 7 ++-- .../model/ResultStatistic.java | 3 +- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 9bcdb83..cc6925a 100644 --- a/README.md +++ b/README.md @@ -19,23 +19,28 @@ Attribute-config must be entered in pairs: `audio:subtitle`; Example: `jpn:eng`. [here](https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/wiki/Attribute-Config). ## Available parameters -```shell - -l,--library Path to library - -a,--attribute-config Attribute config to decide which tracks to choose when - -p,--config-path Path to config file - -m,--mkvtoolnix Path to mkv tool nix installation - -s,--safe-mode Test run (no files will be changes) - -t,--threads Thread count (default: 2) - -i,--include-pattern Include files matching pattern (default: ".*") - -e,--exclude-directories Directories to be excluded, combines with config file - -fk,--forced-keywords Additional keywords to identify forced tracks - -ck,--commentary-keywords Additional keywords to identify commentary tracks - -v,--version Display version - -h,--help "For help this is" - Yoda ``` + -l,--library-path Path to library + -a,--attribute-config Attribute config to decide which tracks to choose when + -p,--config-path Path to config file + -m,--mkvtoolnix Path to mkv tool nix installation + -s,--safe-mode Test run (no files will be changes) + -c,--coherent Try to match all files in dir of depth with the same config + -cf,--force-coherent Force coherent and don't update anything if config fits not whole config (default: false) + -n,--only-new-files Sets filter-date to last successful execution (Overwrites input of filter-date) + -d,--filter-date Only consider files created newer than entered date (format: "dd.MM.yyyy-HH:mm:ss") + -t,--threads Thread count (default: 2) + -i,--include-pattern Include files matching pattern (default: ".*") + -e,--excluded-directories Directories to be excluded, combines with config file + -fk,--forced-keywords Additional keywords to identify forced tracks + -ck,--commentary-keywords Additional keywords to identify commentary tracks + -ps,--preferred-subtitles Additional keywords to prefer specific subtitle tracks + -v,--version Display version + -h,--help "For help this is" - Yoda +``` +If you need more information about how each parameter works, check out [this wiki page](https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/wiki/Parameters). -All parameters can also be defined in a config file. -Please read [this wiki page](https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/wiki/How-to-config-file) for more information. +All parameters can also be defined in a [config file](https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/wiki/How-to-config-file). ## Build requirements - JDK 11 or higher diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java index 176a908..bf48362 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/config/ConfigLoader.java @@ -84,7 +84,7 @@ public class ConfigLoader { if (cmd == null) throw new NullPointerException(); return cmd; } catch (ParseException | NullPointerException e) { - formatter.printHelp(106, "java -jar MKVAudioSubtitlesChanger.jar -l ", + formatter.printHelp(130, "java -jar MKVAudioSubtitlesChanger.jar -l ", "\nParameters:", options, "\nFeature requests and bug reports: https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/issues"); System.exit(1); @@ -94,7 +94,7 @@ public class ConfigLoader { private static void exitIfHelp(CommandLine cmd, Options options, HelpFormatter formatter) { if (cmd.hasOption("help")) { - formatter.printHelp(106, "java -jar MKVAudioSubtitlesChanger.jar -l ", + formatter.printHelp(130, "java -jar MKVAudioSubtitlesChanger.jar -l ", "\nParameters:", options, "\nFeature requests and bug reports: https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/issues"); System.exit(0); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileProcessor.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileProcessor.java index 4364558..6619f59 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileProcessor.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileProcessor.java @@ -30,9 +30,8 @@ public interface FileProcessor { /** * Populate FileInfoDto with the desired tracks, based on AttributeConfig. * @param info to be populated - * @param nonForcedTracks List of all not forced tracks - * @param nonCommentaryTracks List of all not commentary tracks - * @param configs + * @param nonForcedTracks List of all non-forced tracks + * @param nonCommentaryTracks List of all non-commentary tracks */ void detectDesiredTracks(FileInfoDto info, List nonForcedTracks, List nonCommentaryTracks, AttributeConfig... configs); @@ -45,7 +44,7 @@ public interface FileProcessor { * Update the file. * @param file to be updated * @param fileInfo information to update file - * @throws IOException + * @throws IOException when error occurs accessing file retrieving information * @throws MkvToolNixException when error occurs while sending query to mkvpropedit */ void update(File file, FileInfoDto fileInfo) throws IOException, MkvToolNixException; diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java index 6370aa7..98e4dfa 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java @@ -116,7 +116,7 @@ public class ResultStatistic { @Override public String toString() { - String sb = "ResultStatistic[" + "filesTotal=" + filesTotal + + return "ResultStatistic[" + "filesTotal=" + filesTotal + ", excluded=" + excluded + ", shouldChange=" + shouldChange + " (failedChanging=" + failedChanging + @@ -126,6 +126,5 @@ public class ResultStatistic { ", failed=" + failed + ", runtime=" + formatTimer() + ']'; - return sb; } } From 686a9a0da113d697a96c8585fb8d641df982b04c Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Sun, 16 Apr 2023 13:07:58 +0200 Subject: [PATCH 3/4] Implement caching & fix minor bugs --- .../mkvaudiosubtitlechanger/Main.java | 6 ++-- .../mkvaudiosubtitlechanger/impl/Cache.java | 19 ++++++++++++ .../impl/CachedMkvFileProcessor.java | 15 ++++++++++ .../impl/FileFilter.java | 1 + .../impl/kernel/AttributeUpdaterKernel.java | 2 +- .../CoherentAttributeUpdaterKernel.java | 30 +++++++++---------- .../model/ResultStatistic.java | 5 ++-- 7 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java create mode 100644 src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java index db85668..a746649 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java @@ -2,11 +2,11 @@ package at.pcgamingfreaks.mkvaudiosubtitlechanger; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config; import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.ConfigLoader; +import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.CachedMkvFileProcessor; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.AttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.CoherentAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.kernel.DefaultAttributeUpdaterKernel; import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileCollector; -import at.pcgamingfreaks.mkvaudiosubtitlechanger.impl.MkvFileProcessor; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -14,8 +14,8 @@ public class Main { public static void main(String[] args) { ConfigLoader.initConfig(args); AttributeUpdaterKernel kernel = Config.getInstance().getCoherent() != null - ? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor()) - : new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new MkvFileProcessor()); + ? new CoherentAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor()) + : new DefaultAttributeUpdaterKernel(new MkvFileCollector(), new CachedMkvFileProcessor()); kernel.execute(); } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java new file mode 100644 index 0000000..83a46be --- /dev/null +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/Cache.java @@ -0,0 +1,19 @@ +package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +public class Cache { + private final Map cache = new HashMap<>(); + + /** + * Retrieve {@link Value} from Cache or run creationFunction and return its value. + * @param key key of cache map + * @param creationFunction function to create missing values + * @return {@link Value} from Cache, or if missing result from creationFunction. + */ + public synchronized Value retrieve(Key key, Function creationFunction) { + return cache.computeIfAbsent(key, creationFunction::apply); + } +} diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java new file mode 100644 index 0000000..acd9198 --- /dev/null +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CachedMkvFileProcessor.java @@ -0,0 +1,15 @@ +package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl; + +import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; + +import java.io.File; +import java.util.List; + +public class CachedMkvFileProcessor extends MkvFileProcessor { + Cache> cache = new Cache<>(); + + @Override + public List loadAttributes(File file) { + return cache.retrieve(file, super::loadAttributes); + } +} diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java index 20b5c21..5edfe9b 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/FileFilter.java @@ -21,6 +21,7 @@ public class FileFilter { return true; } + ResultStatistic.getInstance().total(); ResultStatistic.getInstance().excluded(); return false; } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java index 9a1be4f..bb31a33 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/AttributeUpdaterKernel.java @@ -115,6 +115,7 @@ public abstract class AttributeUpdaterKernel { * @param fileInfoDto contains information about file and desired configuration. */ protected void updateFile(FileInfoDto fileInfoDto) { + statistic.total(); switch (fileInfoDto.getStatus()) { case CHANGE_NECESSARY: statistic.shouldChange(); @@ -139,7 +140,6 @@ public abstract class AttributeUpdaterKernel { } try { - statistic.total(); processor.update(fileInfo.getFile(), fileInfo); statistic.success(); log.info("Updated {}", fileInfo.getFile().getAbsolutePath()); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java index 4fe1cc7..bc9eac5 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java @@ -34,8 +34,12 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { */ @Override List loadFiles(String path) { + return loadFiles(path, Config.getInstance().getCoherent()); + } + + List loadFiles(String path, int depth) { List excludedFiles = loadExcludedFiles(); - List directories = collector.loadDirectories(path, Config.getInstance().getCoherent()) + List directories = collector.loadDirectories(path, depth) .stream().filter(file -> !excludedFiles.contains(file)) .collect(Collectors.toList()); return directories.stream() @@ -59,25 +63,20 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { */ @Override void process(File file) { + process(file, Config.getInstance().getCoherent()); + } + + void process(File file, int depth) { // TODO: Implement level crawl if coherence is not possible on user entered depth // IMPL idea: recursive method call, cache needs to be implemented - List fileInfos = collector.loadFiles(file.getAbsolutePath()) - .stream().map(FileInfoDto::new) + List fileInfos = collector.loadFiles(file.getAbsolutePath()).stream() + .map(FileInfoDto::new) .collect(Collectors.toList()); - Map> fileAttributeCache = new HashMap<>(); - for (FileInfoDto fileInfo : fileInfos) { - if (!Config.getInstance().getIncludePattern().matcher(fileInfo.getFile().getAbsolutePath()).matches()) { - statistic.excluded(); - continue; - } - fileAttributeCache.put(fileInfo, processor.loadAttributes(fileInfo.getFile())); - } - for (AttributeConfig config : Config.getInstance().getAttributeConfig()) { for (FileInfoDto fileInfo : fileInfos) { - List attributes = fileAttributeCache.get(fileInfo); + List attributes = processor.loadAttributes(fileInfo.getFile()); List nonForcedTracks = processor.retrieveNonForcedTracks(attributes); List nonCommentaryTracks = processor.retrieveNonCommentaryTracks(attributes); @@ -88,7 +87,6 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { if (fileInfos.stream().allMatch(elem -> elem.getDesiredSubtitleLane() != null && elem.getDesiredAudioLane() != null)) { log.info("Found {}/{} match for {}", config.getAudioLanguage(), config.getSubtitleLanguage(), file.getAbsolutePath()); - statistic.increaseTotalBy(fileInfos.size()); fileInfos.forEach(this::updateFile); return; // match found, end process here } @@ -99,9 +97,11 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { }); } + log.info("No coherent match found for {}", file.getAbsoluteFile()); + for (FileInfoDto fileInfo : fileInfos) { statistic.total(); - if (Config.getInstance().isForceCoherent()) { + if (!Config.getInstance().isForceCoherent()) { super.process(fileInfo.getFile()); } else { statistic.excluded(); diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java index 98e4dfa..8147625 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/model/ResultStatistic.java @@ -116,7 +116,7 @@ public class ResultStatistic { @Override public String toString() { - return "ResultStatistic[" + "filesTotal=" + filesTotal + + return "ResultStatistic: " + "filesTotal=" + filesTotal + ", excluded=" + excluded + ", shouldChange=" + shouldChange + " (failedChanging=" + failedChanging + @@ -124,7 +124,6 @@ public class ResultStatistic { "), noSuitableConfigFound=" + noSuitableConfigFound + ", alreadyFits=" + alreadyFits + ", failed=" + failed + - ", runtime=" + formatTimer() + - ']'; + ", runtime=" + formatTimer(); } } From 1963d1cc5c40809ea30ec2bb5ac528ef8ca7d394 Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Sun, 16 Apr 2023 13:45:46 +0200 Subject: [PATCH 4/4] Fix total file count --- pom.xml | 20 +++++++++++++------ .../CoherentAttributeUpdaterKernel.java | 1 - 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index d641cfe..e07bc3b 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,7 @@ org.apache.maven.plugins maven-jar-plugin - 3.1.1 + 3.2.2 @@ -47,7 +47,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.2.1 + 3.4.1 package @@ -62,6 +62,13 @@ *:* + + + + true + + + @@ -124,17 +131,18 @@ org.apache.logging.log4j log4j-api - 2.17.1 + 2.18.0 org.apache.logging.log4j log4j-core - 2.17.1 + 2.18.0 + org.apache.logging.log4j - log4j-slf4j-impl - 2.17.1 + log4j-slf4j18-impl + 2.18.0 com.fasterxml.jackson.dataformat diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java index bc9eac5..9ade781 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/kernel/CoherentAttributeUpdaterKernel.java @@ -100,7 +100,6 @@ public class CoherentAttributeUpdaterKernel extends AttributeUpdaterKernel { log.info("No coherent match found for {}", file.getAbsoluteFile()); for (FileInfoDto fileInfo : fileInfos) { - statistic.total(); if (!Config.getInstance().isForceCoherent()) { super.process(fileInfo.getFile()); } else {