180 lines
4.4 KiB
Java
180 lines
4.4 KiB
Java
package ffmpegconverter.converter;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.ListIterator;
|
|
|
|
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 {
|
|
String[] streams = probeFile(ffprobePath, file);
|
|
if (streams.length == 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, String[] streams, String outFile) {
|
|
List<String> command = generalFile(executable, fileName);
|
|
List<Integer> audioStreams = listAudio(streams);
|
|
if (audioStreams.size() > 0) {
|
|
command.add("-map");
|
|
command.add("0:" + audioStreams.get(0));
|
|
}
|
|
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);
|
|
}
|
|
public class StringList implements List<String> {
|
|
@Override
|
|
public int size() {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEmpty() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean contains(Object o) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public Iterator<String> iterator() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public Object[] toArray() {
|
|
return new Object[0];
|
|
}
|
|
|
|
@Override
|
|
public <T> T[] toArray(T[] a) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean add(String string) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean remove(Object o) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean containsAll(Collection<?> c) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean addAll(Collection<? extends String> c) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean addAll(int index, Collection<? extends String> c) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean removeAll(Collection<?> c) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean retainAll(Collection<?> c) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void clear() {
|
|
|
|
}
|
|
|
|
@Override
|
|
public String get(int index) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String set(int index, String element) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void add(int index, String element) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public String remove(int index) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public int indexOf(Object o) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int lastIndexOf(Object o) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public ListIterator<String> listIterator() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public ListIterator<String> listIterator(int index) {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public List<String> subList(int fromIndex, int toIndex) {
|
|
return null;
|
|
}
|
|
}
|
|
} |