From 58dcb7fd356cc875fb6c2a79582e5bb978ec70bd Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 11 May 2020 13:31:45 +0200 Subject: [PATCH] Improves utility code --- .../ffmpegconverter/utility/FFMpegHelper.java | 146 +++++++++--------- .../ffmpegconverter/utility/FileUtil.java | 47 ++++-- .../ffmpegconverter/utility/ListUtilTest.java | 28 ++++ 3 files changed, 136 insertions(+), 85 deletions(-) diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java index a16db35..872ec4d 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/FFMpegHelper.java @@ -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

The executable to use (ffmpeg/ffprobe).

+ * @param fileName

The name of the file to execute on.

+ * @return

A base list of ffmpeg commands for converting a video for web

+ */ + public static List getFFMpegWebVideoCommand(String executable, String fileName) { + List 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

The executable to use (ffmpeg/ffprobe).

+ * @param fileName

The name of the file to execute on.

+ * @return

A base list of ffmpeg commands for converting a file.

+ */ + public static List getFFMpegGeneralFileCommand(String executable, String fileName) { + List 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

The list containing the command to run.

+ * @param start

The offset before converting.

+ * @param length

The offset for stopping the conversion.

+ */ + public static void addDebugArguments(List 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

The process to run.

+ * @param folder

The folder the process should run in.

+ * @throws IOException

If the process can't be readProcess.

+ */ + 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

The process to run.

- * @param folder

The folder the process should run in.

- * @throws IOException

If the process can't be readProcess.

- */ - 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

The executable to use (ffmpeg/ffprobe).

- * @param fileName

The name of the file to execute on.

- * @return

A base list of ffmpeg commands for converting a video for web

- */ - public static List getFFMpegWebVideoCommand(String executable, String fileName) { - List 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

The executable to use (ffmpeg/ffprobe).

- * @param fileName

The name of the file to execute on.

- * @return

A base list of ffmpeg commands for converting a file.

- */ - public static List getFFMpegGeneralFileCommand(String executable, String fileName) { - List 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

The list containing the command to run.

- * @param start

The offset before converting.

- * @param length

The offset for stopping the conversion.

- */ - public static void addDebugArguments(List 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 * diff --git a/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java b/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java index 0dfaf1c..156273d 100644 --- a/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java +++ b/src/main/java/net/knarcraft/ffmpegconverter/utility/FileUtil.java @@ -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

The folder containing the output file.

+ * @param file

The input file.

+ * @param outExtension

The extension of the output file.

+ * @return

A file name with the new extension and without any collisions.

+ */ + 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); - if (nextLevel != null) { - listOfFiles = ListUtil.concatenate(listOfFiles, nextLevel); - } + //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) { + if (foundFiles == null) { + foundFiles = nextLevel; + } else { + foundFiles = ListUtil.concatenate(foundFiles, nextLevel); } } } - return listOfFiles; + return foundFiles; } /** diff --git a/src/test/java/net/knarcraft/ffmpegconverter/utility/ListUtilTest.java b/src/test/java/net/knarcraft/ffmpegconverter/utility/ListUtilTest.java index 137c45f..42c3554 100644 --- a/src/test/java/net/knarcraft/ffmpegconverter/utility/ListUtilTest.java +++ b/src/test/java/net/knarcraft/ffmpegconverter/utility/ListUtilTest.java @@ -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 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 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); + } + }