mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 10:05:58 +01:00
Restructure files & remove unused code & simplify few statements
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
public class AttributeConfig {
|
||||
private List<String> audio;
|
||||
private List<String> subtitle;
|
||||
|
||||
public AttributeConfig(List<String> audio, List<String> subtitle) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.AttributeConfig;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
|
||||
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;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@Setter
|
||||
public class Config {
|
||||
@Getter(AccessLevel.NONE)
|
||||
@Setter(AccessLevel.NONE)
|
||||
private static Config config = null;
|
||||
|
||||
private List<AttributeConfig> attributeConfig;
|
||||
private int threadCount;
|
||||
@Getter(AccessLevel.NONE)
|
||||
private String mkvtoolnixPath;
|
||||
private String libraryPath;
|
||||
private boolean isSafeMode;
|
||||
private boolean isWindows;
|
||||
|
||||
public static Config getInstance() {
|
||||
if(config == null) {
|
||||
config = new Config();
|
||||
}
|
||||
return 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;
|
||||
}
|
||||
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));
|
||||
setWindows(System.getProperty("os.name").toLowerCase().contains("windows"));
|
||||
}catch(YamlInvalidContentException | YamlKeyNotFoundException | IOException e){
|
||||
log.fatal("Config could not be loaded: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private List<AttributeConfig> 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(
|
||||
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"))
|
||||
: 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,202 +0,0 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ConfigUtil;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
@Log4j2
|
||||
public class ConfigProcessor {
|
||||
private int audioDefault = - 1;
|
||||
private int subtitleDefault = - 1;
|
||||
private final AttributeConfig config;
|
||||
|
||||
public ConfigProcessor(AttributeConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the config lists and apply the changes if the combination matches
|
||||
*
|
||||
* @param file is the file, which will be updated
|
||||
* @param attributes has the metadata for the transferred file
|
||||
* @return If the current configuration matched and changes applied or not
|
||||
*/
|
||||
public boolean processConfig(File file, List<FileAttribute> attributes) {
|
||||
// check if size is bigger or equal 2 to make sure that there is at least one audio and subtitle line
|
||||
// TODO: implement empty audio or subtitle line
|
||||
List<FileAttribute> attributesCopy = new ArrayList<>(attributes);
|
||||
if(attributesCopy.size() >= 2){
|
||||
TransferObject transfer = filterAttributes(attributesCopy);
|
||||
if(! attributesCopy.isEmpty()){
|
||||
return updateFile(file, attributesCopy, transfer);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Filters the attributes that only those are remaining which are needed in the current configuration.
|
||||
* Also analyzes which tracks were default before.
|
||||
*/
|
||||
private TransferObject filterAttributes(List<FileAttribute> attributes) {
|
||||
TransferObject transfer = new TransferObject();
|
||||
Iterator<FileAttribute> iterator = attributes.iterator();
|
||||
while(iterator.hasNext()){
|
||||
FileAttribute elem = iterator.next();
|
||||
if("audio".equals(elem.getType())){
|
||||
if(elem.isDefaultTrack()){
|
||||
audioDefault = elem.getId();
|
||||
}
|
||||
if(config.getAudio().contains("OFF")){
|
||||
transfer.setAudioOn(false);
|
||||
transfer.setAudioIndex(- 2);
|
||||
}
|
||||
if(! config.getAudio().contains(elem.getLanguage())){
|
||||
iterator.remove();
|
||||
}
|
||||
}else if("subtitles".equals(elem.getType())){
|
||||
if(elem.isDefaultTrack()){
|
||||
subtitleDefault = elem.getId();
|
||||
}
|
||||
if(config.getSubtitle().contains("OFF")){
|
||||
transfer.setSubtitleOn(false);
|
||||
transfer.setSubtitleIndex(- 2);
|
||||
}
|
||||
if(! config.getSubtitle().contains(elem.getLanguage())){
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
return transfer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the command which will be executed if the attributes of the current file fit the current {@link AttributeConfig}
|
||||
*
|
||||
* @param file is the file, which will be updated
|
||||
* @param attributes has the metadata for the transferred file
|
||||
* @return if the current file was updated or not. Returns true if the file already has the correct metadata set
|
||||
*/
|
||||
private boolean updateFile(File file, List<FileAttribute> attributes, TransferObject transfer) {
|
||||
StringBuilder stringBuffer = new StringBuilder();
|
||||
if(System.getProperty("os.name").toLowerCase().contains("windows")){
|
||||
stringBuffer.append(format("\"%s\" \"%s\" ",
|
||||
MKVToolProperties.getInstance().getMkvpropeditPath(),
|
||||
file.getAbsolutePath()));
|
||||
}else{
|
||||
stringBuffer.append(format("%s %s ",
|
||||
MKVToolProperties.getInstance().getMkvpropeditPath(),
|
||||
file.getAbsolutePath()));
|
||||
}
|
||||
if(audioDefault != - 1){
|
||||
stringBuffer.append(format("--edit track:=%s --set flag-default=0 ", audioDefault));
|
||||
}
|
||||
if(subtitleDefault != - 1){
|
||||
stringBuffer.append(format("--edit track:=%s --set flag-default=0 ", subtitleDefault));
|
||||
}
|
||||
collectLines(attributes, transfer);
|
||||
if(transfer.isValid){
|
||||
if(transfer.isAudioOn){
|
||||
stringBuffer.append(format("--edit track:=%s --set flag-default=1 ", transfer.getAudioIndex()));
|
||||
}
|
||||
if(transfer.isSubtitleOn){
|
||||
stringBuffer.append(format("--edit track:=%s --set flag-default=1 ", transfer.getSubtitleIndex()));
|
||||
}
|
||||
if(subtitleDefault == transfer.getSubtitleIndex() && audioDefault == transfer.getAudioIndex()){
|
||||
/*
|
||||
* In this case the file would be change to the exact same audio and subtitle lines and we want to
|
||||
* avoid unnecessary changes to the file
|
||||
*/
|
||||
log.info("File already fits config: {}", file.getName());
|
||||
return true;
|
||||
}
|
||||
try{
|
||||
if(!ConfigUtil.getInstance().isSafeMode()) {
|
||||
Runtime.getRuntime().exec(stringBuffer.toString());
|
||||
}
|
||||
}catch(IOException e){
|
||||
log.error("Couldn't make changes to file");
|
||||
}
|
||||
/*
|
||||
* We return true even if there was an error. If there was an error, the chances that this file is still
|
||||
* busy later.
|
||||
*/
|
||||
log.info("Updated {}", file.getName());
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyzes the left over attributes and decides which is the most wanted for audio and subtitle
|
||||
*
|
||||
* @param attributes contains all the leftover attributes
|
||||
* @return is an object, which contains information about which audio and subtitle line is the best suitable for
|
||||
* the entered config. Also transfers a boolean which contains information about if the other two values
|
||||
* were set
|
||||
*/
|
||||
private TransferObject collectLines(List<FileAttribute> attributes, TransferObject transfer) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transfer.analyzeIfValid();
|
||||
return transfer;
|
||||
}
|
||||
|
||||
private int findIndex(String type, FileAttribute elem, int index, List<String> config, TransferObject transfer) {
|
||||
for(int i = 0; i < config.size(); i++){
|
||||
if(config.get(i).equals(elem.getLanguage()) && (index == - 1 || i < index)){
|
||||
switch(type){
|
||||
case "audio":
|
||||
transfer.setAudioIndex(elem.getId());
|
||||
break;
|
||||
case "subtitles":
|
||||
transfer.setSubtitleIndex(elem.getId());
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private static class TransferObject {
|
||||
private boolean isValid;
|
||||
private int audioIndex = - 1;
|
||||
private int subtitleIndex = - 1;
|
||||
private boolean isSubtitleOn = true;
|
||||
private boolean isAudioOn = true;
|
||||
|
||||
TransferObject() {
|
||||
}
|
||||
|
||||
private void analyzeIfValid() {
|
||||
isValid = audioIndex != - 1 && subtitleIndex != - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user