Improves some method names and comments. Refactors parser
Some checks failed
KnarCraft/FFmpegConvert/pipeline/head There was a failure building this commit

This commit is contained in:
Kristian Knarvik 2020-05-12 01:05:52 +02:00
parent e1256f61c7
commit f59152a819
13 changed files with 96 additions and 66 deletions

View File

@ -111,11 +111,11 @@ class Main {
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 && ListUtil.getList(input, 0) != null) { if (input.size() > 0 && ListUtil.getListFromCommaSeparatedStringList(input, 0) != null) {
audioLang = ListUtil.getList(input, 0); audioLang = ListUtil.getListFromCommaSeparatedStringList(input, 0);
} }
if (input.size() > 1 && ListUtil.getList(input, 1) != null) { if (input.size() > 1 && ListUtil.getListFromCommaSeparatedStringList(input, 1) != null) {
subtitleLang = ListUtil.getList(input, 1); subtitleLang = ListUtil.getListFromCommaSeparatedStringList(input, 1);
} }
if (input.size() > 2) { if (input.size() > 2) {
toStereo = Boolean.parseBoolean(input.get(2)); toStereo = Boolean.parseBoolean(input.get(2));

View File

@ -130,7 +130,7 @@ public abstract class AbstractConverter implements Converter {
OutputUtil.println(); OutputUtil.println();
OutputUtil.println("Preparing to start process..."); OutputUtil.println("Preparing to start process...");
OutputUtil.println("Converting " + file); OutputUtil.println("Converting " + file);
ProcessBuilder processBuilder = new ProcessBuilder(builderCommand(ffmpegPath, file, streams, newPath)); ProcessBuilder processBuilder = new ProcessBuilder(generateConversionCommand(ffmpegPath, file, streams, newPath));
FFMpegHelper.runProcess(processBuilder, folder, "\n", true); FFMpegHelper.runProcess(processBuilder, folder, "\n", true);
} }

View File

@ -47,7 +47,7 @@ public class AnimeConverter extends AbstractConverter {
} }
@Override @Override
public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName()); List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName());
if (this.DEBUG) { if (this.DEBUG) {
FFMpegHelper.addDebugArguments(command, 50, 120); FFMpegHelper.addDebugArguments(command, 50, 120);

View File

@ -27,7 +27,7 @@ public class AudioConverter extends AbstractConverter {
} }
@Override @Override
public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName());
if (this.DEBUG) { if (this.DEBUG) {
FFMpegHelper.addDebugArguments(command, 50, 120); FFMpegHelper.addDebugArguments(command, 50, 120);

View File

@ -28,5 +28,5 @@ public interface Converter {
* @param outFile <p>The output file.</p> * @param outFile <p>The output file.</p>
* @return <p>A list of commands</p> * @return <p>A list of commands</p>
*/ */
String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile); String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile);
} }

View File

@ -29,7 +29,7 @@ public class VideoConverter extends AbstractConverter {
} }
@Override @Override
public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName());
if (this.DEBUG) { if (this.DEBUG) {
FFMpegHelper.addDebugArguments(command, 50, 120); FFMpegHelper.addDebugArguments(command, 50, 120);

View File

@ -1,7 +1,7 @@
package net.knarcraft.ffmpegconverter.streams; package net.knarcraft.ffmpegconverter.streams;
/** /**
* An object representation of a stream in a media file * An abstract implementation of a stream object implementing common methods
*/ */
public abstract class AbstractStream implements StreamObject { public abstract class AbstractStream implements StreamObject {
int absoluteIndex; int absoluteIndex;

View File

@ -17,7 +17,8 @@ public class AudioStream extends AbstractStream implements StreamObject {
* @param title <p>The title of the audio stream.</p> * @param title <p>The title of the audio stream.</p>
* @param channels <p>The number of channels for the audio stream.</p> * @param channels <p>The number of channels for the audio stream.</p>
*/ */
public AudioStream(String codecName, int absoluteIndex, int relativeIndex, String language, String title, int channels) { public AudioStream(String codecName, int absoluteIndex, int relativeIndex, String language, String title,
int channels) {
this.codecName = codecName; this.codecName = codecName;
this.absoluteIndex = absoluteIndex; this.absoluteIndex = absoluteIndex;
this.language = language; this.language = language;

View File

@ -27,33 +27,6 @@ public final class FileUtil {
FileUtil.stripExtension(file) + "." + outExtension, outExtension); FileUtil.stripExtension(file) + "." + outExtension, outExtension);
} }
/**
* Adds parentheses with an integer if the output file already exists
*
* @param targetPath <p>The path the file should ideally be saved at.</p>
* @param extension <p>The extension of the target file.</p>
* @return <p>A filename guaranteed not to collide with other files.</p>
*/
public static String getNonCollidingFilename(String targetPath, String extension) {
File newFile = new File(targetPath);
String fileName = stripExtension(targetPath);
int i = 1;
while (newFile.exists()) {
newFile = new File(fileName + "(" + i++ + ")" + "." + extension);
}
return newFile.toString();
}
/**
* Gets filename without extension from File object
*
* @param file <p>A file object.</p>
* @return <p>A filename.</p>
*/
public static String stripExtension(File file) {
return file.getName().substring(0, file.getName().lastIndexOf('.'));
}
/** /**
* Removes the extension from a file name * Removes the extension from a file name
* *
@ -134,4 +107,31 @@ public final class FileUtil {
return classloader.getResourceAsStream(resourceName); return classloader.getResourceAsStream(resourceName);
} }
/**
* Adds parentheses with an integer if the output file already exists
*
* @param targetPath <p>The path the file should ideally be saved at.</p>
* @param extension <p>The extension of the target file.</p>
* @return <p>A filename guaranteed not to collide with other files.</p>
*/
private static String getNonCollidingFilename(String targetPath, String extension) {
File newFile = new File(targetPath);
String fileName = stripExtension(targetPath);
int i = 1;
while (newFile.exists()) {
newFile = new File(fileName + "(" + i++ + ")" + "." + extension);
}
return newFile.toString();
}
/**
* Gets filename without extension from File object
*
* @param file <p>A file object.</p>
* @return <p>A filename.</p>
*/
private static String stripExtension(File file) {
return file.getName().substring(0, file.getName().lastIndexOf('.'));
}
} }

View File

@ -40,11 +40,9 @@ public final class ListUtil {
* @return <p>A new list containing all matching elements.</p> * @return <p>A new list containing all matching elements.</p>
*/ */
static <T> List<T> getMatching(List<T> list, Predicate<T> predicate) { static <T> List<T> getMatching(List<T> list, Predicate<T> predicate) {
list = new ArrayList<>(list); List<T> matching = new ArrayList<>(list);
List<T> copy = new ArrayList<>(list); matching.removeIf(predicate.negate());
list.removeIf(predicate); return matching;
copy.removeAll(list);
return copy;
} }
/** /**
@ -71,7 +69,7 @@ public final class ListUtil {
* @param index <p>The index of the token containing comma separated entries.</p> * @param index <p>The index of the token containing comma separated entries.</p>
* @return <p>A string list.</p> * @return <p>A string list.</p>
*/ */
public static String[] getList(List<String> list, int index) { public static String[] getListFromCommaSeparatedStringList(List<String> list, int index) {
String[] result = null; String[] result = null;
if (list.size() > index) { if (list.size() > index) {
if (list.get(index).contains(",")) { if (list.get(index).contains(",")) {

View File

@ -58,18 +58,12 @@ public final class Parser {
List<String> tokens = new ArrayList<>(); List<String> tokens = new ArrayList<>();
boolean startedQuote = false; boolean startedQuote = false;
StringBuilder currentToken = new StringBuilder(); StringBuilder currentToken = new StringBuilder();
for (int i = 0; i < input.length(); i++) { for (int index = 0; index < input.length(); index++) {
char character = input.charAt(i); char character = input.charAt(index);
switch (character) { switch (character) {
case ' ': case ' ':
if (!startedQuote) { if (tokenizeSpace(startedQuote, currentToken, tokens)) {
//If not inside "", a space marks the end of a parameter
if (isNotEmpty(currentToken)) {
tokens.add(currentToken.toString());
}
currentToken = new StringBuilder(); currentToken = new StringBuilder();
} else {
currentToken.append(character);
} }
break; break;
case '"': case '"':
@ -87,17 +81,51 @@ public final class Parser {
} }
break; break;
default: default:
//Adds a normal character to the token. Adds the current token to tokens if at the end of the input. tokenizeNormalCharacter(currentToken, character, input.length(), index, tokens);
currentToken.append(character);
if (i == input.length() - 1) {
tokens.add(currentToken.toString());
}
break; break;
} }
} }
return tokens; return tokens;
} }
/**
* Adds a normal character to the token. Adds the current token to tokens if at the end of the input
*
* @param currentToken <p>The string builder containing the current token.</p>
* @param character <p>The character found in the input.</p>
* @param inputLength <p>The length of the given input</p>
* @param index <p>The index of the read character.</p>
* @param tokens <p>The list of processed tokens.</p>
*/
private static void tokenizeNormalCharacter(StringBuilder currentToken, char character, int inputLength, int index,
List<String> tokens) {
currentToken.append(character);
if (index == inputLength - 1) {
tokens.add(currentToken.toString());
}
}
/**
* Tokenizes a space character
*
* @param startedQuote <p>Whether this space is inside a pair of quotes.</p>
* @param currentToken <p>The string builder containing the current token.</p>
* @param tokens <p>The list of processed tokens.</p>
* @return <p>True if the token is finished.</p>
*/
private static boolean tokenizeSpace(boolean startedQuote, StringBuilder currentToken, List<String> tokens) {
if (!startedQuote) {
//If not inside "", a space marks the end of a parameter
if (isNotEmpty(currentToken)) {
tokens.add(currentToken.toString());
}
return true;
} else {
currentToken.append(' ');
return false;
}
}
/** /**
* Checks whether a string builder is empty * Checks whether a string builder is empty
* *

View File

@ -17,13 +17,16 @@ final class StringUtil {
* @return <p>A list of all occurrences of the substring.</p> * @return <p>A list of all occurrences of the substring.</p>
*/ */
static String[] stringBetween(String string, String start, String end) { static String[] stringBetween(String string, String start, String end) {
int startPos = string.indexOf(start) + start.length(); int startPosition = string.indexOf(start) + start.length();
if (!string.contains(start) || string.indexOf(end, startPos) < startPos) { //Return if the string is not found
if (!string.contains(start) || string.indexOf(end, startPosition) < startPosition) {
return new String[]{}; return new String[]{};
} }
int endPos = string.indexOf(end, startPos); int endPosition = string.indexOf(end, startPosition);
String outString = string.substring(startPos, endPos).trim(); //Get the string between the start and end string
String nextString = string.substring(endPos + end.length()); String outString = string.substring(startPosition, endPosition).trim();
String nextString = string.substring(endPosition + end.length());
//Add other occurrences recursively
return ListUtil.concatenate(new String[]{outString}, stringBetween(nextString, start, end)); return ListUtil.concatenate(new String[]{outString}, stringBetween(nextString, start, end));
} }
} }

View File

@ -78,9 +78,9 @@ public class ListUtilTest {
inputList.add("some test string"); inputList.add("some test string");
inputList.add("some,test,string"); inputList.add("some,test,string");
inputList.add("some te,st string"); inputList.add("some te,st string");
String[] result1 = ListUtil.getList(inputList, 0); String[] result1 = ListUtil.getListFromCommaSeparatedStringList(inputList, 0);
String[] result2 = ListUtil.getList(inputList, 1); String[] result2 = ListUtil.getListFromCommaSeparatedStringList(inputList, 1);
String[] result3 = ListUtil.getList(inputList, 2); String[] result3 = ListUtil.getListFromCommaSeparatedStringList(inputList, 2);
assertArrayEquals(new String[]{"some test string"}, result1); assertArrayEquals(new String[]{"some test string"}, result1);
assertArrayEquals(new String[]{"some", "test", "string"}, result2); assertArrayEquals(new String[]{"some", "test", "string"}, result2);
assertArrayEquals(new String[]{"some te", "st string"}, result3); assertArrayEquals(new String[]{"some te", "st string"}, result3);