diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/GUI.form b/src/main/java/GUI.form deleted file mode 100644 index 94c37be..0000000 --- a/src/main/java/GUI.form +++ /dev/null @@ -1,13 +0,0 @@ - -
- - - - - - - - - - -
diff --git a/src/main/java/GUI.java b/src/main/java/GUI.java deleted file mode 100644 index b544f49..0000000 --- a/src/main/java/GUI.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java index 62c44d2..9c1f5ef 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/AttributeUpdaterKernel.java @@ -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 configPattern = ConfigUtil.loadConfig(); - List 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 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 allValidPaths = collector.loadFiles(path); + if(! allValidPaths.isEmpty()){ + for(File file : allValidPaths){ + List 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!"); } } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/MKVToolProperties.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/MKVToolProperties.java index 4319234..14618ee 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/MKVToolProperties.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/MKVToolProperties.java @@ -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 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(); - } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java index 926d9d3..0509f99 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/Main.java @@ -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(); } } diff --git a/src/main/java/config/CustomOutputStream.java b/src/main/java/config/CustomOutputStream.java deleted file mode 100644 index f6d5db2..0000000 --- a/src/main/java/config/CustomOutputStream.java +++ /dev/null @@ -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()); - } -} diff --git a/src/main/java/query/QueryBuilder.java b/src/main/java/query/QueryBuilder.java deleted file mode 100644 index dd51991..0000000 --- a/src/main/java/query/QueryBuilder.java +++ /dev/null @@ -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 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()); - } - } -} diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java deleted file mode 100644 index 895a398..0000000 --- a/src/test/java/MainTest.java +++ /dev/null @@ -1,3 +0,0 @@ -public class MainTest { - -} diff --git a/src/test/java/config/MKVToolPropertiesTest.java b/src/test/java/config/MKVToolPropertiesTest.java deleted file mode 100644 index eff1fdf..0000000 --- a/src/test/java/config/MKVToolPropertiesTest.java +++ /dev/null @@ -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(); - } -}