Changes main package name and fixes files with weird names throwing errors
All checks were successful
KnarCraft/FFmpegConvert/master This commit looks good

Fixes conversion of files with a single quote or square brackets in the filename
Moves all files into the net.knarcraft.ffmpegconverter package
This commit is contained in:
2020-02-21 17:23:00 +01:00
parent c80193281d
commit 4e1cebd95d
13 changed files with 39 additions and 40 deletions

View File

@ -0,0 +1,68 @@
package net.knarcraft.ffmpegconverter.converter;
import net.knarcraft.ffmpegconverter.streams.AudioStream;
import net.knarcraft.ffmpegconverter.streams.StreamObject;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class AudioConverter extends Converter {
private String newExt;
public AudioConverter(String ffprobePath, String ffmpegPath, String newExt) {
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<StreamObject> streams = probeFile(ffprobePath, file);
if (streams.size() == 0) {
throw new IllegalArgumentException("The file has no streams");
}
String newPath = stripExtension(file) + "." + newExt;
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<StreamObject> streams, String outFile) {
List<String> command = generalFile(executable, fileName);
List<AudioStream> audioStreams = filterStreamsByType(streams, "audio");
AudioStream audioStream = null;
if (audioStreams.size() > 0) {
audioStream = audioStreams.get(0);
}
if (audioStreams.size() > 0) {
command.add("-map");
command.add("0:" + audioStream.getAbsoluteIndex());
}
command.add(outFile);
return command.toArray(new String[0]);
}
@Override
public String[] getValidFormats() {
return AUDIO_FORMATS;
}
@Override
public void convert(File file) throws IOException {
processFile(file.getParentFile(), file, newExt);
}
}