Improves some method names and comments. Refactors parser
Some checks failed
KnarCraft/FFmpegConvert/pipeline/head There was a failure building this commit
Some checks failed
KnarCraft/FFmpegConvert/pipeline/head There was a failure building this commit
This commit is contained in:
parent
e1256f61c7
commit
f59152a819
@ -111,11 +111,11 @@ class Main {
|
||||
String[] subtitleLang = new String[]{"eng", "*"};
|
||||
boolean toStereo = true;
|
||||
boolean preventSigns = true;
|
||||
if (input.size() > 0 && ListUtil.getList(input, 0) != null) {
|
||||
audioLang = ListUtil.getList(input, 0);
|
||||
if (input.size() > 0 && ListUtil.getListFromCommaSeparatedStringList(input, 0) != null) {
|
||||
audioLang = ListUtil.getListFromCommaSeparatedStringList(input, 0);
|
||||
}
|
||||
if (input.size() > 1 && ListUtil.getList(input, 1) != null) {
|
||||
subtitleLang = ListUtil.getList(input, 1);
|
||||
if (input.size() > 1 && ListUtil.getListFromCommaSeparatedStringList(input, 1) != null) {
|
||||
subtitleLang = ListUtil.getListFromCommaSeparatedStringList(input, 1);
|
||||
}
|
||||
if (input.size() > 2) {
|
||||
toStereo = Boolean.parseBoolean(input.get(2));
|
||||
|
@ -130,7 +130,7 @@ public abstract class AbstractConverter implements Converter {
|
||||
OutputUtil.println();
|
||||
OutputUtil.println("Preparing to start process...");
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ public class AnimeConverter extends AbstractConverter {
|
||||
}
|
||||
|
||||
@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());
|
||||
if (this.DEBUG) {
|
||||
FFMpegHelper.addDebugArguments(command, 50, 120);
|
||||
|
@ -27,7 +27,7 @@ public class AudioConverter extends AbstractConverter {
|
||||
}
|
||||
|
||||
@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());
|
||||
if (this.DEBUG) {
|
||||
FFMpegHelper.addDebugArguments(command, 50, 120);
|
||||
|
@ -28,5 +28,5 @@ public interface Converter {
|
||||
* @param outFile <p>The output file.</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);
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ public class VideoConverter extends AbstractConverter {
|
||||
}
|
||||
|
||||
@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());
|
||||
if (this.DEBUG) {
|
||||
FFMpegHelper.addDebugArguments(command, 50, 120);
|
||||
|
@ -1,7 +1,7 @@
|
||||
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 {
|
||||
int absoluteIndex;
|
||||
|
@ -17,7 +17,8 @@ public class AudioStream extends AbstractStream implements StreamObject {
|
||||
* @param title <p>The title of 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.absoluteIndex = absoluteIndex;
|
||||
this.language = language;
|
||||
|
@ -27,33 +27,6 @@ public final class FileUtil {
|
||||
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
|
||||
*
|
||||
@ -134,4 +107,31 @@ public final class FileUtil {
|
||||
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('.'));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,11 +40,9 @@ public final class ListUtil {
|
||||
* @return <p>A new list containing all matching elements.</p>
|
||||
*/
|
||||
static <T> List<T> getMatching(List<T> list, Predicate<T> predicate) {
|
||||
list = new ArrayList<>(list);
|
||||
List<T> copy = new ArrayList<>(list);
|
||||
list.removeIf(predicate);
|
||||
copy.removeAll(list);
|
||||
return copy;
|
||||
List<T> matching = new ArrayList<>(list);
|
||||
matching.removeIf(predicate.negate());
|
||||
return matching;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,7 +69,7 @@ public final class ListUtil {
|
||||
* @param index <p>The index of the token containing comma separated entries.</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;
|
||||
if (list.size() > index) {
|
||||
if (list.get(index).contains(",")) {
|
||||
|
@ -58,18 +58,12 @@ public final class Parser {
|
||||
List<String> tokens = new ArrayList<>();
|
||||
boolean startedQuote = false;
|
||||
StringBuilder currentToken = new StringBuilder();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char character = input.charAt(i);
|
||||
for (int index = 0; index < input.length(); index++) {
|
||||
char character = input.charAt(index);
|
||||
switch (character) {
|
||||
case ' ':
|
||||
if (!startedQuote) {
|
||||
//If not inside "", a space marks the end of a parameter
|
||||
if (isNotEmpty(currentToken)) {
|
||||
tokens.add(currentToken.toString());
|
||||
}
|
||||
if (tokenizeSpace(startedQuote, currentToken, tokens)) {
|
||||
currentToken = new StringBuilder();
|
||||
} else {
|
||||
currentToken.append(character);
|
||||
}
|
||||
break;
|
||||
case '"':
|
||||
@ -87,17 +81,51 @@ public final class Parser {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//Adds a normal character to the token. Adds the current token to tokens if at the end of the input.
|
||||
currentToken.append(character);
|
||||
if (i == input.length() - 1) {
|
||||
tokens.add(currentToken.toString());
|
||||
}
|
||||
tokenizeNormalCharacter(currentToken, character, input.length(), index, tokens);
|
||||
break;
|
||||
}
|
||||
}
|
||||
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
|
||||
*
|
||||
|
@ -17,13 +17,16 @@ final class StringUtil {
|
||||
* @return <p>A list of all occurrences of the substring.</p>
|
||||
*/
|
||||
static String[] stringBetween(String string, String start, String end) {
|
||||
int startPos = string.indexOf(start) + start.length();
|
||||
if (!string.contains(start) || string.indexOf(end, startPos) < startPos) {
|
||||
int startPosition = string.indexOf(start) + start.length();
|
||||
//Return if the string is not found
|
||||
if (!string.contains(start) || string.indexOf(end, startPosition) < startPosition) {
|
||||
return new String[]{};
|
||||
}
|
||||
int endPos = string.indexOf(end, startPos);
|
||||
String outString = string.substring(startPos, endPos).trim();
|
||||
String nextString = string.substring(endPos + end.length());
|
||||
int endPosition = string.indexOf(end, startPosition);
|
||||
//Get the string between the start and end string
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -78,9 +78,9 @@ public class ListUtilTest {
|
||||
inputList.add("some test string");
|
||||
inputList.add("some,test,string");
|
||||
inputList.add("some te,st string");
|
||||
String[] result1 = ListUtil.getList(inputList, 0);
|
||||
String[] result2 = ListUtil.getList(inputList, 1);
|
||||
String[] result3 = ListUtil.getList(inputList, 2);
|
||||
String[] result1 = ListUtil.getListFromCommaSeparatedStringList(inputList, 0);
|
||||
String[] result2 = ListUtil.getListFromCommaSeparatedStringList(inputList, 1);
|
||||
String[] result3 = ListUtil.getListFromCommaSeparatedStringList(inputList, 2);
|
||||
assertArrayEquals(new String[]{"some test string"}, result1);
|
||||
assertArrayEquals(new String[]{"some", "test", "string"}, result2);
|
||||
assertArrayEquals(new String[]{"some te", "st string"}, result3);
|
||||
|
Loading…
Reference in New Issue
Block a user