All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
188 lines
7.0 KiB
Java
188 lines
7.0 KiB
Java
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.VideoConverter;
|
|
import net.knarcraft.ffmpegconverter.converter.WebVideoConverter;
|
|
import net.knarcraft.ffmpegconverter.utility.FileUtil;
|
|
import net.knarcraft.ffmpegconverter.utility.ListUtil;
|
|
import net.knarcraft.ffmpegconverter.utility.OutputUtil;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
|
|
import static net.knarcraft.ffmpegconverter.utility.Parser.tokenize;
|
|
|
|
/**
|
|
* 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
|
|
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 AbstractConverter converter = null;
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
loadConverter();
|
|
|
|
List<String> input;
|
|
do {
|
|
OutputUtil.println("<Folder/File> [Recursions]:");
|
|
input = readInput(2);
|
|
} while (input.isEmpty());
|
|
File folder = new File(input.get(0));
|
|
|
|
int recursionSteps = 1;
|
|
if (input.size() > 1) {
|
|
try {
|
|
recursionSteps = Integer.parseInt(input.get(1));
|
|
} catch (NumberFormatException e) {
|
|
OutputUtil.println("Recursion steps is invalid and will be ignored.");
|
|
}
|
|
}
|
|
|
|
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. Video converter\n4. Web video converter", 1, 4);
|
|
|
|
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;
|
|
case 4:
|
|
converter = new WebVideoConverter(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);
|
|
}
|
|
} else {
|
|
OutputUtil.println("No valid files found in folder.");
|
|
}
|
|
} else if (fileOrFolder.exists()) {
|
|
converter.convert(fileOrFolder);
|
|
} else {
|
|
OutputUtil.println("Path " + fileOrFolder.getAbsolutePath() + " does not point to any file or folder.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initializes the anime converter
|
|
*
|
|
* @throws IOException <p>If reading or writing fails.</p>
|
|
*/
|
|
private static void animeConverter() throws IOException {
|
|
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<String> input = readInput(4);
|
|
String[] audioLanguage = new String[]{"jpn", "0"};
|
|
String[] subtitleLanguage = new String[]{"eng", "0"};
|
|
boolean toStereo = true;
|
|
boolean preventSigns = true;
|
|
if (input.size() > 0 && ListUtil.getListFromCommaSeparatedString(input.get(0)) != null) {
|
|
audioLanguage = ListUtil.getListFromCommaSeparatedString(input.get(0));
|
|
}
|
|
if (input.size() > 1 && ListUtil.getListFromCommaSeparatedString(input.get(1)) != null) {
|
|
subtitleLanguage = ListUtil.getListFromCommaSeparatedString(input.get(1));
|
|
}
|
|
if (input.size() > 2) {
|
|
toStereo = Boolean.parseBoolean(input.get(2));
|
|
}
|
|
if (input.size() > 3) {
|
|
preventSigns = Boolean.parseBoolean(input.get(3));
|
|
}
|
|
converter = new AnimeConverter(FFPROBE_PATH, FFMPEG_PATH, audioLanguage, subtitleLanguage, toStereo, preventSigns);
|
|
}
|
|
|
|
|
|
/**
|
|
* Reads a number of tokens from the user input
|
|
*
|
|
* @param max <p>The number of tokens expected.</p>
|
|
* @return <p>A list of tokens.</p>
|
|
*/
|
|
private static List<String> readInput(int max) {
|
|
List<String> tokens = tokenize(READER.nextLine());
|
|
if (max < tokens.size()) {
|
|
throw new IllegalArgumentException("Input contains " + tokens.size() +
|
|
" arguments, but the input only supports " + max + " arguments.");
|
|
}
|
|
return tokens;
|
|
}
|
|
|
|
/**
|
|
* Gets the user's choice
|
|
*
|
|
* @param prompt <p>The prompt shown to the user.</p>
|
|
* @return <p>The non-empty choice given by the user.</p>
|
|
* @throws IOException <p>If reading or writing fails.</p>
|
|
*/
|
|
private static String getChoice(String prompt) throws IOException {
|
|
OutputUtil.println(prompt);
|
|
String choice = "";
|
|
while (choice.equals("")) {
|
|
OutputUtil.println("Your input: ");
|
|
choice = READER.nextLine();
|
|
}
|
|
return choice;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
private static int getChoice(String prompt, int min, int max) throws IOException {
|
|
OutputUtil.println(prompt);
|
|
int choice = Integer.MIN_VALUE;
|
|
do {
|
|
OutputUtil.println("Your input: ");
|
|
try {
|
|
choice = Integer.parseInt(READER.next());
|
|
} catch (NumberFormatException e) {
|
|
OutputUtil.println("Invalid choice. Please try again.");
|
|
} finally {
|
|
READER.nextLine();
|
|
}
|
|
} while (choice < min || choice > max);
|
|
return choice;
|
|
}
|
|
}
|