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 fileNameThe name of the file to execute on.
+ * @returnA base list of ffmpeg commands for converting a video for web
+ */ + public static ListThe executable to use (ffmpeg/ffprobe).
+ * @param fileNameThe name of the file to execute on.
+ * @returnA base list of ffmpeg commands for converting a file.
+ */ + public static ListThe list containing the command to run.
+ * @param startThe offset before converting.
+ * @param lengthThe offset for stopping the conversion.
+ */ + public static void addDebugArguments(ListThe process to run.
+ * @param folderThe folder the process should run in.
+ * @throws IOExceptionIf 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 processThe process to run.
- * @param folderThe folder the process should run in.
- * @throws IOExceptionIf 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 executableThe executable to use (ffmpeg/ffprobe).
- * @param fileNameThe name of the file to execute on.
- * @returnA base list of ffmpeg commands for converting a video for web
- */ - public static ListThe executable to use (ffmpeg/ffprobe).
- * @param fileNameThe name of the file to execute on.
- * @returnA base list of ffmpeg commands for converting a file.
- */ - public static ListThe list containing the command to run.
- * @param startThe offset before converting.
- * @param lengthThe offset for stopping the conversion.
- */ - public static void addDebugArguments(ListThe folder containing the output file.
+ * @param fileThe input file.
+ * @param outExtensionThe extension of the output file.
+ * @returnA 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