[IMPL] query and update file attributes

This commit is contained in:
RatzzFatzz
2019-11-26 00:19:04 +01:00
parent 7c2e292da2
commit d711d38cb3
4 changed files with 113 additions and 8 deletions

View File

@@ -146,7 +146,7 @@
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.9.9</version> <version>2.9.10.1</version>
</dependency> </dependency>
<!-- endregion --> <!-- endregion -->
<!-- region unit-tests --> <!-- region unit-tests -->

View File

@@ -1,9 +1,87 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import config.MKVToolProperties; import config.MKVToolProperties;
import lombok.extern.log4j.Log4j2; 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 @Log4j2
public class Main { public class Main {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) { public static void main(String[] args) {
MKVToolProperties.getInstance().defineMKVToolNixPath(); 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<FileAttribute> queryAttributes(String path) {
Map<String, Object> jsonMap;
List<FileAttribute> 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<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");
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<FileAttribute> 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");
}
} }
} }

View File

@@ -3,8 +3,7 @@ package config;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Scanner; import java.util.Scanner;
@@ -31,8 +30,7 @@ public class MKVToolProperties {
} }
public void defineMKVToolNixPath() { public void defineMKVToolNixPath() {
searchWithFilePath(); if(searchWithFilePath() && pathIsValid()){
if(pathIsValid()){
log.info("MKVToolNix found!"); log.info("MKVToolNix found!");
return; return;
} }
@@ -47,11 +45,15 @@ public class MKVToolProperties {
while(true){ while(true){
searchWithUserPath(input); searchWithUserPath(input);
if(pathIsValid()){ if(pathIsValid()){
log.info("MKVToolNix found!");
break; 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() { private boolean pathIsValid() {
@@ -71,12 +73,14 @@ public class MKVToolProperties {
mkvpropeditPath = directoryPath + "mkvpropedit.exe"; mkvpropeditPath = directoryPath + "mkvpropedit.exe";
} }
private void searchWithFilePath() { private boolean searchWithFilePath() {
try(Stream<String> stream = Files.lines(Paths.get("mkvDirectoryPath"))){ try(Stream<String> stream = Files.lines(Paths.get("mkvDirectoryPath"))){
directoryPath = stream.collect(Collectors.joining("\n")); directoryPath = stream.collect(Collectors.joining("\n"));
}catch(IOException e){ }catch(IOException e){
log.fatal(e.getMessage()); log.fatal(e.getMessage());
return false;
} }
return true;
} }
private void searchInDefaultPath() { private void searchInDefaultPath() {
@@ -84,6 +88,7 @@ public class MKVToolProperties {
} }
private void searchWithUserPath(Scanner input) { private void searchWithUserPath(Scanner input) {
log.info("Please enter the path to the directory of MKVToolNix:");
directoryPath = input.nextLine(); directoryPath = input.nextLine();
} }
} }

View File

@@ -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;
}
}