From b42b713e736df1cb8fcbe63b4cafbf0a156937b4 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 11 May 2020 13:30:15 +0200 Subject: [PATCH] Improves converters to remove some code duplication --- .../converter/AbstractConverter.java | 27 ++++++++++++- .../converter/AnimeConverter.java | 39 ++----------------- .../converter/AudioConverter.java | 37 +++--------------- .../ffmpegconverter/converter/Converter.java | 14 +++++++ .../converter/VideoConverter.java | 39 +++---------------- 5 files changed, 55 insertions(+), 101 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java index 977f5a2..cbb5b4f 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AbstractConverter.java @@ -4,7 +4,9 @@ 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.FileUtil; +import net.knarcraft.ffmpegconverter.utility.OutputUtil; import java.io.File; import java.io.IOException; @@ -21,11 +23,13 @@ public abstract class AbstractConverter implements Converter { String ffmpegPath; String[] audioFormats; String[] videoFormats; + private final String newExtension; /** * Initializes variables used by the abstract converter */ - AbstractConverter() { + AbstractConverter(String newExtension) { + this.newExtension = newExtension; try { audioFormats = FileUtil.readFileLines("audio_formats.txt"); videoFormats = FileUtil.readFileLines("video_formats.txt"); @@ -217,4 +221,25 @@ public abstract class AbstractConverter implements Converter { command.add("-profile:v"); command.add("baseline"); } + + /** + * Reads streams from a file, and converts it to an mp4 + * + * @param folder

The folder of the file to process.

+ * @param file

The file to process.

+ * @throws IOException

If the BufferedReader fails.

+ */ + 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" + + " is not corrupt."); + } + String newPath = FileUtil.getNonCollidingPath(folder, file, newExtension); + OutputUtil.println(); + OutputUtil.println("Preparing to start process..."); + OutputUtil.println("Converting " + file); + ProcessBuilder processBuilder = new ProcessBuilder(builderCommand(ffmpegPath, file, streams, newPath)); + FFMpegHelper.convertProcess(processBuilder, folder); + } } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java index 75494ad..98776cc 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AnimeConverter.java @@ -33,6 +33,7 @@ public class AnimeConverter extends AbstractConverter { */ public AnimeConverter(String ffprobePath, String ffmpegPath, String[] audioLang, String[] subtitleLang, boolean toStereo, boolean preventSignsAndSongs) { + super("mp4"); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; this.audioLang = audioLang; @@ -46,41 +47,9 @@ public class AnimeConverter extends AbstractConverter { processFile(file.getParentFile(), file); } - /** - * Reads streams from a file, and converts it to an mp4 - * - * @param folder

The folder of the file to process.

- * @param file

The file to process.

- * @throws IOException

If the BufferedReader fails.

- */ - 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" + - " is not corrupt."); - } - String newPath = FileUtil.getNonCollidingFilename(folder.getAbsolutePath() + File.separator + - FileUtil.stripExtension(file) + ".mp4", "mp4"); - OutputUtil.println(); - OutputUtil.println("Preparing to start process..."); - OutputUtil.println("Converting " + file); - String[] command = builderCommand(ffmpegPath, file.getName(), streams, newPath, file); - ProcessBuilder processBuilder = new ProcessBuilder(command); - FFMpegHelper.convertProcess(processBuilder, folder); - } - - /** - * Generates a command for a ProcessBuilder. - * - * @param executable

The executable file for ffmpeg.

- * @param fileName

The input file.

- * @param streams

A list of ffprobe streams.

- * @param outFile

The output file.

- * @return

A list of commands

- */ - private String[] builderCommand(String executable, String fileName, List streams, String outFile, - File file) { - List command = FFMpegHelper.getFFMpegWebVideoCommand(executable, fileName); + @Override + public String[] builderCommand(String executable, File file, List streams, String outFile) { + List command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName()); if (this.DEBUG) { FFMpegHelper.addDebugArguments(command, 50, 120); diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java index b7f4b99..bb22648 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/AudioConverter.java @@ -3,7 +3,6 @@ package net.knarcraft.ffmpegconverter.converter; import net.knarcraft.ffmpegconverter.streams.AudioStream; import net.knarcraft.ffmpegconverter.streams.StreamObject; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; -import net.knarcraft.ffmpegconverter.utility.FileUtil; import java.io.File; import java.io.IOException; @@ -13,7 +12,6 @@ import java.util.List; * A converter for converting audio files */ public class AudioConverter extends AbstractConverter { - private final String newExtension; /** * Instantiates a new audio converter @@ -23,39 +21,14 @@ public class AudioConverter extends AbstractConverter { * @param newExtension

The extension of the new file.

*/ public AudioConverter(String ffprobePath, String ffmpegPath, String newExtension) { + super(newExtension); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; - this.newExtension = newExtension; } - /** - * Processes a file conversion - * - * @param folder

The work folder containing the file.

- * @param file

The file to convert.

- * @param newExt

The extension of the new file.

- * @throws IOException

If the file cannot be converted.

- */ - private void processFile(File folder, File file, String newExt) throws IOException { - List streams = FFMpegHelper.probeFile(ffprobePath, file); - if (streams.size() == 0) { - throw new IllegalArgumentException("The file has no streams"); - } - String newPath = FileUtil.stripExtension(file) + "." + newExt; - FFMpegHelper.convertProcess(new ProcessBuilder(builderCommand(ffmpegPath, file.getName(), streams, newPath)), folder); - } - - /** - * Generates a command for a ProcessBuilder. - * - * @param executable

The executable file for ffmpeg.

- * @param fileName

The input file.

- * @param streams

A list of ffprobe streams.

- * @param outFile

The output file.

- * @return

A list of commands.

- */ - private String[] builderCommand(String executable, String fileName, List streams, String outFile) { - List command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, fileName); + @Override + public String[] builderCommand(String executable, File file, List streams, String outFile) { + List command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); List audioStreams = filterStreamsByType(streams, "audio"); AudioStream audioStream = null; if (audioStreams.size() > 0) { @@ -76,6 +49,6 @@ public class AudioConverter extends AbstractConverter { @Override public void convert(File file) throws IOException { - processFile(file.getParentFile(), file, newExtension); + 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 17acb80..a143f8a 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/Converter.java @@ -1,7 +1,10 @@ package net.knarcraft.ffmpegconverter.converter; +import net.knarcraft.ffmpegconverter.streams.StreamObject; + import java.io.File; import java.io.IOException; +import java.util.List; /** * This interface describes a file converter @@ -15,4 +18,15 @@ public interface Converter { * @throws IOException

If the file cannot be converted.

*/ void convert(File file) throws IOException; + + /** + * Generates a command for a ProcessBuilder. + * + * @param executable

The executable file for ffmpeg.

+ * @param file

The input file.

+ * @param streams

A list of ffprobe streams.

+ * @param outFile

The output file.

+ * @return

A list of commands

+ */ + String[] builderCommand(String executable, File file, List streams, String outFile); } diff --git a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java index d7f2cce..d5ca136 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/converter/VideoConverter.java @@ -5,7 +5,6 @@ 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.FileUtil; import java.io.File; import java.io.IOException; @@ -15,48 +14,22 @@ import java.util.List; * A converter for converting video files */ public class VideoConverter extends AbstractConverter { - private final String newExt; /** * Instantiates a new video converter * * @param ffprobePath

Path/command to ffprobe.

* @param ffmpegPath

Path/command to ffmpeg.

- * @param newExt

The extension of the new file.

+ * @param newExtension

The extension of the new file.

*/ - public VideoConverter(String ffprobePath, String ffmpegPath, String newExt) { + public VideoConverter(String ffprobePath, String ffmpegPath, String newExtension) { + super(newExtension); this.ffprobePath = ffprobePath; this.ffmpegPath = ffmpegPath; - this.newExt = newExt; } - /** - * Reads streams from a file, and converts it to an mp4 - * - * @param folder

The folder of the file to process.

- * @param file

The file to process.

- * @throws IOException

If the BufferedReader fails.

- */ - private void processFile(File folder, File file, String newExt) throws IOException { - List streams = FFMpegHelper.probeFile(ffprobePath, file); - if (streams.size() == 0) { - throw new IllegalArgumentException("The file has no streams"); - } - String newPath = FileUtil.getNonCollidingFilename(folder.getAbsolutePath() + File.separator + - FileUtil.stripExtension(file) + "." + newExt, newExt); - FFMpegHelper.convertProcess(new ProcessBuilder(builderCommand(ffmpegPath, file, streams, newPath)), folder); - } - - /** - * Generates a command for a ProcessBuilder - * - * @param executable

The executable file for ffmpeg.

- * @param file

The input file.

- * @param streams

A list of ffprobe streams.

- * @param outFile

The output file.

- * @return

A list of commands

- */ - private String[] builderCommand(String executable, File file, List streams, String outFile) { + @Override + public String[] builderCommand(String executable, File file, List streams, String outFile) { List command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); if (this.DEBUG) { @@ -110,6 +83,6 @@ public class VideoConverter extends AbstractConverter { @Override public void convert(File file) throws IOException { - processFile(file.getParentFile(), file, newExt); + processFile(file.getParentFile(), file); } } \ No newline at end of file