EpicKnarvik97 461c7552b3
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
Adds converter modules and sorters
Converters are now the result of combining multiple modules, and running them in the selected sequence. I hope to be able to completely remove converters by replacing them with templates that simply run some modules in a sequence, before running FFmpeg. Then I want to be able to parse a command into a template, so users have full control of what a template does.

Sorters each sort a list of streams after a single criteria. They can be chained in order to create complex chained sorters.
2024-04-12 15:31:34 +02:00

77 lines
2.9 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.mapping.BurnSubtitleModule;
import net.knarcraft.ffmpegconverter.converter.module.mapping.NthAudioStreamModule;
import net.knarcraft.ffmpegconverter.converter.module.mapping.SelectSingleStreamModule;
import net.knarcraft.ffmpegconverter.converter.module.output.SetOutputFileModule;
import net.knarcraft.ffmpegconverter.streams.SubtitleStream;
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;
import static net.knarcraft.ffmpegconverter.utility.FFMpegHelper.getNthSteam;
/**
* A simple converter for web-video
*/
public class WebVideoConverter extends AbstractConverter {
/**
* Instantiates a new web video converter
*
* @param ffprobePath <p>Path/command to ffprobe.</p>
* @param ffmpegPath <p>Path/command to ffmpeg.</p>
* @param outExtension <p>The extension of the new file.</p>
*/
public WebVideoConverter(String ffprobePath, String ffmpegPath, String outExtension) {
super(outExtension);
this.ffprobePath = ffprobePath;
this.ffmpegPath = ffmpegPath;
}
@Override
public String[] getValidFormats() {
return videoFormats;
}
@Override
public String[] generateConversionCommand(@NotNull String executable, @NotNull StreamProbeResult probeResult,
@NotNull String outFile) {
FFMpegCommand command = FFMpegHelper.getFFMpegWebVideoCommand(executable, probeResult.parsedFiles());
List<ConverterModule> modules = new ArrayList<>();
if (this.debug) {
modules.add(new DebugModule(0, 0));
}
//Get first streams from the file
SubtitleStream subtitleStream = getNthSteam(probeResult.getSubtitleStreams(), 0);
VideoStream videoStream = getNthSteam(probeResult.getVideoStreams(), 0);
if (videoStream == null) {
throw new IllegalArgumentException("The selected video stream does not exist.");
}
if (subtitleStream != null) {
modules.add(new BurnSubtitleModule(subtitleStream, videoStream, true));
} else {
modules.add(new SelectSingleStreamModule(videoStream));
}
modules.add(new NthAudioStreamModule(probeResult.getAudioStreams(), 0));
modules.add(new SetOutputFileModule(outFile));
new ModuleExecutor(command, modules).execute();
return command.getResult();
}
}