[IMPL] Changed to CLI tool

This commit is contained in:
2020-06-04 00:53:33 +02:00
parent aef1fdf9a5
commit e0a21b5d7c
10 changed files with 43 additions and 461 deletions

0
README.md Normal file
View File

View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI">
<grid id="27dc6" row-count="1" column-count="1" layout-manager="GridLayoutManager">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</form>

View File

@@ -1,90 +0,0 @@
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileAttribute;
import lombok.extern.log4j.Log4j2;
import query.QueryBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
@Log4j2
public class GUI {
private String path;
private JButton openFileBrowser;
private JButton startOperation;
public GUI() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("MKV Audio and Subtitle Changer");
frame.setSize(500, 75);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel top = new JPanel(new GridLayout(1, 2, 20, 20));
openFileBrowser = new JButton("Browse directory");
openFileBrowser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
try{
if(! readFile("lastDir", Charset.defaultCharset()).isEmpty()){
String temp = readFile("lastDir", Charset.defaultCharset());
fileChooser.setCurrentDirectory(new File(temp));
}
}catch(IOException ie){
log.info("Couldn't start FileChooser with default path");
}
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setPreferredSize(new Dimension(600, 500));
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);
fileChooser.showOpenDialog(null);
try{
if(FileAttribute.pathIsValid(fileChooser.getSelectedFile().getAbsolutePath())){
path = fileChooser.getSelectedFile().getAbsolutePath();
try(PrintWriter out = new PrintWriter("lastDir", "UTF-8")){
out.print(fileChooser.getCurrentDirectory());
}catch(FileNotFoundException | UnsupportedEncodingException fne){
log.error(fne);
}
startOperation.setEnabled(true);
}
}catch(NullPointerException ne){
System.out.println("File or directory not found!");
log.error("File or directory not found!", ne);
}
}
});
startOperation = new JButton("Start updating");
startOperation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
QueryBuilder queryBuilder = new QueryBuilder();
if(queryBuilder.executeUpdateOnAllFiles(path)){
log.info("All files updated!");
System.out.println("All files updated!");
}
}
});
startOperation.setEnabled(false);
top.add(openFileBrowser);
top.add(startOperation);
frame.add(top, BorderLayout.NORTH);
frame.setVisible(true);
}
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@@ -10,36 +10,29 @@ import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@Log4j2
public class AttributeUpdaterKernel {
MkvFileCollector collector = new MkvFileCollector();
public void execute() {
public void execute(String path) {
List<AttributeConfig> configPattern = ConfigUtil.loadConfig();
List<File> allValidPaths = null;
try(Scanner scanner = new Scanner(System.in)){
System.out.println("Please enter the path to the files which should be updated: ");
do{
allValidPaths = collector.loadFiles(scanner.nextLine());
if(allValidPaths == null){
System.out.println("Please enter a valid path: ");
}
}while(allValidPaths == null);
log.info(allValidPaths.size() + " files where found and will now be processed!");
}
for(File file : allValidPaths){
List<FileAttribute> attributes = collector.loadAttributes(file);
for(AttributeConfig config : configPattern){
/*
* Creating new ArrayList, because the method removes elements from the list by reference
*/
boolean fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
if(fileHasChanged){
break;
List<File> allValidPaths = collector.loadFiles(path);
if(! allValidPaths.isEmpty()){
for(File file : allValidPaths){
List<FileAttribute> attributes = collector.loadAttributes(file);
for(AttributeConfig config : configPattern){
/*
* Creating new ArrayList, because the method removes elements from the list by reference
*/
boolean fileHasChanged = new ConfigProcessor(config).processConfig(file, new ArrayList<>(attributes));
if(fileHasChanged){
break;
}
}
}
}else{
log.error("Path is not valid!");
}
}
}

View File

@@ -1,19 +1,13 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Log4j2
@Getter
@Setter
public class MKVToolProperties {
private String directoryPath;
private String mkvmergePath;
private String mkvpropeditPath;
@@ -28,67 +22,4 @@ public class MKVToolProperties {
}
return instance;
}
public void defineMKVToolNixPath() {
if(searchWithFilePath() && pathIsValid()){
log.info("MKVToolNix found!");
return;
}
log.debug("MKVToolNix not found in file!");
searchInDefaultPath();
if(pathIsValid()){
log.info("MKVToolNix found!");
return;
}
log.debug("MKVToolNix not found in default path!");
Scanner input = new Scanner(System.in);
while(true){
searchWithUserPath(input);
if(pathIsValid()){
break;
}
}
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() {
checkForSeparator();
setMKVmergeAndPropEditPath();
return new File(mkvmergePath).exists() && new File(mkvpropeditPath).exists();
}
private void checkForSeparator() {
if(! (directoryPath.endsWith("/") || (directoryPath.endsWith("\\") && System.getProperty("os.name").toLowerCase().contains("windows")))){
directoryPath += File.separator;
}
}
private void setMKVmergeAndPropEditPath() {
mkvmergePath = directoryPath + "mkvmerge.exe";
mkvpropeditPath = directoryPath + "mkvpropedit.exe";
}
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() {
directoryPath = "C:\\Program Files\\MKVToolNix";
}
private void searchWithUserPath(Scanner input) {
log.info("Please enter the path to the directory of MKVToolNix:");
directoryPath = input.nextLine();
}
}

View File

@@ -1,9 +1,32 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger;
import at.pcgamingfreaks.yaml.YAML;
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
import at.pcgamingfreaks.yaml.YamlKeyNotFoundException;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
@Log4j2
public class Main {
public static void main(String[] args) {
MKVToolProperties.getInstance().defineMKVToolNixPath();
AttributeUpdaterKernel kernel = new AttributeUpdaterKernel();
kernel.execute();
if(checkIfMKVToolNixIsValid()){
AttributeUpdaterKernel kernel = new AttributeUpdaterKernel();
kernel.execute(args[0]);
}else{
log.error("MKVToolNix was not found! Please recheck path");
}
}
private static boolean checkIfMKVToolNixIsValid() {
try{
String path = new YAML(new File("config.yml")).getString("mkvtoolnixPath");
MKVToolProperties.getInstance().setMkvmergePath(path + "mkvmerge");
MKVToolProperties.getInstance().setMkvpropeditPath(path + "mkvproperties");
}catch(YamlKeyNotFoundException | IOException | YamlInvalidContentException e){
e.printStackTrace();
}
return new File(MKVToolProperties.getInstance().getMkvmergePath()).isFile() && new File(MKVToolProperties.getInstance().getMkvpropeditPath()).isFile();
}
}

View File

@@ -1,21 +0,0 @@
package config;
import javax.swing.*;
import java.io.IOException;
import java.io.OutputStream;
public class CustomOutputStream extends OutputStream {
private JTextArea textArea;
public CustomOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char) b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}

View File

@@ -1,160 +0,0 @@
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<String> 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<String> getAllFilesFromDirectory(String path) {
try(Stream<Path> 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<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;
}
private void updateAttributes(String path, List<FileAttribute> fileAttributes) {
YAML yaml;
List<String> subtitles = null;
List<String> 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());
}
}
}

View File

@@ -1,3 +0,0 @@
public class MainTest {
}

View File

@@ -1,78 +0,0 @@
package config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.MKVToolProperties;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Log4j2
public class MKVToolPropertiesTest {
@Test
public void testPathIsValid() {
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src/test/resources");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertEquals("src/test/resources\\mkvmerge.exe", MKVToolProperties.getInstance().getMkvmergePath());
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src/test/resources/");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertEquals("src/test/resources/mkvmerge.exe", MKVToolProperties.getInstance().getMkvmergePath());
}
@Test
public void testCheckForSeparator() {
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src/test/resources");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertTrue(MKVToolProperties.getInstance().getDirectoryPath().endsWith("/") || MKVToolProperties.getInstance().getDirectoryPath().endsWith("\\"));
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src/test/resources/");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertTrue(MKVToolProperties.getInstance().getDirectoryPath().endsWith("/") || MKVToolProperties.getInstance().getDirectoryPath().endsWith("\\"));
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src\\test\\resources");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertTrue(MKVToolProperties.getInstance().getDirectoryPath().endsWith("/") || MKVToolProperties.getInstance().getDirectoryPath().endsWith("\\"));
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print("src\\test\\resources\\");
}catch(FileNotFoundException | UnsupportedEncodingException e){
log.error("File not found!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
assertTrue(MKVToolProperties.getInstance().getDirectoryPath().endsWith("/") || MKVToolProperties.getInstance().getDirectoryPath().endsWith("\\"));
}
@AfterEach
public void afterAll() {
File file = new File("mkvDirectoryPath");
file.delete();
}
}