From d0e4a7b3cca910c6f51ce3ecf2cc931bcad0e322 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 25 Jan 2021 21:55:16 +0100 Subject: [PATCH] Adds a new and more advanced converter which is configurable instead of pre-configured --- .../converter/AdvancedConverter.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/main/java/net/knarcraft/ffmpegconverter/converter/AdvancedConverter.java diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AdvancedConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AdvancedConverter.java new file mode 100644 index 0000000..519edff --- /dev/null +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AdvancedConverter.java @@ -0,0 +1,122 @@ +package net.knarcraft.ffmpegconverter.converter; + +import net.knarcraft.ffmpegconverter.streams.AudioStream; +import net.knarcraft.ffmpegconverter.streams.StreamObject; +import net.knarcraft.ffmpegconverter.streams.SubtitleStream; +import net.knarcraft.ffmpegconverter.streams.VideoStream; +import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; +import net.knarcraft.ffmpegconverter.utility.ListUtil; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class AdvancedConverter extends AbstractConverter { + + private boolean burnFirstSubtitle = true; + private boolean burnFirstAudio = true; + private String videoCodec = "h264"; + private String pixelFormat = "yuv420p"; + private int audioSamplingFrequency = 48000; + private boolean moveHeaders = true; + private boolean convertToStereo = true; + private boolean preventPartialSubtitles = true; + private String[] audioLanguages = new String[]{"jpn", "eng", "0"}; + private String[] subtitleLanguages = new String[]{"eng", "0"}; + private String outputExtension = "mp4"; + private boolean autoStreamSelection = false; + + /** + * Initializes variables used by the abstract converter + */ + AdvancedConverter(Map inputArguments) { + super(inputArguments.get("outputextension")); + } + + + @Override + public String[] generateConversionCommand(String executable, File file, List streams, String outFile) { + List command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); + if (this.debug) { + FFMpegHelper.addDebugArguments(command, 50, 120); + } + + if (!this.videoCodec.isEmpty()) { + command.add("-vcodec"); + command.add(this.videoCodec); + } + + if (!this.pixelFormat.isEmpty()) { + command.add("-pix_fmt"); + command.add(this.pixelFormat); + } + + if (this.audioSamplingFrequency > 0) { + command.add("-ar"); + command.add("" + this.audioSamplingFrequency); + } + + if (this.moveHeaders) { + command.add("-movflags"); + command.add("+faststart"); + } + + //If the user wants to convert to an audio file, just select audio streams + if (ListUtil.listContains(audioFormats, (item) -> item.equals(this.outputExtension)) || autoStreamSelection) { + command.add(outFile); + return command.toArray(new String[0]); + } + + if (!burnFirstAudio && !burnFirstSubtitle) { + //Copy all streams + command.add("-map"); + command.add("0"); + command.add("-c:a"); + command.add("copy"); + command.add("-c:s"); + command.add("copy"); + } else { + + //Get the first audio stream in accordance with chosen languages + List audioStreams = filterAudioStreams(filterStreamsByType(streams, AudioStream.class), audioLanguages); + AudioStream audioStream = getFirstAudioSteam(new ArrayList<>(audioStreams)); + + //Get the first subtitle stream in accordance with chosen languages and signs and songs prevention + List subtitleStreams = filterSubtitleStreams(filterStreamsByType(streams, + SubtitleStream.class), this.subtitleLanguages, this.preventPartialSubtitles); + SubtitleStream subtitleStream = getFirstSubtitleStream(new ArrayList<>(subtitleStreams)); + + //Get the first video stream + VideoStream videoStream = getFirstVideoStream(filterStreamsByType(streams, VideoStream.class)); + + //Add streams to output file + if (burnFirstAudio) { + FFMpegHelper.addAudioStream(command, audioStream, this.convertToStereo); + } else { + command.add("-map"); + command.add("0:a"); + command.add("-c:a"); + command.add("copy"); + } + if (burnFirstSubtitle) { + FFMpegHelper.addSubtitleAndVideoStream(command, subtitleStream, videoStream, file); + } else { + command.add("-map"); + command.add("0:s"); + command.add("-map"); + command.add("0:v"); + command.add("-c:s"); + command.add("copy"); + } + } + + command.add(outFile); + return command.toArray(new String[0]); + } + + @Override + public String[] getValidFormats() { + return ListUtil.concatenate(videoFormats, audioFormats); + } +}