Adds a converter for web video and changes behavior of the normal video converter
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good
The normal video converter now includes all streams The web video converter selects the first of each stream Changes some method names Moves the convert command to the abstract converter
This commit is contained in:
parent
a516cdcdff
commit
f2b2de514e
@ -4,6 +4,7 @@ import net.knarcraft.ffmpegconverter.converter.AbstractConverter;
|
|||||||
import net.knarcraft.ffmpegconverter.converter.AnimeConverter;
|
import net.knarcraft.ffmpegconverter.converter.AnimeConverter;
|
||||||
import net.knarcraft.ffmpegconverter.converter.AudioConverter;
|
import net.knarcraft.ffmpegconverter.converter.AudioConverter;
|
||||||
import net.knarcraft.ffmpegconverter.converter.VideoConverter;
|
import net.knarcraft.ffmpegconverter.converter.VideoConverter;
|
||||||
|
import net.knarcraft.ffmpegconverter.converter.WebVideoConverter;
|
||||||
import net.knarcraft.ffmpegconverter.utility.FileUtil;
|
import net.knarcraft.ffmpegconverter.utility.FileUtil;
|
||||||
import net.knarcraft.ffmpegconverter.utility.ListUtil;
|
import net.knarcraft.ffmpegconverter.utility.ListUtil;
|
||||||
import net.knarcraft.ffmpegconverter.utility.OutputUtil;
|
import net.knarcraft.ffmpegconverter.utility.OutputUtil;
|
||||||
@ -27,15 +28,14 @@ class Main {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
loadConverter();
|
loadConverter();
|
||||||
|
|
||||||
int recursionSteps = 1;
|
List<String> input;
|
||||||
|
do {
|
||||||
OutputUtil.println("<Folder/File> [Recursions]: ");
|
OutputUtil.println("<Folder/File> [Recursions]:");
|
||||||
List<String> input = readInput(2);
|
|
||||||
while (input.isEmpty()) {
|
|
||||||
OutputUtil.print("File path required.");
|
|
||||||
input = readInput(2);
|
input = readInput(2);
|
||||||
}
|
} while (input.isEmpty());
|
||||||
File folder = new File(input.get(0));
|
File folder = new File(input.get(0));
|
||||||
|
|
||||||
|
int recursionSteps = 1;
|
||||||
if (input.size() > 1) {
|
if (input.size() > 1) {
|
||||||
try {
|
try {
|
||||||
recursionSteps = Integer.parseInt(input.get(1));
|
recursionSteps = Integer.parseInt(input.get(1));
|
||||||
@ -45,7 +45,6 @@ class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
convertAllFiles(folder, recursionSteps);
|
convertAllFiles(folder, recursionSteps);
|
||||||
|
|
||||||
OutputUtil.close();
|
OutputUtil.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +55,7 @@ class Main {
|
|||||||
*/
|
*/
|
||||||
private static void loadConverter() throws IOException {
|
private static void loadConverter() throws IOException {
|
||||||
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" +
|
int choice = getChoice("Which converter do you want do use?\n1. Anime to web mp4\n2. Audio converter\n" +
|
||||||
"3. VideoStream converter", 1, 3);
|
"3. VideoStream converter", 1, 4);
|
||||||
|
|
||||||
OutputUtil.println("Input for this converter:");
|
OutputUtil.println("Input for this converter:");
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
@ -69,6 +68,9 @@ class Main {
|
|||||||
case 3:
|
case 3:
|
||||||
converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
||||||
break;
|
break;
|
||||||
|
case 4:
|
||||||
|
converter = new WebVideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("<output extension>"));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ public abstract class AbstractConverter implements Converter {
|
|||||||
* @param file <p>The file to process.</p>
|
* @param file <p>The file to process.</p>
|
||||||
* @throws IOException <p>If the BufferedReader fails.</p>
|
* @throws IOException <p>If the BufferedReader fails.</p>
|
||||||
*/
|
*/
|
||||||
void processFile(File folder, File file) throws IOException {
|
private void processFile(File folder, File file) throws IOException {
|
||||||
List<StreamObject> streams = FFMpegHelper.probeFile(ffprobePath, file);
|
List<StreamObject> streams = FFMpegHelper.probeFile(ffprobePath, file);
|
||||||
if (streams.isEmpty()) {
|
if (streams.isEmpty()) {
|
||||||
throw new IllegalArgumentException("The file has no valid streams. Please make sure the file exists and" +
|
throw new IllegalArgumentException("The file has no valid streams. Please make sure the file exists and" +
|
||||||
@ -182,4 +182,9 @@ public abstract class AbstractConverter implements Converter {
|
|||||||
}
|
}
|
||||||
return videoStream;
|
return videoStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void convert(File file) throws IOException {
|
||||||
|
processFile(file.getParentFile(), file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import net.knarcraft.ffmpegconverter.streams.VideoStream;
|
|||||||
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
|
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -41,11 +40,6 @@ public class AnimeConverter extends AbstractConverter {
|
|||||||
this.preventSignsAndSongs = preventSignsAndSongs;
|
this.preventSignsAndSongs = preventSignsAndSongs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convert(File file) throws IOException {
|
|
||||||
processFile(file.getParentFile(), file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
|
public String[] generateConversionCommand(String executable, File file, List<StreamObject> streams, String outFile) {
|
||||||
List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName());
|
List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName());
|
||||||
@ -66,8 +60,8 @@ public class AnimeConverter extends AbstractConverter {
|
|||||||
VideoStream videoStream = getFirstVideoStream(streams);
|
VideoStream videoStream = getFirstVideoStream(streams);
|
||||||
|
|
||||||
//Add streams to output file
|
//Add streams to output file
|
||||||
FFMpegHelper.addAudioStreams(command, audioStream, toStereo);
|
FFMpegHelper.addAudioStream(command, audioStream, toStereo);
|
||||||
FFMpegHelper.addSubtitlesAndVideo(command, subtitleStream, videoStream, file);
|
FFMpegHelper.addSubtitleAndVideoStream(command, subtitleStream, videoStream, file);
|
||||||
|
|
||||||
command.add(outFile);
|
command.add(outFile);
|
||||||
return command.toArray(new String[0]);
|
return command.toArray(new String[0]);
|
||||||
|
@ -5,7 +5,6 @@ import net.knarcraft.ffmpegconverter.streams.StreamObject;
|
|||||||
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
|
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,7 +34,7 @@ public class AudioConverter extends AbstractConverter {
|
|||||||
|
|
||||||
//Gets the first audio stream from the file and adds it to the output file
|
//Gets the first audio stream from the file and adds it to the output file
|
||||||
AudioStream audioStream = getFirstAudioSteam(streams);
|
AudioStream audioStream = getFirstAudioSteam(streams);
|
||||||
FFMpegHelper.addAudioStreams(command, audioStream, false);
|
FFMpegHelper.addAudioStream(command, audioStream, false);
|
||||||
|
|
||||||
return command.toArray(new String[0]);
|
return command.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
@ -44,9 +43,4 @@ public class AudioConverter extends AbstractConverter {
|
|||||||
public String[] getValidFormats() {
|
public String[] getValidFormats() {
|
||||||
return audioFormats;
|
return audioFormats;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convert(File file) throws IOException {
|
|
||||||
processFile(file.getParentFile(), file);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* This interface describes a file converter
|
* This interface describes a file converter
|
||||||
*/
|
*/
|
||||||
public interface Converter {
|
interface Converter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given file
|
* Converts the given file
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
package net.knarcraft.ffmpegconverter.converter;
|
package net.knarcraft.ffmpegconverter.converter;
|
||||||
|
|
||||||
import net.knarcraft.ffmpegconverter.streams.AudioStream;
|
|
||||||
import net.knarcraft.ffmpegconverter.streams.StreamObject;
|
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.FFMpegHelper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,16 +31,11 @@ public class VideoConverter extends AbstractConverter {
|
|||||||
FFMpegHelper.addDebugArguments(command, 50, 120);
|
FFMpegHelper.addDebugArguments(command, 50, 120);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get first streams from the file
|
//Add all streams without re-encoding
|
||||||
SubtitleStream subtitleStream = getFirstSubtitleStream(streams);
|
command.add("-map");
|
||||||
VideoStream videoStream = getFirstVideoStream(streams);
|
command.add("0");
|
||||||
AudioStream audioStream = getFirstAudioSteam(streams);
|
command.add("-c");
|
||||||
|
command.add("copy");
|
||||||
//Add streams to output
|
|
||||||
FFMpegHelper.addSubtitlesAndVideo(command, subtitleStream, videoStream, file);
|
|
||||||
if (audioStream != null) {
|
|
||||||
FFMpegHelper.addAudioStreams(command, audioStream, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
command.add(outFile);
|
command.add(outFile);
|
||||||
return command.toArray(new String[0]);
|
return command.toArray(new String[0]);
|
||||||
@ -55,8 +46,4 @@ public class VideoConverter extends AbstractConverter {
|
|||||||
return videoFormats;
|
return videoFormats;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void convert(File file) throws IOException {
|
|
||||||
processFile(file.getParentFile(), file);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
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.List;
|
||||||
|
|
||||||
|
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(String executable, File file, List<StreamObject> streams, String outFile) {
|
||||||
|
List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName());
|
||||||
|
if (this.debug) {
|
||||||
|
FFMpegHelper.addDebugArguments(command, 50, 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get first streams from the file
|
||||||
|
SubtitleStream subtitleStream = getFirstSubtitleStream(streams);
|
||||||
|
VideoStream videoStream = getFirstVideoStream(streams);
|
||||||
|
AudioStream audioStream = getFirstAudioSteam(streams);
|
||||||
|
|
||||||
|
//Add streams to output
|
||||||
|
FFMpegHelper.addSubtitleAndVideoStream(command, subtitleStream, videoStream, file);
|
||||||
|
if (audioStream != null) {
|
||||||
|
FFMpegHelper.addAudioStream(command, audioStream, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
command.add(outFile);
|
||||||
|
return command.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
}
|
@ -129,7 +129,7 @@ public final class FFMpegHelper {
|
|||||||
* @param audioStream <p>The audio stream to be added.</p>
|
* @param audioStream <p>The audio stream to be added.</p>
|
||||||
* @param toStereo <p>Whether to convert the audio stream to stereo.</p>
|
* @param toStereo <p>Whether to convert the audio stream to stereo.</p>
|
||||||
*/
|
*/
|
||||||
public static void addAudioStreams(List<String> command, AudioStream audioStream, boolean toStereo) {
|
public static void addAudioStream(List<String> command, AudioStream audioStream, boolean toStereo) {
|
||||||
if (audioStream != null) {
|
if (audioStream != null) {
|
||||||
command.add("-map");
|
command.add("-map");
|
||||||
command.add("0:" + audioStream.getAbsoluteIndex());
|
command.add("0:" + audioStream.getAbsoluteIndex());
|
||||||
@ -149,12 +149,11 @@ public final class FFMpegHelper {
|
|||||||
* @param videoStream <p>The video stream to be used.</p>
|
* @param videoStream <p>The video stream to be used.</p>
|
||||||
* @param file <p>The file to convert.</p>
|
* @param file <p>The file to convert.</p>
|
||||||
*/
|
*/
|
||||||
public static void addSubtitlesAndVideo(List<String> command, SubtitleStream subtitleStream,
|
public static void addSubtitleAndVideoStream(List<String> command, SubtitleStream subtitleStream,
|
||||||
VideoStream videoStream, File file) {
|
VideoStream videoStream, File file) {
|
||||||
//No appropriate subtitle was found. Just add the video stream.
|
//No appropriate subtitle was found. Just add the video stream.
|
||||||
if (subtitleStream == null) {
|
if (subtitleStream == null) {
|
||||||
command.add("-map");
|
addVideoStream(command, videoStream);
|
||||||
command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +167,17 @@ public final class FFMpegHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds video mapping to a command
|
||||||
|
*
|
||||||
|
* @param command <p>The list containing the rest of the command.</p>
|
||||||
|
* @param videoStream <p>The video stream to be used.</p>
|
||||||
|
*/
|
||||||
|
private static void addVideoStream(List<String> command, VideoStream videoStream) {
|
||||||
|
command.add("-map");
|
||||||
|
command.add(String.format("0:%d", videoStream.getAbsoluteIndex()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds subtitle commands to a command list
|
* Adds subtitle commands to a command list
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user