mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 10:05:58 +01:00
Change AttributeConfig to only hold one audio & subtitle lane
This commit is contained in:
20
config.yaml
20
config.yaml
@@ -3,14 +3,14 @@ mkvtoolnixPath: C:\Program Files\MKVToolNix
|
|||||||
threadCount: 2
|
threadCount: 2
|
||||||
config:
|
config:
|
||||||
1:
|
1:
|
||||||
audio:
|
audio: jpn
|
||||||
- jpn
|
subtitle: ger
|
||||||
subtitle:
|
|
||||||
- ger
|
|
||||||
- eng
|
|
||||||
2:
|
2:
|
||||||
audio:
|
audio: jpn
|
||||||
- ger
|
subtitle: eng
|
||||||
- eng
|
3:
|
||||||
subtitle:
|
audio: ger
|
||||||
- OFF
|
subtitle: OFF
|
||||||
|
4:
|
||||||
|
audio: eng
|
||||||
|
subtitle: OFF
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import java.util.concurrent.*;
|
|||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class AttributeUpdaterKernel {
|
public class AttributeUpdaterKernel {
|
||||||
|
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreadCount());
|
||||||
MkvFileCollector collector = new MkvFileCollector();
|
MkvFileCollector collector = new MkvFileCollector();
|
||||||
int filesChangedAmount = 0;
|
int filesChangedAmount = 0;
|
||||||
int filesNotChangedAmount = 0;
|
int filesNotChangedAmount = 0;
|
||||||
@@ -21,17 +23,10 @@ public class AttributeUpdaterKernel {
|
|||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public void execute() {
|
public void execute() {
|
||||||
List<AttributeConfig> configPattern = Config.getInstance().getAttributeConfig();
|
|
||||||
List<File> allValidPaths = collector.loadFiles(Config.getInstance().getLibraryPath());
|
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreadCount());
|
|
||||||
|
|
||||||
long beforeTimer = System.currentTimeMillis();
|
long beforeTimer = System.currentTimeMillis();
|
||||||
if(allValidPaths != null && configPattern != null){
|
|
||||||
System.out.print("Running");
|
collector.loadFiles(Config.getInstance().getLibraryPath())
|
||||||
allValidPaths.forEach(file -> executor.submit(() -> process(configPattern, file)));
|
.forEach(file -> executor.submit(() -> process(file)));
|
||||||
}else{
|
|
||||||
log.error("Path is not valid or config has errors!");
|
|
||||||
}
|
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
executor.awaitTermination(1, TimeUnit.DAYS);
|
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||||
runtime = System.currentTimeMillis() - beforeTimer;
|
runtime = System.currentTimeMillis() - beforeTimer;
|
||||||
@@ -45,12 +40,12 @@ public class AttributeUpdaterKernel {
|
|||||||
System.out.printf("Runtime: %ss%n", runtime / 1000);
|
System.out.printf("Runtime: %ss%n", runtime / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void process(List<AttributeConfig> configPattern, File file) {
|
private void process(File file) {
|
||||||
List<FileAttribute> attributes = collector.loadAttributes(file);
|
List<FileAttribute> attributes = collector.loadAttributes(file);
|
||||||
boolean fileHasChanged = false;
|
boolean fileHasChanged = false;
|
||||||
|
|
||||||
if (attributes.isEmpty()) return;
|
if (attributes.isEmpty()) return;
|
||||||
for(AttributeConfig config : configPattern){
|
for(AttributeConfig config : Config.getInstance().getAttributeConfig()){
|
||||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, attributes);
|
fileHasChanged = new ConfigProcessor(config).processConfig(file, attributes);
|
||||||
if(fileHasChanged) break;
|
if(fileHasChanged) break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ 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.List;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@@ -39,17 +40,26 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void isValid() throws RuntimeException{
|
public void isValid() throws RuntimeException{
|
||||||
System.out.println(attributeConfig);
|
boolean isValid = true;
|
||||||
System.out.println(threadCount);
|
if (attributeConfig == null || attributeConfig.isEmpty()
|
||||||
System.out.println(mkvtoolnixPath);
|
|| !attributeConfig.stream().allMatch(AttributeConfig::isValid)) {
|
||||||
System.out.println(libraryPath);
|
isValid = false;
|
||||||
if (attributeConfig != null && !attributeConfig.isEmpty()
|
System.out.println("Audio & subtitle configuration invalid!");
|
||||||
&& threadCount > 0 && !mkvtoolnixPath.isEmpty()
|
}
|
||||||
&& new File(getPathFor(MkvToolNix.MKV_MERGER)).isFile()
|
if (threadCount <= 0) {
|
||||||
&& new File(getPathFor(MkvToolNix.MKV_PROP_EDIT)).isFile()) {
|
isValid = false;
|
||||||
return;
|
System.out.println("Thread count needs to be at least 1!");
|
||||||
|
}
|
||||||
|
if (mkvtoolnixPath.isEmpty()
|
||||||
|
|| !new File(getPathFor(MkvToolNix.MKV_MERGER)).isFile()
|
||||||
|
|| !new File(getPathFor(MkvToolNix.MKV_PROP_EDIT)).isFile()) {
|
||||||
|
isValid = false;
|
||||||
|
System.out.println("MkvToolNix installation path invalid!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValid) {
|
||||||
|
throw new RuntimeException("Invalid configuration");
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Invalid configuration");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadConfig(String configPath) {
|
public void loadConfig(String configPath) {
|
||||||
@@ -63,25 +73,17 @@ public class Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
return config.getKeysFiltered(".*audio.*").stream()
|
return config.getKeysFiltered(".*audio.*").stream()
|
||||||
.sorted()
|
.sorted()
|
||||||
.map(elem -> elem.replace(".audio", ""))
|
.map(key -> key.replace(".audio", ""))
|
||||||
.map(elem -> createAttributeConfig(elem, config))
|
.map(key -> new AttributeConfig(audio.apply(key), subtitle.apply(key)))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private AttributeConfig createAttributeConfig(String key, YAML config) {
|
|
||||||
try{
|
|
||||||
return new AttributeConfig(
|
|
||||||
config.getStringList(key + ".audio"),
|
|
||||||
config.getStringList(key + ".subtitle"));
|
|
||||||
}catch(YamlKeyNotFoundException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException{
|
private int loadThreadCount(YAML config) throws YamlKeyNotFoundException{
|
||||||
return config.isSet("threadCount")
|
return config.isSet("threadCount")
|
||||||
? Integer.parseInt(config.getString("threadCount"))
|
? Integer.parseInt(config.getString("threadCount"))
|
||||||
@@ -89,7 +91,9 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
|
private String loadMkvToolNixPath(YAML config) throws YamlKeyNotFoundException {
|
||||||
return config.isSet("mkvtoolnixPath") ? config.getString("mkvtoolnixPath") : defaultMkvToolNixPath();
|
return config.isSet("mkvtoolnixPath")
|
||||||
|
? config.getString("mkvtoolnixPath")
|
||||||
|
: defaultMkvToolNixPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String defaultMkvToolNixPath() {
|
private String defaultMkvToolNixPath() {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
|
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
|
||||||
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties;
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||||
@@ -14,7 +13,6 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
|
|
||||||
@@ -154,15 +152,15 @@ public class ConfigProcessor {
|
|||||||
int subtitleListIndex = - 1;
|
int subtitleListIndex = - 1;
|
||||||
int audioListIndex = - 1;
|
int audioListIndex = - 1;
|
||||||
for(FileAttribute elem : attributes){
|
for(FileAttribute elem : attributes){
|
||||||
if("audio".equals(elem.getType())){
|
// if("audio".equals(elem.getType())){
|
||||||
for(int i = 0; i < config.getAudio().size(); i++){
|
// for(int i = 0; i < config.getAudio().size(); i++){
|
||||||
audioListIndex = findIndex("audio", elem, audioListIndex, config.getAudio(), transfer);
|
// audioListIndex = findIndex("audio", elem, audioListIndex, config.getAudio(), transfer);
|
||||||
}
|
// }
|
||||||
}else if("subtitles".equals(elem.getType())){
|
// }else if("subtitles".equals(elem.getType())){
|
||||||
for(int i = 0; i < config.getSubtitle().size(); i++){
|
// for(int i = 0; i < config.getSubtitle().size(); i++){
|
||||||
subtitleListIndex = findIndex("subtitles", elem, subtitleListIndex, config.getSubtitle(), transfer);
|
// subtitleListIndex = findIndex("subtitles", elem, subtitleListIndex, config.getSubtitle(), transfer);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer.analyzeIfValid();
|
transfer.analyzeIfValid();
|
||||||
|
|||||||
@@ -42,10 +42,10 @@ public class MkvFileCollector implements FileCollector {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Couldn't find file or directory!", e);
|
log.error("Couldn't find file or directory!", e);
|
||||||
return null;
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,19 +9,23 @@ import java.util.stream.Collectors;
|
|||||||
@Log4j2
|
@Log4j2
|
||||||
@Getter
|
@Getter
|
||||||
public class AttributeConfig {
|
public class AttributeConfig {
|
||||||
private List<String> audio;
|
private String audio;
|
||||||
private List<String> subtitle;
|
private String subtitle;
|
||||||
|
|
||||||
public AttributeConfig(List<String> audio, List<String> subtitle) {
|
public AttributeConfig(String audio, String subtitle) {
|
||||||
this.audio = audio;
|
this.audio = audio;
|
||||||
this.subtitle = subtitle;
|
this.subtitle = subtitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isValid() {
|
||||||
|
return audio != null && subtitle != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuffer sb = new StringBuffer("AttributeConfig{");
|
final StringBuffer sb = new StringBuffer("AttributeConfig{");
|
||||||
sb.append("audio=").append(String.join(", ", audio));
|
sb.append("audio='").append(audio).append('\'');
|
||||||
sb.append(", subtitle=").append(String.join(", ", subtitle));
|
sb.append(", subtitle='").append(subtitle).append('\'');
|
||||||
sb.append('}');
|
sb.append('}');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user