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.hardwarecoding.H264HardwareEncodingModule; 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; import net.knarcraft.ffmpegconverter.streams.AudioStream; import net.knarcraft.ffmpegconverter.streams.StreamObject; 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 solely for the purpose of converting video streams of MKV files into h264 */ public class MkvH264Converter extends AbstractConverter { /** * Initializes variables used by the abstract converter * * @param ffprobePath

Path/command to ffprobe.

* @param ffmpegPath

Path/command to ffmpeg.

*/ public MkvH264Converter(String ffprobePath, String ffmpegPath) { super("mkv"); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; } @Override @NotNull public List getValidFormats() { return List.of("mkv"); } @Override @Nullable public 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(0, 0)); } // Map video if present List videoStreams = new ArrayList<>(probeResult.getVideoStreams()); if (!videoStreams.isEmpty()) { modules.add(new HardwareDecodeModule()); modules.add(new MapAllModule<>(videoStreams)); modules.add(new H264HardwareEncodingModule(17)); modules.add(new FastStartModule()); } // Map audio if present List audioStreams = probeResult.getAudioStreams(); if (!audioStreams.isEmpty()) { modules.add(new MapAllModule<>(audioStreams)); setOutputIndexes(audioStreams); modules.add(new CopyAudioModule(audioStreams)); } // Map subtitles if present List subtitleStreams = new ArrayList<>(probeResult.getSubtitleStreams()); if (!subtitleStreams.isEmpty()) { modules.add(new MapAllModule<>(probeResult.getSubtitleStreams())); modules.add(new CopySubtitlesModule()); } modules.add(new SetOutputFileModule(outFile)); new ModuleExecutor(command, modules).execute(); return command; } }