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.HardwareDecodeModule; import net.knarcraft.ffmpegconverter.converter.module.mapping.NthAudioStreamModule; import net.knarcraft.ffmpegconverter.converter.module.mapping.NthSubtitleStreamModule; import net.knarcraft.ffmpegconverter.converter.module.mapping.NthVideoStreamModule; import net.knarcraft.ffmpegconverter.converter.module.output.CopyAllModule; import net.knarcraft.ffmpegconverter.converter.module.output.SetOutputFileModule; 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 transcoder which takes one of each stream from an MKV file and produces an MP4 file */ public class MKVToMP4Transcoder extends AbstractConverter { private final int videoStreamIndex; private final int audioStreamIndex; private final int subtitleStreamIndex; /** * Instantiates a new mkv to mp4 transcoder * * @param ffprobePath

Path/command to ffprobe.

* @param ffmpegPath

Path/command to ffmpeg.

* @param audioStreamIndex

The relative index of the audio stream to use (0 or below selects the first)

* @param subtitleStreamIndex

The relative index of the subtitle stream to use (0 or below selects the first)

* @param videoStreamIndex

The relative index of the video stream to use (0 or below selects the first)

*/ public MKVToMP4Transcoder(String ffprobePath, String ffmpegPath, int audioStreamIndex, int subtitleStreamIndex, int videoStreamIndex) { super("mp4"); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; this.videoStreamIndex = videoStreamIndex; this.audioStreamIndex = audioStreamIndex; this.subtitleStreamIndex = subtitleStreamIndex; } @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()); } // Copy stream info modules.add(new CopyAllModule()); //Add streams to output file if (!probeResult.getAudioStreams().isEmpty()) { modules.add(new NthAudioStreamModule(probeResult.getAudioStreams(), this.audioStreamIndex)); } if (!probeResult.getVideoStreams().isEmpty()) { modules.add(new NthVideoStreamModule(probeResult.getVideoStreams(), this.videoStreamIndex)); modules.add(new HardwareDecodeModule()); } if (!probeResult.getSubtitleStreams().isEmpty()) { modules.add(new NthSubtitleStreamModule(probeResult.getSubtitleStreams(), this.subtitleStreamIndex)); } modules.add(new SetOutputFileModule(outFile)); new ModuleExecutor(command, modules).execute(); return command; } }