Improves utility code
All checks were successful
KnarCraft/FFmpegConvert/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2020-05-11 13:31:45 +02:00
parent b42b713e73
commit 58dcb7fd35
3 changed files with 136 additions and 85 deletions

View File

@ -35,6 +35,79 @@ public final class FFMpegHelper {
return parseStreams(ffprobePath, probeForStreams(ffprobePath, file), file);
}
/**
* Creates a list containing all required arguments for converting a video to a web playable video
*
* @param executable <p>The executable to use (ffmpeg/ffprobe).</p>
* @param fileName <p>The name of the file to execute on.</p>
* @return <p>A base list of ffmpeg commands for converting a video for web</p>
*/
public static List<String> getFFMpegWebVideoCommand(String executable, String fileName) {
List<String> command = getFFMpegGeneralFileCommand(executable, fileName);
command.add("-vcodec");
command.add("h264");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-ar");
command.add("48000");
command.add("-movflags");
command.add("+faststart");
return command;
}
/**
* Creates a list containing command line arguments for a general file
*
* @param executable <p>The executable to use (ffmpeg/ffprobe).</p>
* @param fileName <p>The name of the file to execute on.</p>
* @return <p>A base list of ffmpeg commands for converting a file.</p>
*/
public static List<String> getFFMpegGeneralFileCommand(String executable, String fileName) {
List<String> command = new ArrayList<>();
command.add(executable);
command.add("-nostdin");
command.add("-i");
command.add(fileName);
return command;
}
/**
* Adds debugging parameters for only converting parts of a file
*
* @param command <p>The list containing the command to run.</p>
* @param start <p>The offset before converting.</p>
* @param length <p>The offset for stopping the conversion.</p>
*/
public static void addDebugArguments(List<String> command, int start, int length) {
command.add("-ss");
command.add("" + start);
command.add("-t");
command.add("" + length);
}
/**
* Starts and prints output of a process
*
* @param process <p>The process to run.</p>
* @param folder <p>The folder the process should run in.</p>
* @throws IOException <p>If the process can't be readProcess.</p>
*/
public static void convertProcess(ProcessBuilder process, File folder) throws IOException {
OutputUtil.print("Command to be run: ");
OutputUtil.println(process.command().toString());
process.directory(folder);
process.redirectErrorStream(true);
Process processConvert = process.start();
BufferedReader readerConvert = new BufferedReader(new InputStreamReader(processConvert.getInputStream()));
while (processConvert.isAlive()) {
String read = readProcess(readerConvert, "\n");
if (!read.equals("")) {
OutputUtil.println(read);
}
}
OutputUtil.println("Process finished.");
}
/**
* Gets a list of all streams in a file
*
@ -129,29 +202,6 @@ public final class FFMpegHelper {
return parsedStreams;
}
/**
* Starts and prints output of a process
*
* @param process <p>The process to run.</p>
* @param folder <p>The folder the process should run in.</p>
* @throws IOException <p>If the process can't be readProcess.</p>
*/
public static void convertProcess(ProcessBuilder process, File folder) throws IOException {
OutputUtil.print("Command to be run: ");
OutputUtil.println(process.command().toString());
process.directory(folder);
process.redirectErrorStream(true);
Process processConvert = process.start();
BufferedReader readerConvert = new BufferedReader(new InputStreamReader(processConvert.getInputStream()));
while (processConvert.isAlive()) {
String read = readProcess(readerConvert, "\n");
if (!read.equals("")) {
OutputUtil.println(read);
}
}
OutputUtil.println("Process is finished.");
}
/**
* Reads from a process reader
*
@ -168,56 +218,6 @@ public final class FFMpegHelper {
return text.toString().trim();
}
/**
* Creates a list containing all required arguments for converting a video to a web playable video
*
* @param executable <p>The executable to use (ffmpeg/ffprobe).</p>
* @param fileName <p>The name of the file to execute on.</p>
* @return <p>A base list of ffmpeg commands for converting a video for web</p>
*/
public static List<String> getFFMpegWebVideoCommand(String executable, String fileName) {
List<String> command = getFFMpegGeneralFileCommand(executable, fileName);
command.add("-vcodec");
command.add("h264");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-ar");
command.add("48000");
command.add("-movflags");
command.add("+faststart");
return command;
}
/**
* Creates a list containing command line arguments for a general file
*
* @param executable <p>The executable to use (ffmpeg/ffprobe).</p>
* @param fileName <p>The name of the file to execute on.</p>
* @return <p>A base list of ffmpeg commands for converting a file.</p>
*/
public static List<String> getFFMpegGeneralFileCommand(String executable, String fileName) {
List<String> command = new ArrayList<>();
command.add(executable);
command.add("-nostdin");
command.add("-i");
command.add(fileName);
return command;
}
/**
* Adds debugging parameters for only converting parts of a file
*
* @param command <p>The list containing the command to run.</p>
* @param start <p>The offset before converting.</p>
* @param length <p>The offset for stopping the conversion.</p>
*/
public static void addDebugArguments(List<String> command, int start, int length) {
command.add("-ss");
command.add("" + start);
command.add("-t");
command.add("" + length);
}
/**
* Parses a list of video stream parameters to a video stream object
*

View File

@ -14,6 +14,19 @@ public final class FileUtil {
private FileUtil() {
}
/**
* Gets the path described by the input, but changed to account for collisions
*
* @param folder <p>The folder containing the output file.</p>
* @param file <p>The input file.</p>
* @param outExtension <p>The extension of the output file.</p>
* @return <p>A file name with the new extension and without any collisions.</p>
*/
public static String getNonCollidingPath(File folder, File file, String outExtension) {
return FileUtil.getNonCollidingFilename(folder.getAbsolutePath() + File.separator +
FileUtil.stripExtension(file) + "." + outExtension, outExtension);
}
/**
* Adds parentheses with an integer if the output file already exists
*
@ -59,26 +72,36 @@ public final class FileUtil {
* @return A list of files
*/
public static File[] listFilesRecursive(File folder, String[] extensions, int maxRecursions) {
//Return if the target depth has been reached
if (maxRecursions == 0) {
return null;
}
File[] listOfFiles = folder.listFiles((file) -> file.isFile() &&
//Get a list of all files which are folders and has one of the extensions specified
File[] foundFiles = folder.listFiles((file) -> file.isFile() &&
ListUtil.listContains(extensions, (item) -> file.getName().endsWith(item)));
if (listOfFiles == null) {
return null;
//Return if recursion is finished
if (maxRecursions == 1) {
return foundFiles;
}
if (maxRecursions > 1) {
File[] listOfFolders = folder.listFiles((dir, name) -> new File(dir, name).isDirectory());
if (listOfFolders != null) {
for (File file : listOfFolders) {
File[] nextLevel = listFilesRecursive(file, extensions, maxRecursions - 1);
//Get all folders in the directory
File[] subFolders = folder.listFiles((dir, name) -> new File(dir, name).isDirectory());
//Return if the folder has no sub folders
if (subFolders == null) {
return foundFiles;
}
for (File subFolder : subFolders) {
//Get all relevant files contained within the sub folder
File[] nextLevel = listFilesRecursive(subFolder, extensions, maxRecursions - 1);
//Add found files to the output
if (nextLevel != null) {
listOfFiles = ListUtil.concatenate(listOfFiles, nextLevel);
if (foundFiles == null) {
foundFiles = nextLevel;
} else {
foundFiles = ListUtil.concatenate(foundFiles, nextLevel);
}
}
}
}
return listOfFiles;
return foundFiles;
}
/**

View File

@ -7,10 +7,13 @@ import java.util.ArrayList;
import java.util.List;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
public class ListUtilTest {
private static List<Integer> matchesList;
private static Integer[] containsList;
@BeforeClass
public static void setUp() {
@ -25,6 +28,7 @@ public class ListUtilTest {
matchesList.add(19);
matchesList.add(21);
matchesList.add(23);
containsList = new Integer[]{1, 3, 5, 7, 234, 23, 45};
}
@Test
@ -58,4 +62,28 @@ public class ListUtilTest {
assertEquals(expected, result);
}
@Test
public void listContainsFalse() {
assertFalse(ListUtil.listContains(containsList, (item) -> item == 0));
}
@Test
public void listContainsTrue() {
assertTrue(ListUtil.listContains(containsList, (item) -> item == 234));
}
@Test
public void getList() {
List<String> inputList = new ArrayList<>();
inputList.add("some test string");
inputList.add("some,test,string");
inputList.add("some te,st string");
String[] result1 = ListUtil.getList(inputList, 0);
String[] result2 = ListUtil.getList(inputList, 1);
String[] result3 = ListUtil.getList(inputList, 2);
assertArrayEquals(new String[]{"some test string"}, result1);
assertArrayEquals(new String[]{"some", "test", "string"}, result2);
assertArrayEquals(new String[]{"some te", "st string"}, result3);
}
}