From 0f9fbc06c76849be0e44a1a67ebc30d9d9185f28 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 15 Mar 2020 18:25:04 +0100 Subject: [PATCH] Properly fixes conversion of files with filenames containing square brackets Unknown whether this breaks conversion of filenames containing semicolons or apostrophes --- .../ffmpegconverter/converter/AnimeConverter.java | 14 +++++++++----- .../ffmpegconverter/converter/Converter.java | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java index 3a1555c..b800bf5 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java @@ -50,7 +50,8 @@ public class AnimeConverter extends Converter { 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"); + String newPath = fileCollisionPrevention(folder.getAbsolutePath() + File.separator + + stripExtension(file) + ".mp4", "mp4"); printl("Preparing to start process..."); String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath); ProcessBuilder processBuilder = new ProcessBuilder(command); @@ -108,17 +109,20 @@ public class AnimeConverter extends Converter { if (subtitleStream != null && subtitleStream.getIsImageSubtitle()) { 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) { command.add("-map"); - command.add("0:" + videoStream.getAbsoluteIndex()); + command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); command.add("-vf"); 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); } else { command.add("-map"); - command.add("0:" + videoStream.getAbsoluteIndex()); + command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); } command.add(outFile); diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java index 6dfc5e8..455c126 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java @@ -427,9 +427,9 @@ public abstract class Converter { static String escapeSpecialCharactersInFileName(String fileName) { return fileName.replace("'", "\\\\\\'") .replace(",", "\\\\\\,") - .replace(";", "\\\\\\;"); - /*.replace("]", "\\]") //Not sure whether these should be escaped or not - .replace("[", "\\[");*/ + .replace(";", "\\\\\\;") + .replace("]", "\\]") + .replace("[", "\\["); } static void print(String input) throws IOException {