diff --git a/src/main/java/net/knarcraft/ffmpegconverter/Main.java b/src/main/java/net/knarcraft/ffmpegconverter/Main.java index b96b164..d4c776d 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/Main.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/Main.java @@ -4,6 +4,7 @@ import net.knarcraft.ffmpegconverter.converter.AbstractConverter; import net.knarcraft.ffmpegconverter.converter.AnimeConverter; import net.knarcraft.ffmpegconverter.converter.AudioConverter; import net.knarcraft.ffmpegconverter.converter.VideoConverter; +import net.knarcraft.ffmpegconverter.converter.WebVideoConverter; import net.knarcraft.ffmpegconverter.utility.FileUtil; import net.knarcraft.ffmpegconverter.utility.ListUtil; import net.knarcraft.ffmpegconverter.utility.OutputUtil; @@ -27,15 +28,14 @@ class Main { public static void main(String[] args) throws IOException { loadConverter(); - int recursionSteps = 1; - - OutputUtil.println(" [Recursions]: "); - List input = readInput(2); - while (input.isEmpty()) { - OutputUtil.print("File path required."); + List input; + do { + OutputUtil.println(" [Recursions]:"); input = readInput(2); - } + } while (input.isEmpty()); File folder = new File(input.get(0)); + + int recursionSteps = 1; if (input.size() > 1) { try { recursionSteps = Integer.parseInt(input.get(1)); @@ -45,7 +45,6 @@ class Main { } convertAllFiles(folder, recursionSteps); - OutputUtil.close(); } @@ -56,7 +55,7 @@ class Main { */ 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" + - "3. VideoStream converter", 1, 3); + "3. VideoStream converter", 1, 4); OutputUtil.println("Input for this converter:"); switch (choice) { @@ -69,6 +68,9 @@ class Main { case 3: converter = new VideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); break; + case 4: + converter = new WebVideoConverter(FFPROBE_PATH, FFMPEG_PATH, getChoice("")); + break; default: System.exit(1); } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java index a959db0..cbc40cd 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java @@ -121,7 +121,7 @@ public abstract class AbstractConverter implements Converter { * @param file

The file to process.

* @throws IOException

If the BufferedReader fails.

*/ - void processFile(File folder, File file) throws IOException { + private void processFile(File folder, File file) throws IOException { List streams = FFMpegHelper.probeFile(ffprobePath, file); if (streams.isEmpty()) { 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; } + + @Override + public void convert(File file) throws IOException { + processFile(file.getParentFile(), file); + } } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java index 157079e..ff14aa9 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java @@ -7,7 +7,6 @@ import net.knarcraft.ffmpegconverter.streams.VideoStream; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -41,11 +40,6 @@ public class AnimeConverter extends AbstractConverter { this.preventSignsAndSongs = preventSignsAndSongs; } - @Override - public void convert(File file) throws IOException { - processFile(file.getParentFile(), file); - } - @Override public String[] generateConversionCommand(String executable, File file, List streams, String outFile) { List command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName()); @@ -66,8 +60,8 @@ public class AnimeConverter extends AbstractConverter { VideoStream videoStream = getFirstVideoStream(streams); //Add streams to output file - FFMpegHelper.addAudioStreams(command, audioStream, toStereo); - FFMpegHelper.addSubtitlesAndVideo(command, subtitleStream, videoStream, file); + FFMpegHelper.addAudioStream(command, audioStream, toStereo); + FFMpegHelper.addSubtitleAndVideoStream(command, subtitleStream, videoStream, file); command.add(outFile); return command.toArray(new String[0]); diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java index b7c6916..3d8916f 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java @@ -5,7 +5,6 @@ import net.knarcraft.ffmpegconverter.streams.StreamObject; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; import java.io.File; -import java.io.IOException; 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 AudioStream audioStream = getFirstAudioSteam(streams); - FFMpegHelper.addAudioStreams(command, audioStream, false); + FFMpegHelper.addAudioStream(command, audioStream, false); return command.toArray(new String[0]); } @@ -44,9 +43,4 @@ public class AudioConverter extends AbstractConverter { public String[] getValidFormats() { return audioFormats; } - - @Override - public void convert(File file) throws IOException { - processFile(file.getParentFile(), file); - } } \ No newline at end of file diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java index 9f78506..900b04a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java @@ -9,7 +9,7 @@ import java.util.List; /** * This interface describes a file converter */ -public interface Converter { +interface Converter { /** * Converts the given file diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java index d3b7120..64aca97 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java @@ -1,13 +1,9 @@ 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.io.IOException; import java.util.List; /** @@ -35,16 +31,11 @@ public class VideoConverter extends AbstractConverter { 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.addSubtitlesAndVideo(command, subtitleStream, videoStream, file); - if (audioStream != null) { - FFMpegHelper.addAudioStreams(command, audioStream, true); - } + //Add all streams without re-encoding + command.add("-map"); + command.add("0"); + command.add("-c"); + command.add("copy"); command.add(outFile); return command.toArray(new String[0]); @@ -55,8 +46,4 @@ public class VideoConverter extends AbstractConverter { return videoFormats; } - @Override - public void convert(File file) throws IOException { - processFile(file.getParentFile(), file); - } } \ No newline at end of file diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/WebVideoConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/WebVideoConverter.java new file mode 100644 index 0000000..74363e9 --- /dev/null +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/WebVideoConverter.java @@ -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

Path/command to ffprobe.

+ * @param ffmpegPath

Path/command to ffmpeg.

+ * @param outExtension

The extension of the new file.

+ */ + 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 streams, String outFile) { + List 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]); + } +} diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java index d738998..47a238a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java @@ -129,7 +129,7 @@ public final class FFMpegHelper { * @param audioStream

The audio stream to be added.

* @param toStereo

Whether to convert the audio stream to stereo.

*/ - public static void addAudioStreams(List command, AudioStream audioStream, boolean toStereo) { + public static void addAudioStream(List command, AudioStream audioStream, boolean toStereo) { if (audioStream != null) { command.add("-map"); command.add("0:" + audioStream.getAbsoluteIndex()); @@ -149,12 +149,11 @@ public final class FFMpegHelper { * @param videoStream

The video stream to be used.

* @param file

The file to convert.

*/ - public static void addSubtitlesAndVideo(List command, SubtitleStream subtitleStream, - VideoStream videoStream, File file) { + public static void addSubtitleAndVideoStream(List command, SubtitleStream subtitleStream, + VideoStream videoStream, File file) { //No appropriate subtitle was found. Just add the video stream. if (subtitleStream == null) { - command.add("-map"); - command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); + addVideoStream(command, videoStream); return; } @@ -168,6 +167,17 @@ public final class FFMpegHelper { } } + /** + * Adds video mapping to a command + * + * @param command

The list containing the rest of the command.

+ * @param videoStream

The video stream to be used.

+ */ + private static void addVideoStream(List command, VideoStream videoStream) { + command.add("-map"); + command.add(String.format("0:%d", videoStream.getAbsoluteIndex())); + } + /** * Adds subtitle commands to a command list *