Changes some things
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good

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
This commit is contained in:
Kristian Knarvik 2024-05-06 01:32:19 +02:00
parent dae93b9f81
commit ac25ca1986
4 changed files with 36 additions and 12 deletions

View File

@ -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<SubtitleStream> subtitleSorter = new SubtitleTitleSorter(this.subtitleNameFilter)
StreamSorter<SubtitleStream> subtitleSorter = new SubtitleTitleSorter(
List.of(this.subtitleNameFilter.split(",")))
.append(new SubtitleLanguageSorter(this.subtitleLanguages))
.append(new MinimalSubtitleSorter(this.subtitlePreference))
.append(new ForcedFirstSorter<>(this.forcedSubtitleIndex));

View File

@ -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<SubtitleStream> subtitleSorter = new SubtitleTitleSorter(this.subtitleNameFilter)
StreamSorter<SubtitleStream> subtitleSorter = new SubtitleTitleSorter(List.of(this.subtitleNameFilter))
.append(new MinimalSubtitleSorter(this.subtitlePreference))
.append(new SubtitleLanguageSorter(this.subtitleLanguages));
SubtitleStream subtitleStream = getNthSteam(subtitleSorter.chainSort(probeResult.getSubtitleStreams()),

View File

@ -37,7 +37,7 @@ public class SetDefaultStreamModule<K extends StreamObject> implements Converter
defaultModifier = '-';
}
command.addOutputFileOption(String.format("-disposition:%s:%d", stream.streamTypeCharacter(), i),
String.format("%sdefault", defaultModifier));
String.format("%sdefault-forced", defaultModifier));
}
}

View File

@ -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<SubtitleStream> {
private final String titleFilter;
private final List<String> titleFilter;
/**
* Instantiates a new subtitle title sorter
@ -23,18 +24,40 @@ public class SubtitleTitleSorter extends AbstractSorter<SubtitleStream> {
*
* @param titleFilter <p>The filter to use. RegEx match, or a string the title must contain.</p>
*/
public SubtitleTitleSorter(@NotNull String titleFilter) {
this.titleFilter = titleFilter;
public SubtitleTitleSorter(@NotNull List<String> titleFilter) {
this.titleFilter = new ArrayList<>(titleFilter);
}
@Override
public @NotNull List<SubtitleStream> sort(@NotNull List<SubtitleStream> input) {
List<SubtitleStream> 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<SubtitleStream> 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;