From f380f97cca6a4aee16c7584839536af81edca024 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 8 May 2020 19:12:47 +0200 Subject: [PATCH] Removes inner classes and updates functions in main --- .../net/knarcraft/ffmpegconverter/Main.java | 196 ++++-------------- 1 file changed, 41 insertions(+), 155 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/Main.java b/src/main/java/net/knarcraft/ffmpegconverter/Main.java index 295738a..b9fb6fe 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/Main.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/Main.java @@ -1,26 +1,28 @@ package net.knarcraft.ffmpegconverter; +import net.knarcraft.ffmpegconverter.converter.AbstractConverter; import net.knarcraft.ffmpegconverter.converter.AnimeConverter; import net.knarcraft.ffmpegconverter.converter.AudioConverter; -import net.knarcraft.ffmpegconverter.converter.Converter; import net.knarcraft.ffmpegconverter.converter.VideoConverter; +import net.knarcraft.ffmpegconverter.utility.FileUtil; +import net.knarcraft.ffmpegconverter.utility.ListUtil; +import net.knarcraft.ffmpegconverter.utility.OutputUtil; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Scanner; -import java.util.function.Predicate; -import static net.knarcraft.ffmpegconverter.Parser.tokenize; +import static net.knarcraft.ffmpegconverter.utility.Parser.tokenize; /** * Converts a files or files in a folder to a web playable mp4. */ -public class Main { +class Main { private static final String FFPROBE_PATH = "ffprobe"; //Can be just ffprobe if it's in the path private static final String FFMPEG_PATH = "ffmpeg"; //Can be just ffmpeg if it's in the path private static final Scanner READER = new Scanner(System.in, "UTF-8"); - private static final BufferedWriter WRITER = new BufferedWriter(new OutputStreamWriter(System.out)); - private static Converter con = null; + private static AbstractConverter converter = null; public static void main(String[] args) throws IOException { //System.out.println(tokenizer("AnimeConverter -audiolang jap,eng -sublang eng,nor,* \"C:\\Users\\Kristian\\Downloads\\Anime\\[Kametsu] ERASED (BD 1080p Hi10 FLAC)\"")); @@ -30,16 +32,16 @@ public class Main { int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" + "3. VideoStream converter", 1, 3); - printl("Input for this converter:"); + OutputUtil.println("Input for this converter:"); switch (choice) { case 1: animeConverter(); break; case 2: - con = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); + converter = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); break; case 3: - con = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); + converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); break; default: System.exit(1); @@ -47,10 +49,10 @@ public class Main { int recursionSteps = 1; - printl(" [Recursions]: "); + OutputUtil.println(" [Recursions]: "); List input = readInput(2); while (input.isEmpty()) { - print("File path required."); + OutputUtil.print("File path required."); input = readInput(2); } File folder = new File(input.get(0)); @@ -58,105 +60,45 @@ public class Main { try { recursionSteps = Integer.parseInt(input.get(1)); } catch (NumberFormatException e) { - printl("Recursion steps is invalid and will be ignored."); + OutputUtil.println("Recursion steps is invalid and will be ignored."); } } if (folder.isDirectory()) { - File[] files = listFilesRec(folder, con.getValidFormats(), recursionSteps); + File[] files = FileUtil.listFilesRecursive(folder, converter.getValidFormats(), recursionSteps); if (files != null && files.length > 0) { for (File file : files) { - con.convert(file); + converter.convert(file); } } else { - printl("No valid files found in folder."); + OutputUtil.println("No valid files found in folder."); } } else if (folder.exists()) { - con.convert(folder); + converter.convert(folder); } else { - System.out.println("Path " + folder.getAbsolutePath() + " does not point to any file or folder."); - } - WRITER.close(); - } - - /** - * Prints a string - * @param input

The string to print.

- * @throws IOException

If the writer fails to write.

- */ - private static void print(String input) throws IOException { - WRITER.write(input); - WRITER.flush(); - } - - /** - * Prints a string and a line break - * @param input

The string to print.

- * @throws IOException

If the writer fails to write.

- */ - private static void printl(String input) throws IOException { - WRITER.write(input); - WRITER.newLine(); - WRITER.flush(); - } - - enum converterArgumentValueType { - BOOLEAN, - COMMA_SEPARATED_LIST, - SINGLE_VALUE, - INT - } - - static class converterArgument { - private String name; - private boolean valueRequired; - private converterArgumentValueType valueType; - converterArgument(String name, boolean valueRequired, converterArgumentValueType valueType) { - this.name = name; - this.valueRequired = valueRequired; - this.valueType = valueType; - } - - private boolean testArgumentValue(String value) { - if (value.length() == 0) { - return !valueRequired; - } - if (valueRequired && value.startsWith("-")) { - return false; - } - switch (valueType) { - case BOOLEAN: - String lower = value.toLowerCase(); - return lower.equals("true") || lower.equals("false"); - case COMMA_SEPARATED_LIST: - return !value.contains(" "); - case SINGLE_VALUE: - return !value.contains(" "); - case INT: - int ignored = Integer.parseInt(value); - return true; - } - return false; + OutputUtil.println("Path " + folder.getAbsolutePath() + " does not point to any file or folder."); } + OutputUtil.close(); } /** * Initializes the anime converter + * * @throws IOException

If reading or writing fails.

*/ private static void animeConverter() throws IOException { - printl("[Audio languages jpn,eng,ger,fre] [Subtitle languages eng,ger,fre] [Convert to stereo if " + + OutputUtil.println("[Audio languages jpn,eng,ger,fre] [Subtitle languages eng,ger,fre] [Convert to stereo if " + "necessary true/false] [Prevent signs&songs subtitles true/false]\nYour input: "); List input = readInput(4); String[] audioLang = new String[]{"jpn", "*"}; String[] subtitleLang = new String[]{"eng", "*"}; boolean toStereo = true; boolean preventSigns = true; - if (input.size() > 0 && getList(input, 0) != null) { - audioLang = getList(input, 0); + if (input.size() > 0 && ListUtil.getList(input, 0) != null) { + audioLang = ListUtil.getList(input, 0); } - if (input.size() > 1 && getList(input, 1) != null) { - subtitleLang = getList(input, 1); + if (input.size() > 1 && ListUtil.getList(input, 1) != null) { + subtitleLang = ListUtil.getList(input, 1); } if (input.size() > 2) { toStereo = Boolean.parseBoolean(input.get(2)); @@ -164,34 +106,18 @@ public class Main { if (input.size() > 3) { preventSigns = Boolean.parseBoolean(input.get(3)); } - con = new AnimeConverter(FFPROBE_PATH, FFMPEG_PATH, audioLang, subtitleLang, toStereo, preventSigns); + converter = new AnimeConverter(FFPROBE_PATH, FFMPEG_PATH, audioLang, subtitleLang, toStereo, preventSigns); } - /** - * Gets a list from a comma separated string at index in list - * @param list

A list of tokens.

- * @param index

The index of the token containing comma separated entries.

- * @return

A string list.

- */ - private static String[] getList(List list, int index) { - String[] result = null; - if (list.size() > index) { - if (list.get(index).contains(",")) { - result = list.get(index).split(","); - } else { - result = new String[]{list.get(index)}; - } - } - return result; - } /** * Reads a number of tokens from the user input + * * @param max

The number of tokens expected.

* @return

A list of tokens.

*/ private static List readInput(int max) { - List tokens = tokenize(READER.nextLine()); + List tokens = tokenize(READER.nextLine()); if (max < tokens.size()) { throw new IllegalArgumentException("Input contains " + tokens.size() + " arguments, but the input only supports " + max + " arguments."); @@ -201,15 +127,16 @@ public class Main { /** * Gets the user's choice + * * @param prompt

The prompt shown to the user.

* @return

The non-empty choice given by the user.

* @throws IOException

If reading or writing fails.

*/ private static String getChoice(String prompt) throws IOException { - printl(prompt); + OutputUtil.println(prompt); String choice = ""; while (choice.equals("")) { - printl("Your input: "); + OutputUtil.println("Your input: "); choice = READER.nextLine(); } return choice; @@ -217,66 +144,25 @@ public class Main { /** * Gets an integer from the user - * @param prompt The prompt to give the user - * @param min The minimum allowed value - * @param max The maximum allowed value - * @return The value given by the user + * + * @param prompt The prompt to give the user + * @param min The minimum allowed value + * @param max The maximum allowed value + * @return The value given by the user */ private static int getChoice(String prompt, int min, int max) throws IOException { - printl(prompt); + OutputUtil.println(prompt); int choice = 0; while (choice < min || choice > max) { - printl("Your input: "); + OutputUtil.println("Your input: "); try { choice = Integer.parseInt(READER.next()); } catch (NumberFormatException e) { - printl("Invalid choice. Please try again."); + OutputUtil.println("Invalid choice. Please try again."); } finally { READER.nextLine(); } } return choice; } - - /** - * Tests if any element in a list fulfills a condition. - * - * @param list The list to test against - * @param predicate A predicate to use on every element in the list - * @param Anything which can be stored in a list - * @return True if at least one element fulfills the predicate - */ - static boolean listContains(T[] list, Predicate predicate) { - for (T item : list) { - if (predicate.test(item)) { - return true; - } - } - return false; - } - - /** - * Recursively lists all files in a folder - * - * @param folder The folder to start from - * @param maxRec Maximum number of recursions - * @return A list of files - */ - private static File[] listFilesRec(File folder, String[] extensions, int maxRec) { - if (maxRec == 0) { return null; } - File[] listOfFiles = folder.listFiles((file) -> file.isFile() && listContains(extensions, (item) -> file.getName().endsWith(item))); - if (listOfFiles == null) { return null; } - if (maxRec > 1) { - File[] listOfFolders = folder.listFiles((dir, name) -> new File(dir, name).isDirectory()); - if (listOfFolders != null) { - for (File file : listOfFolders) { - File[] nextLevel = listFilesRec(file, extensions, maxRec - 1); - if (nextLevel != null) { - listOfFiles = Converter.concatenate(listOfFiles, nextLevel); - } - } - } - } - return listOfFiles; - } }