From ac25ca1986e16d8afff73c258b5dab3dd43bfbae Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 6 May 2024 01:32:19 +0200 Subject: [PATCH] Changes some things Removes any forced subtitles when changing the default stream Makes the title filter accept a list of filters, and allows using it for sorting rather than filtering --- .../converter/AnimeConverter.java | 3 +- .../converter/WebAnimeConverter.java | 2 +- .../mapping/SetDefaultStreamModule.java | 2 +- .../converter/sorter/SubtitleTitleSorter.java | 41 +++++++++++++++---- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java index 94f685d..8d99581 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java @@ -104,7 +104,8 @@ public class AnimeConverter extends AbstractConverter { modules.add(new SetDefaultStreamModule<>(sortedAudio, 0)); //Get the first subtitle stream in accordance with chosen languages and signs and songs prevention - StreamSorter subtitleSorter = new SubtitleTitleSorter(this.subtitleNameFilter) + StreamSorter subtitleSorter = new SubtitleTitleSorter( + List.of(this.subtitleNameFilter.split(","))) .append(new SubtitleLanguageSorter(this.subtitleLanguages)) .append(new MinimalSubtitleSorter(this.subtitlePreference)) .append(new ForcedFirstSorter<>(this.forcedSubtitleIndex)); diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/WebAnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/WebAnimeConverter.java index 8516e40..66cad0a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/WebAnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/WebAnimeConverter.java @@ -95,7 +95,7 @@ public class WebAnimeConverter extends AbstractConverter { VideoStream videoStream = getNthSteam(probeResult.getVideoStreams(), 0); //Get the first subtitle stream in accordance with chosen languages and signs and songs prevention - StreamSorter subtitleSorter = new SubtitleTitleSorter(this.subtitleNameFilter) + StreamSorter subtitleSorter = new SubtitleTitleSorter(List.of(this.subtitleNameFilter)) .append(new MinimalSubtitleSorter(this.subtitlePreference)) .append(new SubtitleLanguageSorter(this.subtitleLanguages)); SubtitleStream subtitleStream = getNthSteam(subtitleSorter.chainSort(probeResult.getSubtitleStreams()), diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/module/mapping/SetDefaultStreamModule.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/module/mapping/SetDefaultStreamModule.java index b46b645..9c40f8a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/module/mapping/SetDefaultStreamModule.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/module/mapping/SetDefaultStreamModule.java @@ -37,7 +37,7 @@ public class SetDefaultStreamModule implements Converter defaultModifier = '-'; } command.addOutputFileOption(String.format("-disposition:%s:%d", stream.streamTypeCharacter(), i), - String.format("%sdefault", defaultModifier)); + String.format("%sdefault-forced", defaultModifier)); } } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/sorter/SubtitleTitleSorter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/sorter/SubtitleTitleSorter.java index 101df9e..2161027 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/sorter/SubtitleTitleSorter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/sorter/SubtitleTitleSorter.java @@ -1,5 +1,6 @@ package net.knarcraft.ffmpegconverter.converter.sorter; +import net.knarcraft.ffmpegconverter.FFMpegConvert; import net.knarcraft.ffmpegconverter.streams.SubtitleStream; import org.jetbrains.annotations.NotNull; @@ -13,7 +14,7 @@ import java.util.regex.PatternSyntaxException; */ public class SubtitleTitleSorter extends AbstractSorter { - private final String titleFilter; + private final List titleFilter; /** * Instantiates a new subtitle title sorter @@ -23,18 +24,40 @@ public class SubtitleTitleSorter extends AbstractSorter { * * @param titleFilter

The filter to use. RegEx match, or a string the title must contain.

*/ - public SubtitleTitleSorter(@NotNull String titleFilter) { - this.titleFilter = titleFilter; + public SubtitleTitleSorter(@NotNull List titleFilter) { + this.titleFilter = new ArrayList<>(titleFilter); } @Override public @NotNull List sort(@NotNull List input) { - List output = new ArrayList<>(input); - if (!this.titleFilter.trim().isEmpty()) { - if (isValidRegularExpression(this.titleFilter) && hasSpecialRegexCharacters(this.titleFilter)) { - output.removeIf((stream) -> !stream.getTitle().matches(this.titleFilter)); - } else { - output.removeIf((stream) -> !stream.getTitle().contains(this.titleFilter)); + // Don't change anything if no filter is given + this.titleFilter.removeIf(String::isBlank); + if (this.titleFilter.isEmpty()) { + return input; + } + + List output = new ArrayList<>(); + + for (String filter : this.titleFilter) { + if (filter.trim().isEmpty()) { + continue; + } + + boolean isRegEx = isValidRegularExpression(filter) && hasSpecialRegexCharacters(filter); + + if (FFMpegConvert.isDebugEnabled()) { + System.out.println("Filtering subtitles by filter " + filter + ". RegEx is " + isRegEx); + } + + for (SubtitleStream subtitleStream : input) { + String title = subtitleStream.getTitle().trim().toLowerCase(); + // Add the subtitle if the filter matches, and it hasn't been added already + boolean matches = filter.trim().equals("*") || (isRegEx && title.matches(filter)) || + (!isRegEx && title.contains(filter)); + if (matches && !output.contains(subtitleStream)) { + System.out.println("Subtitle stream with title " + title + " matches"); + output.add(subtitleStream); + } } } return output;