Adds a new and more advanced converter which is configurable instead of pre-configured
This commit is contained in:
		@@ -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<String, String> inputArguments) {
 | 
				
			||||||
 | 
					        super(inputArguments.get("outputextension"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
 | 
				
			||||||
 | 
					        List<String> 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<AudioStream> 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<SubtitleStream> 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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user