streamInfo = getStreamInfo(streamParts);
+ parsedStreams.add(new SubtitleStream(streamInfo, 0, subtitleFile.getName(), false));
}
}
return parsedStreams;
@@ -350,91 +406,4 @@ public final class FFMpegHelper {
return text.toString().trim();
}
- /**
- * Parses a list of video stream parameters to a video stream object
- *
- * @param streamParts A list of parameters belonging to an video stream.
- * @param relativeIndex The relative index of the video stream.
- * @return A SubtitleStream object.
- * @throws NumberFormatException If codec index contains a non-numeric value.
- */
- private static VideoStream parseVideoStream(String[] streamParts, int relativeIndex) throws NumberFormatException {
- String codec = null;
- int absoluteIndex = -1;
- int width = -1;
- int height = -1;
- for (String streamPart : streamParts) {
- if (streamPart.startsWith("codec_name=")) {
- codec = streamPart.replace("codec_name=", "");
- } else if (streamPart.startsWith("index=")) {
- absoluteIndex = Integer.parseInt(streamPart.replace("index=", ""));
- } else if (streamPart.startsWith("width=")) {
- width = Integer.parseInt(streamPart.replace("width=", ""));
- } else if (streamPart.startsWith("height=")) {
- height = Integer.parseInt(streamPart.replace("height=", ""));
- }
- }
- return new VideoStream(codec, absoluteIndex, relativeIndex, width, height);
- }
-
- /**
- * Parses a list of audio stream parameters to an audio stream object
- *
- * @param streamParts A list of parameters belonging to an audio stream.
- * @param relativeIndex The relative index of the audio stream.
- * @return A SubtitleStream object.
- * @throws NumberFormatException If codec index contains a non-numeric value.
- */
- private static AudioStream parseAudioStream(String[] streamParts, int relativeIndex) throws NumberFormatException {
- String codec = null;
- int absoluteIndex = -1;
- String language = null;
- int channels = 0;
- String title = "";
- for (String streamPart : streamParts) {
- if (streamPart.startsWith("codec_name=")) {
- codec = streamPart.replace("codec_name=", "");
- } else if (streamPart.startsWith("index=")) {
- absoluteIndex = Integer.parseInt(streamPart.replace("index=", ""));
- } else if (streamPart.startsWith("TAG:language=")) {
- language = streamPart.replace("TAG:language=", "");
- } else if (streamPart.startsWith("channels=")) {
- channels = Integer.parseInt(streamPart.replace("channels=", ""));
- } else if (streamPart.startsWith("TAG:title=")) {
- title = streamPart.replace("TAG:title=", "");
- }
- }
- return new AudioStream(codec, absoluteIndex, relativeIndex, language, title, channels);
- }
-
- /**
- * Parses a list of subtitle stream parameters to a subtitle stream object
- *
- * @param streamParts A list of parameters belonging to a subtitle stream.
- * @param relativeIndex The relative index of the subtitle.
- * @param file The file currently being converted.
- * @param isInternalStream Whether the subtitle stream is in the main video file
- * @return A SubtitleStream object.
- * @throws NumberFormatException If codec index contains a non-numeric value.
- */
- private static SubtitleStream parseSubtitleStream(String[] streamParts, int relativeIndex, String file,
- boolean isInternalStream) throws NumberFormatException {
- String codecName = null;
- int absoluteIndex = -1;
- String language = null;
- String title = "";
- for (String streamPart : streamParts) {
- if (streamPart.startsWith("codec_name=")) {
- codecName = streamPart.replace("codec_name=", "");
- } else if (streamPart.startsWith("index=")) {
- absoluteIndex = Integer.parseInt(streamPart.replace("index=", ""));
- } else if (streamPart.startsWith("TAG:language=")) {
- language = streamPart.replace("TAG:language=", "");
- } else if (streamPart.startsWith("TAG:title=")) {
- title = streamPart.replace("TAG:title=", "");
- }
- }
- return new SubtitleStream(codecName, absoluteIndex, relativeIndex, language, title, file, isInternalStream);
- }
-
}
diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/ValueParsingHelper.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/ValueParsingHelper.java
new file mode 100644
index 0000000..b1bd2c8
--- /dev/null
+++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/ValueParsingHelper.java
@@ -0,0 +1,63 @@
+package net.knarcraft.ffmpegconverter.utility;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Objects;
+
+/**
+ * A helper class for parsing values without causing errors
+ */
+public final class ValueParsingHelper {
+
+ private ValueParsingHelper() {
+
+ }
+
+ /**
+ * Parses an integer from a string
+ *
+ * @param input The input given
+ * @param defaultValue The default value to return if no integer could be parsed
+ * @return The parsed integer, or the given default value
+ */
+ public static int parseInt(@Nullable String input, int defaultValue) {
+ if (input == null) {
+ return defaultValue;
+ }
+
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException exception) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Parses a string
+ *
+ * @param input The input string
+ * @param defaultValue The default value to use if the string is null
+ * @return The input string, or the default value
+ */
+ @NotNull
+ public static String parseString(@Nullable String input, @NotNull String defaultValue) {
+ return Objects.requireNonNullElse(input, defaultValue);
+ }
+
+ /**
+ * Parses a boolean
+ *
+ * @param input The input string
+ * @param defaultValue The default value to use if the string is null
+ * @return The parsed boolean, or the default value
+ */
+ public static boolean parseBoolean(@Nullable String input, boolean defaultValue) {
+ if (input == null || input.isEmpty()) {
+ return defaultValue;
+ } else {
+ return Boolean.parseBoolean(input);
+ }
+ }
+
+}