mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
[IMPL] query and update file attributes
This commit is contained in:
@@ -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<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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
22
src/main/java/model/FileAttribute.java
Normal file
22
src/main/java/model/FileAttribute.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user