package query; import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute; import at.pcgamingfreaks.yaml.YAML; import at.pcgamingfreaks.yaml.YamlInvalidContentException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.log4j.Log4j2; import java.io.File; 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 final 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) { YAML yaml; List subtitles = null; List audios = null; try{ yaml = new YAML(new File("./src/main/resources/config.yaml")); subtitles = yaml.getStringList("subtitle", null); audios = yaml.getStringList("audio", null); if(fileAttributes.size() > 2 && subtitles != null && audios != null){ int oldAudioDefault = - 1; int oldSubtitleDefault = - 1; int audioDefault = - 1; int subtitleDefault = - 1; int subtitleIndex = - 1; int audioIndex = - 1; for(FileAttribute attribute : fileAttributes){ if(subtitles.contains(attribute.getLanguage()) && "subtitles".equals(attribute.getType())){ for(int i = 0; i < subtitles.size(); i++){ if(subtitles.get(i).equals(attribute.getLanguage())){ if(subtitleIndex == - 1 || i < subtitleIndex){ subtitleIndex = i; subtitleDefault = attribute.getId(); } } } } if(audios.contains(attribute.getLanguage()) && "audio".equals(attribute.getType())){ for(int i = 0; i < audios.size(); i++){ if(audios.get(i).equals(attribute.getLanguage())){ if(audioIndex == - 1 || i < audioIndex){ audioIndex = i; audioDefault = attribute.getId(); } } } } if(attribute.isDefaultTrack() && "audio".equals(attribute.getType())){ oldAudioDefault = attribute.getId(); } if(attribute.isDefaultTrack() && "subtitles".equals(attribute.getType())){ oldSubtitleDefault = attribute.getId(); } } if(oldAudioDefault == audioDefault && oldSubtitleDefault == subtitleDefault){ return; } if(audioIndex != 0){ subtitleDefault = oldSubtitleDefault; } StringBuilder stringBuffer = new StringBuilder("\""); stringBuffer.append(MKVToolProperties.getInstance().getMkvpropeditPath()); stringBuffer.append("\" \"").append(path).append("\" "); stringBuffer.append("--edit track:").append(oldSubtitleDefault).append(" --set flag-default=0 "); stringBuffer.append("--edit track:").append(oldAudioDefault).append(" --set flag-default=0 "); stringBuffer.append("--edit track:").append(subtitleDefault).append(" --set flag-default=1 "); stringBuffer.append("--edit track:").append(audioDefault).append(" --set flag-default=1 "); try{ Runtime.getRuntime().exec(stringBuffer.toString()); }catch(IOException e){ log.error("Couldn't make changes to file"); } log.info("Success: " + path); }else{ log.info("There were not enough lines provided to make any changes to the file"); } }catch(YamlInvalidContentException | IOException e){ log.error("Failure: " + path); log.error(e.getMessage()); } } }