Properly fixes conversion of files with filenames containing square brackets
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good

Unknown whether this breaks conversion of filenames containing semicolons or apostrophes
This commit is contained in:
Kristian Knarvik 2020-03-15 18:25:04 +01:00
parent 4a23f91357
commit 0f9fbc06c7
2 changed files with 12 additions and 8 deletions

View File

@ -50,7 +50,8 @@ public class AnimeConverter extends Converter {
if (streams.isEmpty()) { if (streams.isEmpty()) {
throw new IllegalArgumentException("The file has no valid streams. Please make sure the file exists and is not corrupt."); 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"); String newPath = fileCollisionPrevention(folder.getAbsolutePath() + File.separator +
stripExtension(file) + ".mp4", "mp4");
printl("Preparing to start process..."); printl("Preparing to start process...");
String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath); String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath);
ProcessBuilder processBuilder = new ProcessBuilder(command); ProcessBuilder processBuilder = new ProcessBuilder(command);
@ -108,17 +109,20 @@ public class AnimeConverter extends Converter {
if (subtitleStream != null && subtitleStream.getIsImageSubtitle()) { if (subtitleStream != null && subtitleStream.getIsImageSubtitle()) {
command.add("-filter_complex"); command.add("-filter_complex");
command.add("[0:v:" + videoStream.getAbsoluteIndex() + "][0:" + subtitleStream.getAbsoluteIndex() + "]overlay"); String filter = String.format("[0:v:%d][0:%d]overlay", videoStream.getAbsoluteIndex(),
subtitleStream.getAbsoluteIndex());
command.add(filter);
} else if (subtitleStream != null) { } else if (subtitleStream != null) {
command.add("-map"); command.add("-map");
command.add("0:" + videoStream.getAbsoluteIndex()); command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
command.add("-vf"); command.add("-vf");
String safeFileName = escapeSpecialCharactersInFileName(fileName); String safeFileName = escapeSpecialCharactersInFileName(fileName);
String subtitleCommand = String.format("subtitles=\"%s\"", safeFileName); String subtitleCommand = String.format("subtitles='%s':si=%d", safeFileName,
subtitleStream.getRelativeIndex());
command.add(subtitleCommand); command.add(subtitleCommand);
} else { } else {
command.add("-map"); command.add("-map");
command.add("0:" + videoStream.getAbsoluteIndex()); command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
} }
command.add(outFile); command.add(outFile);

View File

@ -427,9 +427,9 @@ public abstract class Converter {
static String escapeSpecialCharactersInFileName(String fileName) { static String escapeSpecialCharactersInFileName(String fileName) {
return fileName.replace("'", "\\\\\\'") return fileName.replace("'", "\\\\\\'")
.replace(",", "\\\\\\,") .replace(",", "\\\\\\,")
.replace(";", "\\\\\\;"); .replace(";", "\\\\\\;")
/*.replace("]", "\\]") //Not sure whether these should be escaped or not .replace("]", "\\]")
.replace("[", "\\[");*/ .replace("[", "\\[");
} }
static void print(String input) throws IOException { static void print(String input) throws IOException {