Adds lots of improvements

This commit is contained in:
2020-02-10 22:50:35 +01:00
parent 20e6df04f1
commit b2e1cc83fc
7 changed files with 198 additions and 469 deletions

View File

@ -17,7 +17,8 @@ import java.util.function.Predicate;
public 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 Scanner in = 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 Converter con = null;
public static void main(String[] args) throws IOException {
@ -27,7 +28,7 @@ public class Main {
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n3. VideoStream converter", 1, 3);
System.out.println("Input for this converter:");
printl("Input for this converter:");
switch (choice) {
case 1:
animeConverter();
@ -44,10 +45,10 @@ public class Main {
int recursionSteps = 1;
System.out.println("<Folder/File> [Recursions]");
printl("<Folder/File> [Recursions]: ");
List<String> input = readInput(2);
while (input.size() == 0) {
System.out.print("File path required.");
while (input.isEmpty()) {
print("File path required.");
input = readInput(2);
}
File folder = new File(input.get(0));
@ -55,7 +56,7 @@ public class Main {
try {
recursionSteps = Integer.parseInt(input.get(1));
} catch (NumberFormatException e) {
System.out.println("Recursion steps is invalid and will be ignored.");
printl("Recursion steps is invalid and will be ignored.");
}
}
@ -66,13 +67,35 @@ public class Main {
con.convert(file);
}
} else {
System.out.println("No valid files found in folder.");
printl("No valid files found in folder.");
}
} else if (folder.exists()) {
con.convert(folder);
} else {
System.out.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 can't be written to.</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 can't be written to.</p>
*/
private static void printl(String input) throws IOException {
WRITER.write(input);
WRITER.newLine();
WRITER.flush();
}
private enum converterArgumentValueType {
@ -108,7 +131,7 @@ public class Main {
case SINGLE_VALUE:
return !value.contains(" ");
case INT:
Integer.parseInt(value);
int ignored = Integer.parseInt(value);
return true;
}
return false;
@ -142,46 +165,59 @@ public class Main {
}
}
/**
* Tokenizes a string
* @param input <p>A string.</p>
* @return <p>A list of tokens.</p>
*/
private static List<String> tokenizer(String input) {
List<String> tokens = new ArrayList<>();
boolean startedQuote = false;
StringBuilder currentToken = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == ' ') {
if (!startedQuote) {
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
switch (c) {
case ' ':
if (!startedQuote) {
//If not inside "", a space marks the end of a parameter
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
} else {
currentToken = new StringBuilder();
}
} else {
currentToken = new StringBuilder();
currentToken.append(c);
}
} else {
break;
case '"':
if (startedQuote) {
if (!currentToken.toString().trim().equals("")) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
}
startedQuote = false;
} else {
startedQuote = true;
currentToken = new StringBuilder();
} break;
default:
currentToken.append(c);
}
} else if (c == '"') {
if (startedQuote) {
if (!currentToken.toString().trim().equals("")) {
if (i == input.length() - 1) {
tokens.add(currentToken.toString());
currentToken = new StringBuilder();
}
startedQuote = false;
} else {
startedQuote = true;
currentToken = new StringBuilder();
}
} else {
currentToken.append(c);
if (i == input.length() - 1) {
tokens.add(currentToken.toString());
}
} break;
}
}
return tokens;
}
private static void animeConverter() {
System.out.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: ");
/**
* Initializes the anime converter
* @throws IOException <p>If something goes wrong.</p>
*/
private static void animeConverter() throws IOException {
printl("[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[] audioLang = new String[]{"jpn", "*"};
String[] subtitleLang = new String[]{"eng", "*"};
@ -202,6 +238,12 @@ public class Main {
con = 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) {
@ -214,8 +256,13 @@ public class Main {
return result;
}
/**
* 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 = tokenizer(in.nextLine());
List<String> tokens = tokenizer(READER.nextLine());
if (max < tokens.size()) {
throw new IllegalArgumentException("Input contains " + tokens.size() +
" arguments, but the input only supports " + max + " arguments.");
@ -223,27 +270,40 @@ public class Main {
return tokens;
}
private static String getChoice(String prompt) {
System.out.println(prompt);
/**
* 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 the reader can't be read.</p>
*/
private static String getChoice(String prompt) throws IOException {
printl(prompt);
String choice = "";
while (choice.equals("")) {
System.out.println("Your input: ");
choice = in.nextLine();
printl("Your input: ");
choice = READER.nextLine();
}
return choice;
}
private static int getChoice(String prompt, int min, int max) {
System.out.println(prompt);
/**
* 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 {
printl(prompt);
int choice = 0;
while (choice < min || choice > max) {
System.out.println("Your input: ");
printl("Your input: ");
try {
choice = Integer.parseInt(in.next());
choice = Integer.parseInt(READER.next());
} catch (NumberFormatException e) {
System.out.println("Invalid choice. Please try again.");
printl("Invalid choice. Please try again.");
} finally {
in.nextLine();
READER.nextLine();
}
}
return choice;

View File

@ -46,11 +46,14 @@ public class AnimeConverter extends Converter {
*/
private void processFile(File folder, File file) throws IOException {
List<StreamObject> streams = probeFile(ffprobePath, file);
if (streams.size() == 0) {
if (streams.isEmpty()) {
throw new IllegalArgumentException("The file has no valid streams. Please make sure the file exists and is not corrupt.");
}
String newPath = fileCollisionPrevention(folder.getAbsolutePath() + File.separator + stripExtension(file) + ".mp4", "mp4");
convertProcess(new ProcessBuilder(builderCommand(ffmpegPath, file.getName(), streams, newPath)), folder);
printl("Preparing to start process...");
String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath);
ProcessBuilder processBuilder = new ProcessBuilder(command);
convertProcess(processBuilder, folder);
}
/**

View File

@ -6,9 +6,11 @@ import ffmpegconverter.streams.SubtitleStream;
import ffmpegconverter.streams.VideoStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
@ -21,6 +23,8 @@ import java.util.function.Predicate;
public abstract class Converter {
String ffprobePath;
String ffmpegPath;
private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
private static final String PROBE_SPLIT_CHARACTER = "øæåÆØå";
@ -51,7 +55,8 @@ public abstract class Converter {
"stream_tags=language,title:stream=index,codec_name,codec_type,channels",
file.toString()
);
System.out.println(builderProbe.command());
print("Probe command: ");
printl(builderProbe.command().toString());
builderProbe.redirectErrorStream(true);
Process processProbe = builderProbe.start();
BufferedReader readerProbe = new BufferedReader(new InputStreamReader(processProbe.getInputStream()));
@ -59,7 +64,7 @@ public abstract class Converter {
while (processProbe.isAlive()) {
String read = read(readerProbe, PROBE_SPLIT_CHARACTER);
if (!read.equals("")) {
System.out.print(read);
print(read);
output.append(read);
}
}
@ -82,7 +87,8 @@ public abstract class Converter {
* @throws IOException If the process can't be read
*/
static void convertProcess(ProcessBuilder process, File folder) throws IOException {
System.out.println(process.command());
print("Command to be run: ");
printl(process.command().toString());
process.directory(folder);
process.redirectErrorStream(true);
Process processConvert = process.start();
@ -90,9 +96,10 @@ public abstract class Converter {
while (processConvert.isAlive()) {
String read = read(readerConvert, "\n");
if (!read.equals("")) {
System.out.println(read);
printl(read);
}
}
printl("FFMPEG is finished.");
}
/**
@ -416,4 +423,19 @@ public abstract class Converter {
}
return new SubtitleStream(codecName, absoluteIndex, relativeIndex, language, title);
}
static void print(String input) throws IOException {
if (!input.equals("")) {
writer.write(input);
writer.flush();
}
}
static void printl(String input) throws IOException {
if (!input.equals("")) {
writer.write(input);
}
writer.newLine();
writer.flush();
}
}

View File

@ -4,10 +4,10 @@ package ffmpegconverter.streams;
* An object representation of a subtitle stream in a media file
*/
public class SubtitleStream extends StreamObject {
private String language;
private String title; //Title shown
private boolean isFullSubtitle; //Songs and signs will be false
private boolean isImageSubtitle;
final private String language;
final private String title; //Title shown
final private boolean isFullSubtitle; //Songs and signs will be false
final private boolean isImageSubtitle;
public SubtitleStream(String codecName, int absoluteIndex, int relativeIndex, String language, String title) {
this.codecType = "subtitle";
@ -36,9 +36,15 @@ public class SubtitleStream extends StreamObject {
if (getTitle() == null) {
return false;
}
String title = getTitle().toLowerCase();
return !(title.contains("songs and signs") || title.contains("songs & signs") || title.contains("songs ") ||
title.contains("signs/songs") || title.contains("[forced]") || title.contains("(forced)"));
String titleLowercase = getTitle().toLowerCase();
return !(titleLowercase.contains("songs and signs") ||
titleLowercase.contains("signs and songs") ||
titleLowercase.contains("songs & signs") ||
titleLowercase.contains("signs & songs") ||
titleLowercase.contains("signs/song") ||
titleLowercase.contains("songs/sign") ||
titleLowercase.contains("[forced]") ||
titleLowercase.contains("(forced)"));
}
public String getLanguage() {