[FIX] Linux compatibility

This commit is contained in:
2020-06-04 23:07:32 +02:00
parent aebc89fcae
commit 79fa6de190
6 changed files with 51 additions and 15 deletions

View File

@@ -1,3 +1,4 @@
mkvtoolnixPath: /usr/bin
config:
1:
audio:

View File

@@ -72,6 +72,14 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -18,18 +18,22 @@ public class AttributeUpdaterKernel {
public void execute(String path) {
List<AttributeConfig> configPattern = ConfigUtil.loadConfig();
List<File> allValidPaths = collector.loadFiles(path);
if(! allValidPaths.isEmpty() && configPattern != null){
if(allValidPaths != null && configPattern != null){
for(File file : allValidPaths){
List<FileAttribute> attributes = collector.loadAttributes(file);
boolean fileHasChanged = false;
for(AttributeConfig config : configPattern){
/*
* Creating new ArrayList, because the method removes elements from the list by reference
*/
boolean fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
if(fileHasChanged){
break;
}
}
if(! fileHasChanged){
log.info(file.getName() + " didn't change!");
}
}
}else{
log.error("Path is not valid or config has errors!");

View File

@@ -21,12 +21,17 @@ public class Main {
private static boolean checkIfMKVToolNixIsValid() {
try{
String path = new YAML(new File("config.yml")).getString("mkvtoolnixPath");
String path = new YAML(new File("config.yaml")).getString("mkvtoolnixPath");
if(! path.endsWith(File.separator)){
path += File.separator;
}
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvproperties");
if(System.getProperty("os.name").toLowerCase().contains("windows")){
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge.exe");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit.exe");
}else{
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvpropedit");
}
}catch(YamlKeyNotFoundException | IOException | YamlInvalidContentException e){
e.printStackTrace();
}

View File

@@ -84,9 +84,15 @@ public class ConfigProcessor {
* @return if the 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("\"");
stringBuffer.append(MKVToolProperties.getInstance().getMkvpropeditPath());
stringBuffer.append("\" \"").append(file.getAbsolutePath()).append("\" ");
StringBuilder stringBuffer = new StringBuilder();
if(System.getProperty("os.name").toLowerCase().contains("windows")){
stringBuffer.append("\"");
stringBuffer.append(MKVToolProperties.getInstance().getMkvpropeditPath());
stringBuffer.append("\" \"").append(file.getAbsolutePath()).append("\" ");
}else{
stringBuffer.append(MKVToolProperties.getInstance().getMkvpropeditPath());
stringBuffer.append(" ").append(file.getAbsolutePath()).append(" ");
}
if(audioDefault != - 1){
stringBuffer.append("--edit track:=").append(audioDefault).append(" --set flag-default=0 ");
}
@@ -106,18 +112,19 @@ public class ConfigProcessor {
* 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.getName() + " already fits config!");
return true;
}
try{
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(file.getName() + " was updated");
return true;
}else{
return false;

View File

@@ -56,14 +56,24 @@ public class MkvFileCollector implements FileCollector {
public List<FileAttribute> loadAttributes(File file) {
Map<String, Object> jsonMap;
List<FileAttribute> fileAttributes = new ArrayList<>();
System.out.println("\"" + MKVToolProperties.getInstance().getMkvmergePath()
+ "\" --identify --identification-format json \"" + file.getAbsolutePath() + "\"");
try(InputStream inputStream =
Runtime.getRuntime().exec("\"" + MKVToolProperties.getInstance().getMkvmergePath()
+ "\" --identify --identification-format json \"" + file.getAbsolutePath() + "\"").getInputStream()){
try{
String command = "";
if(System.getProperty("os.name").toLowerCase().contains("windows")){
command = "\"" + MKVToolProperties.getInstance().getMkvmergePath() + "\"";
}else{
command = MKVToolProperties.getInstance().getMkvmergePath();
}
String[] array = new String[]{
command,
"--identify",
"--identification-format",
"json",
file.getAbsoluteFile().toString()
};
InputStream inputStream = Runtime.getRuntime().exec(array).getInputStream();
jsonMap = mapper.readValue(inputStream, Map.class);
List<Map<String, Object>> tracks = (List<Map<String, Object>>) jsonMap.get("tracks");
for(Map<String, Object> attribute : tracks){
if(! "video".equals(attribute.get("type"))){
Map<String, Object> properties = (Map<String, Object>) attribute.get("properties");
@@ -77,6 +87,7 @@ public class MkvFileCollector implements FileCollector {
}
}
}catch(IOException e){
e.printStackTrace();
log.error("File could not be found or loaded!");
}
return fileAttributes;