Improves converters to remove some code duplication

This commit is contained in:
Kristian Knarvik 2020-05-11 13:30:15 +02:00
parent 7f479fd760
commit b42b713e73
5 changed files with 55 additions and 101 deletions

View File

@ -4,7 +4,9 @@ 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.SubtitleStream;
import net.knarcraft.ffmpegconverter.streams.VideoStream; import net.knarcraft.ffmpegconverter.streams.VideoStream;
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
import net.knarcraft.ffmpegconverter.utility.FileUtil; import net.knarcraft.ffmpegconverter.utility.FileUtil;
import net.knarcraft.ffmpegconverter.utility.OutputUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -21,11 +23,13 @@ public abstract class AbstractConverter implements Converter {
String ffmpegPath; String ffmpegPath;
String[] audioFormats; String[] audioFormats;
String[] videoFormats; String[] videoFormats;
private final String newExtension;
/** /**
* Initializes variables used by the abstract converter * Initializes variables used by the abstract converter
*/ */
AbstractConverter() { AbstractConverter(String newExtension) {
this.newExtension = newExtension;
try { try {
audioFormats = FileUtil.readFileLines("audio_formats.txt"); audioFormats = FileUtil.readFileLines("audio_formats.txt");
videoFormats = FileUtil.readFileLines("video_formats.txt"); videoFormats = FileUtil.readFileLines("video_formats.txt");
@ -217,4 +221,25 @@ public abstract class AbstractConverter implements Converter {
command.add("-profile:v"); command.add("-profile:v");
command.add("baseline"); 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);
}
} }

View File

@ -33,6 +33,7 @@ public class AnimeConverter extends AbstractConverter {
*/ */
public AnimeConverter(String ffprobePath, String ffmpegPath, String[] audioLang, String[] subtitleLang, public AnimeConverter(String ffprobePath, String ffmpegPath, String[] audioLang, String[] subtitleLang,
boolean toStereo, boolean preventSignsAndSongs) { boolean toStereo, boolean preventSignsAndSongs) {
super("mp4");
this.ffprobePath = ffprobePath; this.ffprobePath = ffprobePath;
this.ffmpegPath = ffmpegPath; this.ffmpegPath = ffmpegPath;
this.audioLang = audioLang; this.audioLang = audioLang;
@ -46,41 +47,9 @@ public class AnimeConverter extends AbstractConverter {
processFile(file.getParentFile(), file); processFile(file.getParentFile(), file);
} }
/** @Override
* Reads streams from a file, and converts it to an mp4 public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) {
* List<String> command = FFMpegHelper.getFFMpegWebVideoCommand(executable, file.getName());
* @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);
if (this.DEBUG) { if (this.DEBUG) {
FFMpegHelper.addDebugArguments(command, 50, 120); FFMpegHelper.addDebugArguments(command, 50, 120);

View File

@ -3,7 +3,6 @@ package net.knarcraft.ffmpegconverter.converter;
import net.knarcraft.ffmpegconverter.streams.AudioStream; import net.knarcraft.ffmpegconverter.streams.AudioStream;
import net.knarcraft.ffmpegconverter.streams.StreamObject; import net.knarcraft.ffmpegconverter.streams.StreamObject;
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
import net.knarcraft.ffmpegconverter.utility.FileUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -13,7 +12,6 @@ import java.util.List;
* A converter for converting audio files * A converter for converting audio files
*/ */
public class AudioConverter extends AbstractConverter { public class AudioConverter extends AbstractConverter {
private final String newExtension;
/** /**
* Instantiates a new audio converter * Instantiates a new audio converter
@ -23,39 +21,14 @@ public class AudioConverter extends AbstractConverter {
* @param newExtension <p>The extension of the new file.</p> * @param newExtension <p>The extension of the new file.</p>
*/ */
public AudioConverter(String ffprobePath, String ffmpegPath, String newExtension) { public AudioConverter(String ffprobePath, String ffmpegPath, String newExtension) {
super(newExtension);
this.ffprobePath = ffprobePath; this.ffprobePath = ffprobePath;
this.ffmpegPath = ffmpegPath; this.ffmpegPath = ffmpegPath;
this.newExtension = newExtension;
} }
/** @Override
* Processes a file conversion public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) {
* List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName());
* @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);
List<AudioStream> audioStreams = filterStreamsByType(streams, "audio"); List<AudioStream> audioStreams = filterStreamsByType(streams, "audio");
AudioStream audioStream = null; AudioStream audioStream = null;
if (audioStreams.size() > 0) { if (audioStreams.size() > 0) {
@ -76,6 +49,6 @@ public class AudioConverter extends AbstractConverter {
@Override @Override
public void convert(File file) throws IOException { public void convert(File file) throws IOException {
processFile(file.getParentFile(), file, newExtension); processFile(file.getParentFile(), file);
} }
} }

View File

@ -1,7 +1,10 @@
package net.knarcraft.ffmpegconverter.converter; package net.knarcraft.ffmpegconverter.converter;
import net.knarcraft.ffmpegconverter.streams.StreamObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List;
/** /**
* This interface describes a file converter * This interface describes a file converter
@ -15,4 +18,15 @@ public interface Converter {
* @throws IOException <p>If the file cannot be converted.</p> * @throws IOException <p>If the file cannot be converted.</p>
*/ */
void convert(File file) throws IOException; 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);
} }

View File

@ -5,7 +5,6 @@ import net.knarcraft.ffmpegconverter.streams.StreamObject;
import net.knarcraft.ffmpegconverter.streams.SubtitleStream; import net.knarcraft.ffmpegconverter.streams.SubtitleStream;
import net.knarcraft.ffmpegconverter.streams.VideoStream; import net.knarcraft.ffmpegconverter.streams.VideoStream;
import net.knarcraft.ffmpegconverter.utility.FFMpegHelper; import net.knarcraft.ffmpegconverter.utility.FFMpegHelper;
import net.knarcraft.ffmpegconverter.utility.FileUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -15,48 +14,22 @@ import java.util.List;
* A converter for converting video files * A converter for converting video files
*/ */
public class VideoConverter extends AbstractConverter { public class VideoConverter extends AbstractConverter {
private final String newExt;
/** /**
* Instantiates a new video converter * Instantiates a new video converter
* *
* @param ffprobePath <p>Path/command to ffprobe.</p> * @param ffprobePath <p>Path/command to ffprobe.</p>
* @param ffmpegPath <p>Path/command to ffmpeg.</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.ffprobePath = ffprobePath;
this.ffmpegPath = ffmpegPath; this.ffmpegPath = ffmpegPath;
this.newExt = newExt;
} }
/** @Override
* Reads streams from a file, and converts it to an mp4 public String[] builderCommand(String executable, File file, List<StreamObject> streams, String outFile) {
*
* @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) {
List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName()); List<String> command = FFMpegHelper.getFFMpegGeneralFileCommand(executable, file.getName());
if (this.DEBUG) { if (this.DEBUG) {
@ -110,6 +83,6 @@ public class VideoConverter extends AbstractConverter {
@Override @Override
public void convert(File file) throws IOException { public void convert(File file) throws IOException {
processFile(file.getParentFile(), file, newExt); processFile(file.getParentFile(), file);
} }
} }