Improves converters to remove some code duplication
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user