Improves converters to remove some code duplication
This commit is contained in:
		| @@ -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 <p>The folder of the file to process.</p> | ||||
|      * @param file   <p>The file to process.</p> | ||||
|      * @throws IOException <p>If the BufferedReader fails.</p> | ||||
|      */ | ||||
|     void processFile(File folder, File file) throws IOException { | ||||
|         List<StreamObject> 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); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 <p>The folder of the file to process.</p> | ||||
|      * @param file   <p>The file to process.</p> | ||||
|      * @throws IOException <p>If the BufferedReader fails.</p> | ||||
|      */ | ||||
|     private void processFile(File folder, File file) throws IOException { | ||||
|         List<StreamObject> 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 <p>The executable file for ffmpeg.</p> | ||||
|      * @param fileName   <p>The input file.</p> | ||||
|      * @param streams    <p>A list of ffprobe streams.</p> | ||||
|      * @param outFile    <p>The output file.</p> | ||||
|      * @return <p>A list of commands</p> | ||||
|      */ | ||||
|     private String[] builderCommand(String executable, String fileName, List<StreamObject> streams, String outFile, | ||||
|                                     File file) { | ||||
|         List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, fileName); | ||||
|     @Override | ||||
|     public String[] builderCommand(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); | ||||
|   | ||||
| @@ -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      <p>The extension of the new file.</p> | ||||
|      */ | ||||
|     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 <p>The work folder containing the file.</p> | ||||
|      * @param file   <p>The file to convert.</p> | ||||
|      * @param newExt <p>The extension of the new file.</p> | ||||
|      * @throws IOException <p>If the file cannot be converted.</p> | ||||
|      */ | ||||
|     private void processFile(File folder, File file, String newExt) throws IOException { | ||||
|         List<StreamObject> 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 <p>The executable file for ffmpeg.</p> | ||||
|      * @param fileName   <p>The input file.</p> | ||||
|      * @param streams    <p>A list of ffprobe streams.</p> | ||||
|      * @param outFile    <p>The output file.</p> | ||||
|      * @return <p>A list of commands.</p> | ||||
|      */ | ||||
|     private String[] builderCommand(String executable, String fileName, List<StreamObject> streams, String outFile) { | ||||
|         List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, fileName); | ||||
|     @Override | ||||
|     public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { | ||||
|         List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); | ||||
|         List<AudioStream> 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); | ||||
|     } | ||||
| } | ||||
| @@ -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 <p>If the file cannot be converted.</p> | ||||
|      */ | ||||
|     void convert(File file) throws IOException; | ||||
|  | ||||
|     /** | ||||
|      * Generates a command for a ProcessBuilder. | ||||
|      * | ||||
|      * @param executable <p>The executable file for ffmpeg.</p> | ||||
|      * @param file       <p>The input file.</p> | ||||
|      * @param streams    <p>A list of ffprobe streams.</p> | ||||
|      * @param outFile    <p>The output file.</p> | ||||
|      * @return <p>A list of commands</p> | ||||
|      */ | ||||
|     String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile); | ||||
| } | ||||
|   | ||||
| @@ -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 <p>Path/command to ffprobe.</p> | ||||
|      * @param ffmpegPath  <p>Path/command to ffmpeg.</p> | ||||
|      * @param newExt      <p>The extension of the new file.</p> | ||||
|      * @param newExtension      <p>The extension of the new file.</p> | ||||
|      */ | ||||
|     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 <p>The folder of the file to process.</p> | ||||
|      * @param file   <p>The file to process.</p> | ||||
|      * @throws IOException <p>If the BufferedReader fails.</p> | ||||
|      */ | ||||
|     private void processFile(File folder, File file, String newExt) throws IOException { | ||||
|         List<StreamObject> 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 <p>The executable file for ffmpeg.</p> | ||||
|      * @param file       <p>The input file.</p> | ||||
|      * @param streams    <p>A list of ffprobe streams.</p> | ||||
|      * @param outFile    <p>The output file.</p> | ||||
|      * @return <p>A list of commands</p> | ||||
|      */ | ||||
|     private String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { | ||||
|     @Override | ||||
|     public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) { | ||||
|         List<String> 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); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user