EpicKnarvik97 f0e75eb440
Some checks failed
KnarCraft/FFmpegConvert/pipeline/head There was a failure building this commit
Adds a new type of anime converter and stuff
Adds a new type of anime converter that retains all streams, but converts the video to hevc, and re-orders the streams
Adds support for several types of encoding hardware acceleration that are automatically disabled if not available on the system.
Adds automatic hardware decoding acceleration.
Automatically removes empty files if ffmpeg fails.
2024-04-17 04:39:42 +02:00

85 lines
3.4 KiB
Java

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 <p>Path/command to ffprobe.</p>
* @param ffmpegPath <p>Path/command to ffmpeg.</p>
* @param newWidth <p>The new width of the video</p>
* @param newHeight <p>The new height of the video</p>
*/
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<StreamObject> streams = probeResult.parsedStreams();
List<ConverterModule> 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;
}
}