Adds new converters

Adds a MKV to HEVC converter which aims to reduce file-sizes
Adds a MKV to MP4 transcoder which allows selection of each type of stream
This commit is contained in:
2023-11-14 17:10:45 +01:00
parent f81a21b9e9
commit 2346e651ef
9 changed files with 294 additions and 26 deletions

View File

@@ -0,0 +1,81 @@
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 java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* A transcoder which takes one of each stream from an MKV file and produces an MP4 file
*/
public class MKVToMP4Transcoder extends AbstractConverter {
private final int videoStreamIndex;
private final int audioStreamIndex;
private final int subtitleStreamIndex;
/**
* Instantiates a new mkv to mp4 transcoder
*
* @param ffprobePath <p>Path/command to ffprobe.</p>
* @param ffmpegPath <p>Path/command to ffmpeg.</p>
* @param audioStreamIndex <p>The relative index of the audio stream to use (0 or below selects the first)</p>
* @param subtitleStreamIndex <p>The relative index of the subtitle stream to use (0 or below selects the first)</p>
* @param videoStreamIndex <p>The relative index of the video stream to use (0 or below selects the first)</p>
*/
public MKVToMP4Transcoder(String ffprobePath, String ffmpegPath, int audioStreamIndex, int subtitleStreamIndex,
int videoStreamIndex) {
super("mp4");
this.ffprobePath = ffprobePath;
this.ffmpegPath = ffmpegPath;
this.videoStreamIndex = videoStreamIndex;
this.audioStreamIndex = audioStreamIndex;
this.subtitleStreamIndex = subtitleStreamIndex;
}
@Override
public String[] getValidFormats() {
return new String[]{"mkv"};
}
@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);
}
//Get the nth audio stream
List<AudioStream> audioStreams = filterStreamsByType(streams, AudioStream.class);
AudioStream audioStream = getNthAudioSteam(new ArrayList<>(audioStreams), Math.max(this.audioStreamIndex, 0));
//Get the nth subtitle stream
List<SubtitleStream> allSubtitleStreams = filterStreamsByType(streams, SubtitleStream.class);
SubtitleStream subtitleStream = getNthSubtitleStream(new ArrayList<>(allSubtitleStreams),
Math.max(this.subtitleStreamIndex, 0));
//Get the nth video stream
VideoStream videoStream = getNthVideoStream(streams, Math.max(this.videoStreamIndex, 0));
// Copy stream info
command.add("-map_metadata");
command.add("0");
command.add("-movflags");
command.add("use_metadata_tags");
command.add("-c");
command.add("copy");
//Add streams to output file
FFMpegHelper.addAudioStream(command, audioStream, false);
FFMpegHelper.addSubtitleAndVideoStream(command, subtitleStream, videoStream, file);
command.add(outFile);
return command.toArray(new String[0]);
}
}