From 3531299a237b2082eb2642efc5817e1f8486c19b Mon Sep 17 00:00:00 2001 From: RatzzFatzz Date: Thu, 28 Nov 2019 23:40:59 +0100 Subject: [PATCH] [IMPL] path supports dir and files now --- src/main/java/Main.java | 74 +---------------- src/main/java/query/QueryBuilder.java | 112 ++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 70 deletions(-) create mode 100644 src/main/java/query/QueryBuilder.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index b7899e9..f61cb42 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,87 +1,21 @@ -import com.fasterxml.jackson.databind.ObjectMapper; import config.MKVToolProperties; import lombok.extern.log4j.Log4j2; -import model.FileAttribute; +import query.QueryBuilder; -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)); + QueryBuilder queryBuilder = new QueryBuilder(); + queryBuilder.executeUpdateOnAllFiles(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/query/QueryBuilder.java b/src/main/java/query/QueryBuilder.java new file mode 100644 index 0000000..028b09b --- /dev/null +++ b/src/main/java/query/QueryBuilder.java @@ -0,0 +1,112 @@ +package query; + +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.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Log4j2 +public class QueryBuilder { + private ObjectMapper mapper = new ObjectMapper(); + + public QueryBuilder() { + } + + public boolean executeUpdateOnAllFiles(String path) { + List allFilePaths = getAllFilesFromDirectory(path); + if(allFilePaths == null){ + log.error("Couldn't process path!"); + return false; + } + for(String filePath : allFilePaths){ + updateAttributes(filePath, queryAttributes(filePath)); + } + return true; + } + + private List getAllFilesFromDirectory(String path) { + try(Stream paths = Files.walk(Paths.get(path))){ + return paths + .filter(Files::isRegularFile) + .map(file -> file.toAbsolutePath().toString()) + .collect(Collectors.toList()); + }catch(IOException e){ + log.error("Couldn't find file or directory!", e); + } + return null; + } + + private 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; + } + + private 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"); + } + } +}