From 405c94789bc04d530fc19e5812575403c56c076a Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 11 Apr 2020 00:58:12 +0200 Subject: [PATCH] Adds handling of external subtitles for anime --- .../converter/AnimeConverter.java | 29 ++++++++++++--- .../ffmpegconverter/converter/Converter.java | 35 +++++++++++++++++- .../converter/VideoConverter.java | 36 ++----------------- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java index 388550d..b286595 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java @@ -57,8 +57,10 @@ public class AnimeConverter extends Converter { } String newPath = fileCollisionPrevention(folder.getAbsolutePath() + File.separator + stripExtension(file) + ".mp4", "mp4"); + printl(); printl("Preparing to start process..."); - String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath); + printl("Converting " + file); + String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath, file); ProcessBuilder processBuilder = new ProcessBuilder(command); convertProcess(processBuilder, folder); } @@ -71,7 +73,8 @@ public class AnimeConverter extends Converter { * @param outFile

The output file.

* @return

A list of commands

*/ - private String[] builderCommand(String executable, String fileName, List streams, String outFile) { + private String[] builderCommand(String executable, String fileName, List streams, String outFile, + File file) { List command = ffmpegWebVideo(executable, fileName); if (this.DEBUG) { @@ -100,7 +103,7 @@ public class AnimeConverter extends Converter { } addAudioStreams(command, audioStream); - addSubtitles(command, subtitleStream, videoStream, fileName); + addSubtitles(command, subtitleStream, videoStream, fileName, file); command.add(outFile); return command.toArray(new String[0]); @@ -128,9 +131,13 @@ public class AnimeConverter extends Converter { * @param subtitleStream

The subtitle stream to be used.

* @param videoStream

The video stream to be used.

* @param fileName

The name of the file which is converted.

+ * @param file

The file to convert.

*/ private void addSubtitles(List command, SubtitleStream subtitleStream, VideoStream videoStream, - String fileName) { + String fileName, File file) { + File folder = file.getParentFile(); + String externalImageSubtitle = hasExternalImageSubtitle(folder.getAbsolutePath(), fileName); + String externalSubtitle = hasExternalSubtitle(folder.getAbsolutePath(), fileName); if (subtitleStream != null && subtitleStream.getIsImageSubtitle()) { command.add("-filter_complex"); String filter = String.format("[0:v:%d][0:%d]overlay", videoStream.getAbsoluteIndex(), @@ -144,6 +151,20 @@ public class AnimeConverter extends Converter { String subtitleCommand = String.format("subtitles='%s':si=%d", safeFileName, subtitleStream.getRelativeIndex()); command.add(subtitleCommand); + } else if (!externalSubtitle.equals("")) { + command.add("-map"); + command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); + command.add("-vf"); + String subtitleCommand = String.format("subtitles='%s'", escapeSpecialCharactersInFileName(externalSubtitle)); + command.add(subtitleCommand); + } else if (!externalImageSubtitle.equals("")) { + command.add("-i"); + command.add(stripExtension(fileName) + externalImageSubtitle); + command.add("-filter_complex"); + command.add(String.format("[1:s]scale=width=1272:height=720,crop=w=1272:h=720:x=0:y=out_h[sub];[%d:v]" + + "[sub]overlay", videoStream.getAbsoluteIndex())); + command.add("-profile:v"); + command.add("baseline"); } else { command.add("-map"); command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java index 9b5347b..8c0aba7 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java @@ -452,7 +452,7 @@ public abstract class Converter { * @param input

The text to print.

* @throws IOException

If a write is not possible.

*/ - static void print(String input) throws IOException { + private static void print(String input) throws IOException { if (!input.equals("")) { writer.write(input); writer.flush(); @@ -479,4 +479,37 @@ public abstract class Converter { writer.newLine(); writer.flush(); } + + /** + * Checks whether there exists an external image subtitle with the same filename as the file + * @param directory

The directory containing the file.

+ * @param file

The file to be converted.

+ * @return

The extension of the subtitle or empty if no subtitle was found.

+ */ + String hasExternalImageSubtitle(String directory, String file) { + String path = stripExtension(file); + for (String subtitleExtension : new String[] {".idx", ".sub"}) { + if (new File(directory + File.separator + path + subtitleExtension).exists()) { + return path + subtitleExtension; + } + } + return ""; + } + + /** + * Checks whether there exists an external subtitle with the same filename as the file + * @param directory

The directory containing the file.

+ * @param file

The file to be converted.

+ * @return

The extension of the subtitle or empty if no subtitle was found.

+ */ + String hasExternalSubtitle(String directory, String file) { + String path = stripExtension(file); + for (String subtitleExtension : new String[] {".srt", ".ass"}) { + System.out.println(directory + File.separator + path + subtitleExtension); + if (new File(directory + File.separator + path + subtitleExtension).exists()) { + return path + subtitleExtension; + } + } + return ""; + } } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java index a8a3874..4f8168c 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java @@ -95,10 +95,10 @@ public class VideoConverter extends Converter { String externalImageSubtitle = hasExternalImageSubtitle(folder.getAbsolutePath(), fileName); if (!externalSubtitle.equals("")) { command.add("-vf"); - command.add("subtitles=" + stripExtension(fileName) + externalSubtitle); + command.add("subtitles=" + externalSubtitle); } else if (!externalImageSubtitle.equals("")) { command.add("-i"); - command.add(stripExtension(fileName) + externalImageSubtitle); + command.add(externalImageSubtitle); if (this.DEBUG) { addDebug(command, 50, 120); } @@ -122,38 +122,6 @@ public class VideoConverter extends Converter { command.add("pan=stereo|FL < 1.0*FL + 0.707*FC + 0.707*BL|FR < 1.0*FR + 0.707*FC + 0.707*BR"); } - /** - * Checks whether there exists an external image subtitle with the same filename as the file - * @param directory

The directory containing the file.

- * @param file

The file to be converted.

- * @return

The extension of the subtitle or empty if no subtitle was found.

- */ - private String hasExternalImageSubtitle(String directory, String file) { - String path = stripExtension(file); - for (String subtitleExtension : new String[] {".idx", ".sub"}) { - if (new File(directory + File.separator + path + subtitleExtension).exists()) { - return subtitleExtension; - } - } - return ""; - } - - /** - * Checks whether there exists an external subtitle with the same filename as the file - * @param directory

The directory containing the file.

- * @param file

The file to be converted.

- * @return

The extension of the subtitle or empty if no subtitle was found.

- */ - private String hasExternalSubtitle(String directory, String file) { - String path = stripExtension(file); - for (String subtitleExtension : new String[] {".srt", ".ass"}) { - if (new File(directory + File.separator + path + subtitleExtension).exists()) { - return subtitleExtension; - } - } - return ""; - } - @Override public String[] getValidFormats() { return VIDEO_FORMATS;