Simplifies and improves code. Removes some duplication

This commit is contained in:
2020-05-12 00:19:01 +02:00
parent 9673266c09
commit e1256f61c7
10 changed files with 237 additions and 221 deletions

@ -16,7 +16,7 @@ import java.util.Scanner;
import static net.knarcraft.ffmpegconverter.utility.Parser.tokenize;
/**
* Converts a files or files in a folder to a web playable mp4.
* The main class for starting the software
*/
class Main {
private static final String FFPROBE_PATH = "ffprobe"; //Can be just ffprobe if it's in the path
@ -25,27 +25,7 @@ class Main {
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)\""));
//parser(tokenizer("AnimeConverter \"C:\\Users\\Kristian\\Downloads\\Anime\\[Kametsu] ERASED (BD 1080p Hi10 FLAC)\""));
//System.exit(1);
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" +
"3. VideoStream converter", 1, 3);
OutputUtil.println("Input for this converter:");
switch (choice) {
case 1:
animeConverter();
break;
case 2:
converter = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
break;
case 3:
converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
break;
default:
System.exit(1);
}
loadConverter();
int recursionSteps = 1;
@ -64,8 +44,46 @@ class Main {
}
}
if (folder.isDirectory()) {
File[] files = FileUtil.listFilesRecursive(folder, converter.getValidFormats(), recursionSteps);
convertAllFiles(folder, recursionSteps);
OutputUtil.close();
}
/**
* Asks the user which converter they want, and assigns a converter instance to the converter variable
*
* @throws IOException <p>If there's a problem getting user input.</p>
*/
private static void loadConverter() throws IOException {
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" +
"3. VideoStream converter", 1, 3);
OutputUtil.println("Input for this converter:");
switch (choice) {
case 1:
animeConverter();
break;
case 2:
converter = new AudioConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
break;
case 3:
converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
break;
default:
System.exit(1);
}
}
/**
* Converts the file(s) as specified
*
* @param fileOrFolder <p>A file or a folder.</p>
* @param recursionSteps <p>The depth to recurse if a folder is given.</p>
* @throws IOException <p>If conversion or writing fails.</p>
*/
private static void convertAllFiles(File fileOrFolder, int recursionSteps) throws IOException {
if (fileOrFolder.isDirectory()) {
File[] files = FileUtil.listFilesRecursive(fileOrFolder, converter.getValidFormats(), recursionSteps);
if (files != null && files.length > 0) {
for (File file : files) {
converter.convert(file);
@ -73,12 +91,11 @@ class Main {
} else {
OutputUtil.println("No valid files found in folder.");
}
} else if (folder.exists()) {
converter.convert(folder);
} else if (fileOrFolder.exists()) {
converter.convert(fileOrFolder);
} else {
OutputUtil.println("Path " + folder.getAbsolutePath() + " does not point to any file or folder.");
OutputUtil.println("Path " + fileOrFolder.getAbsolutePath() + " does not point to any file or folder.");
}
OutputUtil.close();
}
/**
@ -152,8 +169,8 @@ class Main {
*/
private static int getChoice(String prompt, int min, int max) throws IOException {
OutputUtil.println(prompt);
int choice = 0;
while (choice < min || choice > max) {
int choice = Integer.MIN_VALUE;
do {
OutputUtil.println("Your input: ");
try {
choice = Integer.parseInt(READER.next());
@ -162,7 +179,7 @@ class Main {
} finally {
READER.nextLine();
}
}
} while (choice < min || choice > max);
return choice;
}
}