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.MapAllModule; import net.knarcraft.ffmpegconverter.converter.module.output.CopyAudioModule; import net.knarcraft.ffmpegconverter.converter.module.output.CopySubtitlesModule; import net.knarcraft.ffmpegconverter.converter.module.output.ScaleModule; import net.knarcraft.ffmpegconverter.converter.module.output.SetOutputFileModule; import net.knarcraft.ffmpegconverter.converter.module.output.SetQualityModule; import net.knarcraft.ffmpegconverter.streams.StreamObject; import net.knarcraft.ffmpegconverter.streams.VideoStream; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; /** * A converter for converting video files */ public class DownScaleConverter extends AbstractConverter { private final int newWidth; private final int newHeight; /** * Instantiates a new video converter * * @param ffprobePath

Path/command to ffprobe.

* @param ffmpegPath

Path/command to ffmpeg.

* @param newWidth

The new width of the video

* @param newHeight

The new height of the video

*/ public DownScaleConverter(String ffprobePath, String ffmpegPath, int newWidth, int newHeight) { super(null); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; this.newHeight = newHeight; this.newWidth = newWidth; } @Override public FFMpegCommand generateConversionCommand(@NotNull String executable, @NotNull StreamProbeResult probeResult, @NotNull String outFile) { List streams = probeResult.parsedStreams(); List modules = new ArrayList<>(); VideoStream videoStream = FFMpegHelper.getNthSteam(probeResult.getVideoStreams(), 0); if (videoStream == null) { throw new IllegalArgumentException("The selected file has no video stream"); } // Simply ignore files below, or at, the target resolution if (videoStream.getWidth() <= this.newWidth && videoStream.getHeight() <= this.newHeight) { return null; } FFMpegCommand command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, probeResult.parsedFiles()); if (this.debug) { modules.add(new DebugModule(0, 0)); } //Add all streams without re-encoding modules.add(new MapAllModule<>(streams)); modules.add(new CopyAudioModule()); modules.add(new CopySubtitlesModule()); modules.add(new ScaleModule(this.newWidth, this.newHeight)); modules.add(new SetQualityModule(20, "p7")); modules.add(new SetOutputFileModule(outFile)); modules.add(new HardwareDecodeModule()); new ModuleExecutor(command, modules).execute(); return command; } @Override public String[] getValidFormats() { return videoFormats; } }