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:
@@ -14,6 +14,8 @@ import java.util.concurrent.*;
|
||||
|
||||
@Log4j2
|
||||
public class AttributeUpdaterKernel {
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(Config.getInstance().getThreadCount());
|
||||
MkvFileCollector collector = new MkvFileCollector();
|
||||
int filesChangedAmount = 0;
|
||||
int filesNotChangedAmount = 0;
|
||||
@@ -21,17 +23,10 @@ public class AttributeUpdaterKernel {
|
||||
|
||||
@SneakyThrows
|
||||
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();
|
||||
if(allValidPaths != null && configPattern != null){
|
||||
System.out.print("Running");
|
||||
allValidPaths.forEach(file -> executor.submit(() -> process(configPattern, file)));
|
||||
}else{
|
||||
log.error("Path is not valid or config has errors!");
|
||||
}
|
||||
|
||||
collector.loadFiles(Config.getInstance().getLibraryPath())
|
||||
.forEach(file -> executor.submit(() -> process(file)));
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(1, TimeUnit.DAYS);
|
||||
runtime = System.currentTimeMillis() - beforeTimer;
|
||||
@@ -45,12 +40,12 @@ public class AttributeUpdaterKernel {
|
||||
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);
|
||||
boolean fileHasChanged = false;
|
||||
|
||||
if (attributes.isEmpty()) return;
|
||||
for(AttributeConfig config : configPattern){
|
||||
for(AttributeConfig config : Config.getInstance().getAttributeConfig()){
|
||||
fileHasChanged = new ConfigProcessor(config).processConfig(file, attributes);
|
||||
if(fileHasChanged) break;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import lombok.extern.log4j.Log4j2;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Log4j2
|
||||
@@ -39,17 +40,26 @@ public class Config {
|
||||
}
|
||||
|
||||
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;
|
||||
boolean isValid = true;
|
||||
if (attributeConfig == null || attributeConfig.isEmpty()
|
||||
|| !attributeConfig.stream().allMatch(AttributeConfig::isValid)) {
|
||||
isValid = false;
|
||||
System.out.println("Audio & subtitle configuration invalid!");
|
||||
}
|
||||
if (threadCount <= 0) {
|
||||
isValid = false;
|
||||
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) {
|
||||
@@ -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()
|
||||
.sorted()
|
||||
.map(elem -> elem.replace(".audio", ""))
|
||||
.map(elem -> createAttributeConfig(elem, config))
|
||||
.map(key -> key.replace(".audio", ""))
|
||||
.map(key -> new AttributeConfig(audio.apply(key), subtitle.apply(key)))
|
||||
.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{
|
||||
return config.isSet("threadCount")
|
||||
? Integer.parseInt(config.getString("threadCount"))
|
||||
@@ -89,7 +91,9 @@ public class Config {
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.intimpl;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||
@@ -14,7 +13,6 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
@@ -154,15 +152,15 @@ public class ConfigProcessor {
|
||||
int subtitleListIndex = - 1;
|
||||
int audioListIndex = - 1;
|
||||
for(FileAttribute elem : attributes){
|
||||
if("audio".equals(elem.getType())){
|
||||
for(int i = 0; i < config.getAudio().size(); i++){
|
||||
audioListIndex = findIndex("audio", elem, audioListIndex, config.getAudio(), transfer);
|
||||
}
|
||||
}else if("subtitles".equals(elem.getType())){
|
||||
for(int i = 0; i < config.getSubtitle().size(); i++){
|
||||
subtitleListIndex = findIndex("subtitles", elem, subtitleListIndex, config.getSubtitle(), transfer);
|
||||
}
|
||||
}
|
||||
// if("audio".equals(elem.getType())){
|
||||
// for(int i = 0; i < config.getAudio().size(); i++){
|
||||
// audioListIndex = findIndex("audio", elem, audioListIndex, config.getAudio(), transfer);
|
||||
// }
|
||||
// }else if("subtitles".equals(elem.getType())){
|
||||
// for(int i = 0; i < config.getSubtitle().size(); i++){
|
||||
// subtitleListIndex = findIndex("subtitles", elem, subtitleListIndex, config.getSubtitle(), transfer);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
transfer.analyzeIfValid();
|
||||
|
||||
@@ -42,10 +42,10 @@ public class MkvFileCollector implements FileCollector {
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
log.error("Couldn't find file or directory!", e);
|
||||
return null;
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,19 +9,23 @@ import java.util.stream.Collectors;
|
||||
@Log4j2
|
||||
@Getter
|
||||
public class AttributeConfig {
|
||||
private List<String> audio;
|
||||
private List<String> subtitle;
|
||||
private String audio;
|
||||
private String subtitle;
|
||||
|
||||
public AttributeConfig(List<String> audio, List<String> subtitle) {
|
||||
public AttributeConfig(String audio, String subtitle) {
|
||||
this.audio = audio;
|
||||
this.subtitle = subtitle;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return audio != null && subtitle != null;
|
||||
}
|
||||
|
||||
@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("audio='").append(audio).append('\'');
|
||||
sb.append(", subtitle='").append(subtitle).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user