diff --git a/pom.xml b/pom.xml index ad32318..09e2baa 100644 --- a/pom.xml +++ b/pom.xml @@ -146,7 +146,7 @@ com.fasterxml.jackson.core jackson-databind - 2.9.9 + 2.9.10.1 diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 4b72a3c..b7899e9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,87 @@ +import com.fasterxml.jackson.databind.ObjectMapper; import config.MKVToolProperties; import lombok.extern.log4j.Log4j2; +import model.FileAttribute; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; @Log4j2 public class Main { + private static ObjectMapper mapper = new ObjectMapper(); + public static void main(String[] args) { MKVToolProperties.getInstance().defineMKVToolNixPath(); + Scanner input = new Scanner(System.in); + log.info("Please enter path to file"); + String path = input.nextLine(); + updateAttributes(path, queryAttributes(path)); + } + + public static List queryAttributes(String path) { + Map jsonMap; + List fileAttributes = new ArrayList<>(); + try(InputStream inputStream = Runtime.getRuntime().exec("\"" + MKVToolProperties.getInstance().getMkvmergePath() + "\" --identify --identification-format json \"" + path + "\"").getInputStream()){ + jsonMap = mapper.readValue(inputStream, Map.class); + List> tracks = (List>) jsonMap.get("tracks"); + + for(Map attribute : tracks){ + if(! "video".equals(attribute.get("type"))){ + Map properties = (Map) attribute.get("properties"); + fileAttributes.add(new FileAttribute( + (int) properties.get("number"), + (String) properties.get("language"), + (String) properties.get("track_name"), + (Boolean) properties.get("default_track"), + (Boolean) properties.get("forced_track"), + (String) attribute.get("type"))); + } + } + }catch(IOException e){ + log.error("File could not be found or loaded!"); + } + return fileAttributes; + } + + public static void updateAttributes(String path, List fileAttributes) { + if(fileAttributes.size() > 2){ + StringBuffer stringBuffer = new StringBuffer("\""); + stringBuffer.append(MKVToolProperties.getInstance().getMkvpropeditPath()); + stringBuffer.append("\" \""); + stringBuffer.append(path); + stringBuffer.append("\" "); + + int c = 0; + int d = 0; + + for(FileAttribute attributes : fileAttributes){ + if(attributes.isDefaultTrack() && "audio".equals(attributes.getType())){ + stringBuffer.append("--edit track:" + attributes.getId() + " --set flag-default=0 "); + } + if(attributes.isDefaultTrack() && "subtitles".equals(attributes.getType())){ + stringBuffer.append("--edit track:" + attributes.getId() + " --set flag-default=0 "); + } + if("jpn".equals(attributes.getLanguage()) && "audio".equals(attributes.getType()) && c == 0){ + c++; + stringBuffer.append("--edit track:" + attributes.getId() + " --set flag-default=1 "); + } + if("eng".equals(attributes.getLanguage()) && "subtitles".equals(attributes.getType()) && d == 0){ + d++; + stringBuffer.append("--edit track:" + attributes.getId() + " --set flag-default=1 "); + } + } + try{ + Runtime.getRuntime().exec(stringBuffer.toString()); + }catch(IOException e){ + log.error("Couldn't make changes to file"); + + } + }else{ + log.info("There were not enough lines provided to make any changes to the file"); + } } } diff --git a/src/main/java/config/MKVToolProperties.java b/src/main/java/config/MKVToolProperties.java index 45acb5c..2fb44f9 100644 --- a/src/main/java/config/MKVToolProperties.java +++ b/src/main/java/config/MKVToolProperties.java @@ -3,8 +3,7 @@ package config; import lombok.Getter; import lombok.extern.log4j.Log4j2; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; @@ -31,8 +30,7 @@ public class MKVToolProperties { } public void defineMKVToolNixPath() { - searchWithFilePath(); - if(pathIsValid()){ + if(searchWithFilePath() && pathIsValid()){ log.info("MKVToolNix found!"); return; } @@ -47,11 +45,15 @@ public class MKVToolProperties { while(true){ searchWithUserPath(input); if(pathIsValid()){ - log.info("MKVToolNix found!"); break; } } - log.error("MKVToolNix not found anywhere!"); + try(PrintWriter writer = new PrintWriter("mkvDirectoryPath", "UTF-8")){ + writer.println(directoryPath); + }catch(UnsupportedEncodingException | FileNotFoundException e){ + log.error("File counldn't be written!"); + } + log.info("MKVToolNix found!"); } private boolean pathIsValid() { @@ -71,12 +73,14 @@ public class MKVToolProperties { mkvpropeditPath = directoryPath + "mkvpropedit.exe"; } - private void searchWithFilePath() { + private boolean searchWithFilePath() { try(Stream stream = Files.lines(Paths.get("mkvDirectoryPath"))){ directoryPath = stream.collect(Collectors.joining("\n")); }catch(IOException e){ log.fatal(e.getMessage()); + return false; } + return true; } private void searchInDefaultPath() { @@ -84,6 +88,7 @@ public class MKVToolProperties { } private void searchWithUserPath(Scanner input) { + log.info("Please enter the path to the directory of MKVToolNix:"); directoryPath = input.nextLine(); } } diff --git a/src/main/java/model/FileAttribute.java b/src/main/java/model/FileAttribute.java new file mode 100644 index 0000000..f6a6149 --- /dev/null +++ b/src/main/java/model/FileAttribute.java @@ -0,0 +1,22 @@ +package model; + +import lombok.Getter; + +@Getter +public class FileAttribute { + private int id; + private String language; + private String trackName; + private boolean defaultTrack; + private boolean forcedTrack; + private String type; + + public FileAttribute(int id, String language, String trackName, boolean defaultTrack, boolean forcedTrack, String type) { + this.id = id; + this.language = language; + this.trackName = trackName; + this.defaultTrack = defaultTrack; + this.forcedTrack = forcedTrack; + this.type = type; + } +}