Adds a converter to embed subtitles
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good

This commit is contained in:
2024-03-27 12:36:11 +01:00
parent 2962114601
commit 2c75d91cce
7 changed files with 158 additions and 6 deletions

View File

@ -19,6 +19,7 @@ import java.util.List;
public final class FFMpegHelper {
private static final String PROBE_SPLIT_CHARACTER = "øæåÆØå";
private static String[] subtitleFormats = null;
private FFMpegHelper() {
@ -305,7 +306,9 @@ public final class FFMpegHelper {
throws IOException {
List<StreamObject> parsedStreams = new ArrayList<>();
//Find all files in the same directory with external subtitle formats
String[] subtitleFormats = FileUtil.readFileLines("subtitle_formats.txt");
if (subtitleFormats == null) {
subtitleFormats = FileUtil.readFileLines("subtitle_formats.txt");
}
File[] subtitleFiles = FileUtil.listFilesRecursive(directory, subtitleFormats, 1);
//Return early if no files were found
@ -315,9 +318,11 @@ public final class FFMpegHelper {
String fileTitle = FileUtil.stripExtension(convertingFile);
List<File> subtitleFilesList = new ArrayList<>(Arrays.asList(subtitleFiles));
//Finds the files which are subtitles probably belonging to the file
subtitleFilesList = ListUtil.getMatching(subtitleFilesList,
(subtitleFile) -> subtitleFile.getName().contains(fileTitle));
for (File subtitleFile : subtitleFilesList) {
//Probe the files and add them to the result list
String[] streams = probeForStreams(ffprobePath, subtitleFile);

View File

@ -1,5 +1,8 @@
package net.knarcraft.ffmpegconverter.utility;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@ -139,4 +142,43 @@ public final class FileUtil {
return file.substring(0, file.lastIndexOf('.'));
}
/**
* Gets the locale specifying the language of the given file name
*
* @param fileName <p>The file name to check</p>
* @return <p>The locale, or null if no locale could be parsed</p>
*/
public static @Nullable String getLanguage(@NotNull String fileName) {
fileName = stripExtension(fileName);
String possibleLanguage = getExtension(fileName);
// NRK Nett-TV has a tendency to use nb-ttv for Norwegian for some reason
possibleLanguage = possibleLanguage.replace("nb-ttv", "nb-nor");
// TODO: Some languages are specified by using "-en" or "-English" or ".en" or ".English" at the end of file names
if (possibleLanguage.length() <= 1 || (possibleLanguage.length() >= 4 &&
(!possibleLanguage.contains("-") || possibleLanguage.length() >= 8))) {
return null;
}
// Hope the text is an actual valid language
if (!possibleLanguage.contains("-")) {
return possibleLanguage;
}
// Make sure the "-" has at least two characters on each side
String[] parts = possibleLanguage.split("-");
if (parts[0].length() < 2 || parts[1].length() < 2) {
return null;
}
if (parts[1].length() == 3) {
// Return three-letter country code
return parts[1].toLowerCase();
} else {
// Return en-US country code
return parts[0].substring(0, 2).toLowerCase() + "-" + parts[1].substring(0, 2).toUpperCase();
}
}
}