From da67b195de94185f5cb5da9e50f429e5386787b4 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 5 Jul 2024 11:11:20 +0200 Subject: [PATCH] Adds a converter for converting video audio to vorbis --- .../ffmpegconverter/FFMpegConvert.java | 5 +- .../converter/AudioToVorbisConverter.java | 84 +++++++++++++++++++ .../converter/MkvH265ReducedConverter.java | 4 +- 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/knarcraft/ffmpegconverter/converter/AudioToVorbisConverter.java diff --git a/src/main/java/net/knarcraft/ffmpegconverter/FFMpegConvert.java b/src/main/java/net/knarcraft/ffmpegconverter/FFMpegConvert.java index ee9e9d6..a65450d 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/FFMpegConvert.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/FFMpegConvert.java @@ -3,6 +3,7 @@ package net.knarcraft.ffmpegconverter; import net.knarcraft.ffmpegconverter.config.Configuration; import net.knarcraft.ffmpegconverter.converter.AnimeConverter; import net.knarcraft.ffmpegconverter.converter.AudioConverter; +import net.knarcraft.ffmpegconverter.converter.AudioToVorbisConverter; import net.knarcraft.ffmpegconverter.converter.Converter; import net.knarcraft.ffmpegconverter.converter.DownScaleConverter; import net.knarcraft.ffmpegconverter.converter.LetterboxCropper; @@ -98,7 +99,8 @@ public class FFMpegConvert { 9. mp4 Subtitle Embed 10. Anime to h265 all streams 11. Stream reorder - 12. Letterbox cropper""", 1, 12); + 12. Letterbox cropper + 13. Video's Audio to vorbis converter""", 1, 13); return switch (choice) { case 1 -> generateWebAnimeConverter(); @@ -113,6 +115,7 @@ public class FFMpegConvert { case 10 -> generateAnimeConverter(); case 11 -> generateStreamOrderConverter(); case 12 -> new LetterboxCropper(FFPROBE_PATH, FFMPEG_PATH); + case 13 -> new AudioToVorbisConverter(FFPROBE_PATH, FFMPEG_PATH); default -> null; }; } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioToVorbisConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioToVorbisConverter.java new file mode 100644 index 0000000..8b0e053 --- /dev/null +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioToVorbisConverter.java @@ -0,0 +1,84 @@ +package net.knarcraft.ffmpegconverter.converter; + +import net.knarcraft.ffmpegconverter.container.FFMpegCommand; +import net.knarcraft.ffmpegconverter.container.StreamProbeResult; +import net.knarcraft.ffmpegconverter.converter.module.ConverterModule; +import net.knarcraft.ffmpegconverter.converter.module.DebugModule; +import net.knarcraft.ffmpegconverter.converter.module.ModuleExecutor; +import net.knarcraft.ffmpegconverter.converter.module.mapping.MapAllModule; +import net.knarcraft.ffmpegconverter.converter.module.output.CopySubtitlesModule; +import net.knarcraft.ffmpegconverter.converter.module.output.CopyVideoModule; +import net.knarcraft.ffmpegconverter.converter.module.output.SetOutputFileModule; +import net.knarcraft.ffmpegconverter.streams.AudioStream; +import net.knarcraft.ffmpegconverter.streams.SubtitleStream; +import net.knarcraft.ffmpegconverter.streams.VideoStream; +import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * A converter to convert a video's audio into flac + */ +public class AudioToVorbisConverter extends AbstractConverter { + + /** + * Initializes variables used by the abstract converter + * + * @param ffprobePath

Path/command to ffprobe.

+ * @param ffmpegPath

Path/command to ffmpeg.

+ */ + public AudioToVorbisConverter(String ffprobePath, String ffmpegPath) { + super("mkv"); + this.ffprobePath = ffprobePath; + this.ffmpegPath = ffmpegPath; + } + + @Override + public @NotNull List getValidFormats() { + return this.videoFormats; + } + + @Override + public @Nullable FFMpegCommand generateConversionCommand(@NotNull String executable, @NotNull StreamProbeResult probeResult, @NotNull String outFile) { + FFMpegCommand command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, probeResult.parsedFiles()); + List modules = new ArrayList<>(); + + if (this.debug) { + modules.add(new DebugModule()); + } + + // Map video if present + List videoStreams = probeResult.getVideoStreams(); + if (!videoStreams.isEmpty()) { + modules.add(new MapAllModule<>(videoStreams)); + modules.add(new CopyVideoModule()); + } + + // Map audio if present + List audioStreams = probeResult.getAudioStreams(); + if (!audioStreams.isEmpty()) { + modules.add(new MapAllModule<>(audioStreams)); + setOutputIndexes(audioStreams); + } + + // Map subtitles if present + List subtitleStreams = probeResult.getSubtitleStreams(); + if (!subtitleStreams.isEmpty()) { + modules.add(new MapAllModule<>(subtitleStreams)); + setOutputIndexes(subtitleStreams); + modules.add(new CopySubtitlesModule()); + } + + // Map any fonts, cover images or similar + modules.add(new MapAllModule<>(probeResult.getOtherStreams())); + + // Set output file and execute + modules.add(new SetOutputFileModule(outFile)); + new ModuleExecutor(command, modules).execute(); + return command; + } + +} diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/MkvH265ReducedConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/MkvH265ReducedConverter.java index 15c6df9..69f9f2e 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/MkvH265ReducedConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/MkvH265ReducedConverter.java @@ -8,7 +8,6 @@ import net.knarcraft.ffmpegconverter.converter.module.ModuleExecutor; import net.knarcraft.ffmpegconverter.converter.module.hardwarecoding.H265HardwareEncodingModule; import net.knarcraft.ffmpegconverter.converter.module.hardwarecoding.HardwareDecodeModule; import net.knarcraft.ffmpegconverter.converter.module.mapping.MapAllModule; -import net.knarcraft.ffmpegconverter.converter.module.output.CopyAudioModule; import net.knarcraft.ffmpegconverter.converter.module.output.CopySubtitlesModule; import net.knarcraft.ffmpegconverter.converter.module.output.FastStartModule; import net.knarcraft.ffmpegconverter.converter.module.output.SetOutputFileModule; @@ -70,7 +69,8 @@ public class MkvH265ReducedConverter extends AbstractConverter { if (!audioStreams.isEmpty()) { modules.add(new MapAllModule<>(audioStreams)); setOutputIndexes(audioStreams); - modules.add(new CopyAudioModule(audioStreams)); + command.addOutputFileOption("-c:a", "flac"); + command.addOutputFileOption("-compression_level", "12"); } // Map subtitles if present