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 The prompt shown to the user.
+ * @return The non-empty choice given by the user.
+ * @throws IOException If the reader can't be read.
+ */
+ 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;
diff --git a/src/ffmpegconverter/converter/AnimeConverter.java b/src/ffmpegconverter/converter/AnimeConverter.java
index 1b7e43a..240196b 100644
--- a/src/ffmpegconverter/converter/AnimeConverter.java
+++ b/src/ffmpegconverter/converter/AnimeConverter.java
@@ -46,11 +46,14 @@ public class AnimeConverter extends Converter {
*/
private void processFile(File folder, File file) throws IOException {
List 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);
}
/**
diff --git a/src/ffmpegconverter/converter/Converter.java b/src/ffmpegconverter/converter/Converter.java
index 73bd313..917b326 100644
--- a/src/ffmpegconverter/converter/Converter.java
+++ b/src/ffmpegconverter/converter/Converter.java
@@ -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();
+ }
}
diff --git a/src/ffmpegconverter/streams/SubtitleStream.java b/src/ffmpegconverter/streams/SubtitleStream.java
index a1c50b0..22a9e9d 100644
--- a/src/ffmpegconverter/streams/SubtitleStream.java
+++ b/src/ffmpegconverter/streams/SubtitleStream.java
@@ -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() {