Removes inner classes and updates functions in main
This commit is contained in:
parent
4f07fd09fd
commit
f380f97cca
@ -1,26 +1,28 @@
|
|||||||
package net.knarcraft.ffmpegconverter;
|
package net.knarcraft.ffmpegconverter;
|
||||||
|
|
||||||
|
import net.knarcraft.ffmpegconverter.converter.AbstractConverter;
|
||||||
import net.knarcraft.ffmpegconverter.converter.AnimeConverter;
|
import net.knarcraft.ffmpegconverter.converter.AnimeConverter;
|
||||||
import net.knarcraft.ffmpegconverter.converter.AudioConverter;
|
import net.knarcraft.ffmpegconverter.converter.AudioConverter;
|
||||||
import net.knarcraft.ffmpegconverter.converter.Converter;
|
|
||||||
import net.knarcraft.ffmpegconverter.converter.VideoConverter;
|
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.List;
|
||||||
import java.util.Scanner;
|
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.
|
* 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 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 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 Scanner READER = new Scanner(System.in, "UTF-8");
|
||||||
private static final BufferedWriter WRITER = new BufferedWriter(new OutputStreamWriter(System.out));
|
private static AbstractConverter converter = null;
|
||||||
private static Converter con = null;
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
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)\""));
|
//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" +
|
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" +
|
||||||
"3. VideoStream converter", 1, 3);
|
"3. VideoStream converter", 1, 3);
|
||||||
|
|
||||||
printl("Input for this converter:");
|
OutputUtil.println("Input for this converter:");
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
case 1:
|
case 1:
|
||||||
animeConverter();
|
animeConverter();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
con = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
converter = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
con = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
@ -47,10 +49,10 @@ public class Main {
|
|||||||
|
|
||||||
int recursionSteps = 1;
|
int recursionSteps = 1;
|
||||||
|
|
||||||
printl("<Folder/File> [Recursions]: ");
|
OutputUtil.println("<Folder/File> [Recursions]: ");
|
||||||
List<String> input = readInput(2);
|
List<String> input = readInput(2);
|
||||||
while (input.isEmpty()) {
|
while (input.isEmpty()) {
|
||||||
print("File path required.");
|
OutputUtil.print("File path required.");
|
||||||
input = readInput(2);
|
input = readInput(2);
|
||||||
}
|
}
|
||||||
File folder = new File(input.get(0));
|
File folder = new File(input.get(0));
|
||||||
@ -58,105 +60,45 @@ public class Main {
|
|||||||
try {
|
try {
|
||||||
recursionSteps = Integer.parseInt(input.get(1));
|
recursionSteps = Integer.parseInt(input.get(1));
|
||||||
} catch (NumberFormatException e) {
|
} 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()) {
|
if (folder.isDirectory()) {
|
||||||
File[] files = listFilesRec(folder, con.getValidFormats(), recursionSteps);
|
File[] files = FileUtil.listFilesRecursive(folder, converter.getValidFormats(), recursionSteps);
|
||||||
if (files != null && files.length > 0) {
|
if (files != null && files.length > 0) {
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
con.convert(file);
|
converter.convert(file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printl("No valid files found in folder.");
|
OutputUtil.println("No valid files found in folder.");
|
||||||
}
|
}
|
||||||
} else if (folder.exists()) {
|
} else if (folder.exists()) {
|
||||||
con.convert(folder);
|
converter.convert(folder);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Path " + folder.getAbsolutePath() + " does not point to any file or folder.");
|
OutputUtil.println("Path " + folder.getAbsolutePath() + " does not point to any file or folder.");
|
||||||
}
|
|
||||||
WRITER.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints a string
|
|
||||||
* @param input <p>The string to print.</p>
|
|
||||||
* @throws IOException <p>If the writer fails to write.</p>
|
|
||||||
*/
|
|
||||||
private static void print(String input) throws IOException {
|
|
||||||
WRITER.write(input);
|
|
||||||
WRITER.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints a string and a line break
|
|
||||||
* @param input <p>The string to print.</p>
|
|
||||||
* @throws IOException <p>If the writer fails to write.</p>
|
|
||||||
*/
|
|
||||||
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.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the anime converter
|
* Initializes the anime converter
|
||||||
|
*
|
||||||
* @throws IOException <p>If reading or writing fails.</p>
|
* @throws IOException <p>If reading or writing fails.</p>
|
||||||
*/
|
*/
|
||||||
private static void animeConverter() throws IOException {
|
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: ");
|
"necessary true/false] [Prevent signs&songs subtitles true/false]\nYour input: ");
|
||||||
List<String> input = readInput(4);
|
List<String> input = readInput(4);
|
||||||
String[] audioLang = new String[]{"jpn", "*"};
|
String[] audioLang = new String[]{"jpn", "*"};
|
||||||
String[] subtitleLang = new String[]{"eng", "*"};
|
String[] subtitleLang = new String[]{"eng", "*"};
|
||||||
boolean toStereo = true;
|
boolean toStereo = true;
|
||||||
boolean preventSigns = true;
|
boolean preventSigns = true;
|
||||||
if (input.size() > 0 && getList(input, 0) != null) {
|
if (input.size() > 0 && ListUtil.getList(input, 0) != null) {
|
||||||
audioLang = getList(input, 0);
|
audioLang = ListUtil.getList(input, 0);
|
||||||
}
|
}
|
||||||
if (input.size() > 1 && getList(input, 1) != null) {
|
if (input.size() > 1 && ListUtil.getList(input, 1) != null) {
|
||||||
subtitleLang = getList(input, 1);
|
subtitleLang = ListUtil.getList(input, 1);
|
||||||
}
|
}
|
||||||
if (input.size() > 2) {
|
if (input.size() > 2) {
|
||||||
toStereo = Boolean.parseBoolean(input.get(2));
|
toStereo = Boolean.parseBoolean(input.get(2));
|
||||||
@ -164,34 +106,18 @@ public class Main {
|
|||||||
if (input.size() > 3) {
|
if (input.size() > 3) {
|
||||||
preventSigns = Boolean.parseBoolean(input.get(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 <p>A list of tokens.</p>
|
|
||||||
* @param index <p>The index of the token containing comma separated entries.</p>
|
|
||||||
* @return <p>A string list.</p>
|
|
||||||
*/
|
|
||||||
private static String[] getList(List<String> 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
|
* Reads a number of tokens from the user input
|
||||||
|
*
|
||||||
* @param max <p>The number of tokens expected.</p>
|
* @param max <p>The number of tokens expected.</p>
|
||||||
* @return <p>A list of tokens.</p>
|
* @return <p>A list of tokens.</p>
|
||||||
*/
|
*/
|
||||||
private static List<String> readInput(int max) {
|
private static List<String> readInput(int max) {
|
||||||
List<String> tokens = tokenize(READER.nextLine());
|
List<String> tokens = tokenize(READER.nextLine());
|
||||||
if (max < tokens.size()) {
|
if (max < tokens.size()) {
|
||||||
throw new IllegalArgumentException("Input contains " + tokens.size() +
|
throw new IllegalArgumentException("Input contains " + tokens.size() +
|
||||||
" arguments, but the input only supports " + max + " arguments.");
|
" arguments, but the input only supports " + max + " arguments.");
|
||||||
@ -201,15 +127,16 @@ public class Main {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the user's choice
|
* Gets the user's choice
|
||||||
|
*
|
||||||
* @param prompt <p>The prompt shown to the user.</p>
|
* @param prompt <p>The prompt shown to the user.</p>
|
||||||
* @return <p>The non-empty choice given by the user.</p>
|
* @return <p>The non-empty choice given by the user.</p>
|
||||||
* @throws IOException <p>If reading or writing fails.</p>
|
* @throws IOException <p>If reading or writing fails.</p>
|
||||||
*/
|
*/
|
||||||
private static String getChoice(String prompt) throws IOException {
|
private static String getChoice(String prompt) throws IOException {
|
||||||
printl(prompt);
|
OutputUtil.println(prompt);
|
||||||
String choice = "";
|
String choice = "";
|
||||||
while (choice.equals("")) {
|
while (choice.equals("")) {
|
||||||
printl("Your input: ");
|
OutputUtil.println("Your input: ");
|
||||||
choice = READER.nextLine();
|
choice = READER.nextLine();
|
||||||
}
|
}
|
||||||
return choice;
|
return choice;
|
||||||
@ -217,66 +144,25 @@ public class Main {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an integer from the user
|
* Gets an integer from the user
|
||||||
* @param prompt The prompt to give the user
|
*
|
||||||
* @param min The minimum allowed value
|
* @param prompt The prompt to give the user
|
||||||
* @param max The maximum allowed value
|
* @param min The minimum allowed value
|
||||||
* @return The value given by the user
|
* @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 {
|
private static int getChoice(String prompt, int min, int max) throws IOException {
|
||||||
printl(prompt);
|
OutputUtil.println(prompt);
|
||||||
int choice = 0;
|
int choice = 0;
|
||||||
while (choice < min || choice > max) {
|
while (choice < min || choice > max) {
|
||||||
printl("Your input: ");
|
OutputUtil.println("Your input: ");
|
||||||
try {
|
try {
|
||||||
choice = Integer.parseInt(READER.next());
|
choice = Integer.parseInt(READER.next());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
printl("Invalid choice. Please try again.");
|
OutputUtil.println("Invalid choice. Please try again.");
|
||||||
} finally {
|
} finally {
|
||||||
READER.nextLine();
|
READER.nextLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return choice;
|
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 <T> Anything which can be stored in a list
|
|
||||||
* @return True if at least one element fulfills the predicate
|
|
||||||
*/
|
|
||||||
static <T> boolean listContains(T[] list, Predicate<T> 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user