diff --git a/src/zone/arctic/ytpplus/EffectsFactory.java b/src/zone/arctic/ytpplus/EffectsFactory.java index cba599e..95543d8 100644 --- a/src/zone/arctic/ytpplus/EffectsFactory.java +++ b/src/zone/arctic/ytpplus/EffectsFactory.java @@ -13,266 +13,186 @@ import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; - + public class EffectsFactory { - + public Utilities toolBox; - + public EffectsFactory(Utilities utilities) { this.toolBox = utilities; } - + public String pickSound() { - FilenameFilter fileFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - String lowercase = name.toLowerCase(); - if (lowercase.endsWith(".mp3")) - return true; - else - return false; - } - }; - File[] files = new File(toolBox.SOUNDS).listFiles(fileFilter); + File[] files = new File(toolBox.SOUNDS).listFiles(); Random rand = new Random(); File file = files[rand.nextInt(files.length)]; - return file.getName(); + return file.getPath(); } public String pickSource() { - FilenameFilter fileFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - String lowercase = name.toLowerCase(); - if (lowercase.endsWith(".mp4")) - return true; - else - return false; - } - }; - File[] files = new File(toolBox.SOURCES).listFiles(fileFilter); + File[] files = new File(toolBox.SOURCES).listFiles(); Random rand = new Random(); File file = files[rand.nextInt(files.length)]; - return file.getName(); + return file.getPath(); } - + public String pickMusic() { - FilenameFilter fileFilter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - String lowercase = name.toLowerCase(); - if (lowercase.endsWith(".mp3")) - return true; - else - return false; - } - }; - File[] files = new File(toolBox.MUSIC).listFiles(fileFilter); + File[] files = new File(toolBox.MUSIC).listFiles(); Random rand = new Random(); File file = files[rand.nextInt(files.length)]; - return file.getName(); + return file.getPath(); } - + /* EFFECTS */ public void effect_RandomSound(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String randomSound = pickSound(); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -i "+ toolBox.SOUNDS + randomSound - + " -filter_complex \"[1:a]volume=1,apad[A];[0:a][A]amerge[out]\"" - + " -ac 2" - //+ " -c:v copy" - - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - - + " -map 0:v" - + " -map [out]" - + " -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-i", pickSound(), + "-filter_complex", "[1:a]volume=1,apad[A];[0:a][A]amerge[out]", + "-ac", "2", + "-ar", "44100", + "-vf", "scale=640x480,setsar=1:1,fps=fps=30", + "-map", "0:v", + "-map", "[out]", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } + } public void effect_RandomSoundMute(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { String randomSound = pickSound(); - String soundLength = toolBox.getLength(toolBox.SOUNDS+randomSound); + String soundLength = toolBox.getLength(randomSound); System.out.println("Doing a mute now. " + randomSound + " length: " + soundLength + "."); - //Scanner userInput = new Scanner(System.in); - //String input = userInput.nextLine(); - - //if (!input.isEmpty()) { - File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); - File temp2 = new File(toolBox.TEMP + "temp2.mp4"); - if (in.exists()) - in.renameTo(temp); - if (temp2.exists()) - temp2.delete(); - String command1 = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -af \"volume=0\" -y " + toolBox.TEMP + "temp2.mp4"; - String command2 = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp2.mp4" - + " -i \"" + toolBox.SOUNDS +""+randomSound+"\"" - + " -to "+soundLength - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -filter_complex \"[1:a]volume=1,apad[A]; [0:a][A]amerge[out]\" -ac 2 -map 0:v -map [out] -y " + video; - //String command1 = "lib/ffmpeg -i " + toolBox.TEMP + "temp.mp4 -i sounds/"+randomSound+" -c:v copy -map 0:v:0 -map 1:a:0 -shortest "+ video; - CommandLine cmdLine = CommandLine.parse(command1); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); - - cmdLine = CommandLine.parse(command2); - executor = new DefaultExecutor(); - exitValue = executor.execute(cmdLine); - - temp.delete(); + File in = new File(video); + File temp = new File(toolBox.getTempVideoName()); + File temp2 = new File(toolBox.getTempVideoName()); + if (in.exists()) + in.renameTo(temp); + if (temp2.exists()) temp2.delete(); - //} - //System.out.println("Did a mute sfx. Type anything to verify."); + toolBox.execFFmpeg("-i", temp.getPath(), + "-af", "volume=0", + "-y", temp2.getPath()); + toolBox.execFFmpeg("-i", temp2.getPath(), + "-i", randomSound, + "-to", soundLength, + "-ar", "44100", + "-filter_complex", "[1:a]volume=1,apad[A];[0:a][A]amerge[out]", + "-ac", "2", + "-map", "0:v", + "-map", "[out]", + "-y", video); + temp.delete(); + temp2.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } + } public void effect_Reverse(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); - File temp2 = new File(toolBox.TEMP + "temp2.mp4"); + File temp = new File(toolBox.getTempVideoName()); + File temp2 = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); if (temp2.exists()) temp2.delete(); - String command1 = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4 -map 0" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -af \"areverse\" -y " + toolBox.TEMP + "temp2.mp4"; - String command2 = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp2.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -vf reverse -y " + video; - - CommandLine cmdLine = CommandLine.parse(command1); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); - - cmdLine = CommandLine.parse(command2); - executor = new DefaultExecutor(); - exitValue = executor.execute(cmdLine); - + + toolBox.execFFmpeg("-i", temp.getPath(), + "-map", "0", + "-af", "areverse", + "-y", temp2.getPath()); + toolBox.execFFmpeg("-i", temp2.getPath(), + "-vf", "reverse", + "-y", video); temp.delete(); temp2.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } - - + } + + public void effect_SpeedUp(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -filter:v setpts=0.5*PTS -filter:a atempo=2.0 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-filter:v", "setpts=0.5*PTS", + "-filter:a", "atempo=2.0", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } - + } + public void effect_SlowDown(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -filter:v setpts=2*PTS -filter:a atempo=0.5 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-filter:v", "setpts=2*PTS", + "-filter:a", "atempo=0.5", + "-y", video); temp.delete(); } catch (Exception ex) { System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex); } - } - + } + public void effect_Chorus(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -af chorus=0.5:0.9:50|60|40:0.4|0.32|0.3:0.25|0.4|0.3:2|2.3|1.3 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-af", "chorus=0.5:0.9:50|60|40:0.4|0.32|0.3:0.25|0.4|0.3:2|2.3|1.3", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } + } public void effect_Vibrato(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -af vibrato=f=7.0:d=0.5 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-af", "vibrato=f=7.0:d=0.5", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } - + } + public void effect_LowPitch(String video) { System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -filter:v setpts=2*PTS -af asetrate=44100*0.5,aresample=44100 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-filter:v", "setpts=2*PTS", + "-af", "asetrate=44100*0.5,aresample=44100", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} } @@ -281,36 +201,32 @@ public void effect_HighPitch(String video) { System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - File temp = new File(toolBox.TEMP + "temp.mp4"); + File temp = new File(toolBox.getTempVideoName()); if (in.exists()) in.renameTo(temp); - String command = toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -filter:v setpts=0.5*PTS -af asetrate=44100*2,aresample=44100 -y " + video; - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-filter:v", "setpts=0.5*PTS", + "-af", "asetrate=44100*2,aresample=44100", + "-y", video); temp.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} } - + public void effect_Dance(String video){ System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - - File temp = new File(toolBox.TEMP + "temp.mp4"); //og file - File temp2 = new File(toolBox.TEMP + "temp2.mp4"); //1st cut - File temp3 = new File(toolBox.TEMP + "temp3.mp4"); //backwards (silent - File temp4 = new File(toolBox.TEMP + "temp4.mp4"); //forwards (silent - File temp5 = new File(toolBox.TEMP + "temp5.mp4"); //backwards & forwards concatenated - File temp6 = new File(toolBox.TEMP + "temp6.mp4"); //backwards & forwards concatenated - File temp7 = new File(toolBox.TEMP + "temp7.mp4"); //backwards & forwards concatenated - + + File temp = new File(toolBox.getTempVideoName()); //og file + File temp2 = new File(toolBox.getTempVideoName()); //1st cut + File temp3 = new File(toolBox.getTempVideoName()); //backwards (silent + File temp4 = new File(toolBox.getTempVideoName()); //forwards (silent + File temp5 = new File(toolBox.getTempVideoName()); //backwards & forwards concatenated + File temp6 = new File(toolBox.getTempVideoName()); //backwards & forwards concatenated + // final result is backwards & forwards concatenated with music - + if (in.exists()) in.renameTo(temp); if (temp2.exists()) @@ -323,190 +239,136 @@ public void effect_Dance(String video){ temp5.delete(); if (temp6.exists()) temp6.delete(); - if (temp7.exists()) - temp7.delete(); - + String randomSound = pickMusic(); - - /* - lib/ffmpeg -i stu.mp4 -map 0 -ar 44100 -to 00:00:00.5 -vf "scale=640x480,setsar=1:1" -an -y " + toolBox.TEMP + "temp2.mp4 - lib/ffmpeg -i " + toolBox.TEMP + "temp2.mp4 -map 0 -ar 44100 -vf "reverse,scale=640x480,setsar=1:1" -y " + toolBox.TEMP + "temp3.mp4 - lib/ffmpeg -i " + toolBox.TEMP + "temp3.mp4 -map 0 -ar 44100 -vf "reverse,scale=640x480,setsar=1:1" -y " + toolBox.TEMP + "temp4.mp4 - lib/ffmpeg -i " + toolBox.TEMP + "temp3.mp4 -i " + toolBox.TEMP + "temp4.mp4 -filter_complex "[0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0]concat=n=8:v=1[outv]" -map "[outv]" -c:v libx264 -shortest -y " + toolBox.TEMP + "temp5.mp4 - lib/ffmpeg -i " + toolBox.TEMP + "temp5.mp4 -map 0 -ar 44100 -vf "setpts=0.5*PTS,scale=640x480,setsar=1:1" -af "atempo=2.0" -shortest -y " + toolBox.TEMP + "temp6.mp4 - lib/ffmpeg -i " + toolBox.TEMP + "temp6.mp4 -i music/dancemusic.mp3 -c:v libx264 -map 0:v:0 -map 1:a:0 -vf "scale=640x480,setsar=1:1,fps=fps=30" -shortest -y " + toolBox.TEMP + "temp7.mp4 - */ - ArrayList commands = new ArrayList(); - int randomTime = randomInt(3,9); - int randomTime2 = randomInt(0,1); - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4 -map 0"// -c:v copy" - + " -ar 44100" - + " -to 00:00:0"+randomTime2+"." + randomTime - + " -vf scale=640x480,setsar=1:1" - + " -an" - + " -y " + toolBox.TEMP + "temp2.mp4"); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp2.mp4 -map 0"// -c:v copy" - + " -ar 44100" - + " -vf reverse,scale=640x480,setsar=1:1" - + " -y " + toolBox.TEMP + "temp3.mp4"); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp3.mp4" - + " -ar 44100" - + " -vf reverse,scale=640x480,setsar=1:1" - + " -y " + toolBox.TEMP + "temp4.mp4"); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp3.mp4" - + " -i " + toolBox.TEMP + "temp4.mp4" - + " -filter_complex \"[0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0]concat=n=8:v=1[outv]\"" - + " -map \"[outv]\"" - + " -c:v libx264 -shortest" - + " -y " + toolBox.TEMP + "temp5.mp4"); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp5.mp4" - + " -map 0" - + " -ar 44100" - + " -vf \"setpts=0.5*PTS,scale=640x480,setsar=1:1\"" - + " -af \"atempo=2.0\"" - + " -shortest" - + " -y " + toolBox.TEMP + "temp6.mp4"); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp6.mp4" - + " -i " + toolBox.MUSIC + randomSound - + " -c:v libx264" - + " -map 0:v:0 -map 1:a:0" - + " -vf \"scale=640x480,setsar=1:1,fps=fps=30\"" - + " -shortest" - + " -y " + video); - - //String command = "lib/ffmpeg -i " + toolBox.TEMP + "temp.mp4 -i sounds/"+randomSound+" -c:v copy -map 0:v:0 -map 1:a:0 -shortest "+ video; - for (String command : commands) { - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); - } - + int randomTime = toolBox.randomInt(3,9); + int randomTime2 = toolBox.randomInt(0,1); + + toolBox.execFFmpeg("-i", temp.getPath(), + "-map", "0", // "-c:v", "copy", + "-to", "00:00:0"+randomTime2+"." + randomTime, + "-an", "-y", temp2.getPath()); + toolBox.execFFmpeg("-i", temp2.getPath(), + "-map", "0", // "-c:v", "copy", + "-vf", "reverse", + "-y", temp3.getPath()); + toolBox.execFFmpeg("-i", temp3.getPath(), + "-vf", "reverse", + "-y", temp4.getPath()); + toolBox.execFFmpeg("-i", temp3.getPath(), + "-i", temp4.getPath(), + "-filter_complex", "[0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0][0:v:0][1:v:0]concat=n=8:v=1[outv]", + "-map", "[outv]", + "-c:v", "libx264", + "-shortest", "-y", temp5.getPath()); + toolBox.execFFmpeg("-i", temp5.getPath(), + "-map", "0", + "-vf", "setpts=0.5*PTS", + "-af", "atempo=2.0", + "-shortest", "-y", temp6.getPath()); + toolBox.execFFmpeg("-i", temp6.getPath(), + "-i", randomSound, + "-c:v", "libx264", + "-map", "0:v:0", + "-map", "1:a:0", + "-shortest", "-y", video); + temp.delete(); temp2.delete(); temp3.delete(); temp4.delete(); temp5.delete(); temp6.delete(); - temp7.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } - - public void effect_Squidward(String video){ + } + + public void effect_Squidward(String video) { System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + " initiated"); try { File in = new File(video); - - File temp = new File(toolBox.TEMP + "temp.mp4"); //og file - + File temp = new File(toolBox.getTempVideoName()); //og file + int pictureNum = toolBox.randomInt(); + String picturePrefix = toolBox.TEMP + pictureNum + "-"; + // final result is backwards & forwards concatenated with music - + if (in.exists()) in.renameTo(temp); - - ArrayList commands = new ArrayList(); - - commands.add(toolBox.FFMPEG - + " -i " + toolBox.TEMP + "temp.mp4"// -c:v copy" - + " -vf \"select=gte(n\\,1)\"" - + " -vframes 1" - + " -y " + toolBox.TEMP + "squidward0.png"); - - for (int i=1; i<6; i++) { - String effect = ""; - int random = randomInt(0,6); - switch (random) { - case 0: - effect = " -flop"; + + toolBox.execFFmpeg("-i", temp.getPath(), + "-vf", "select=gte(n\\,1)", + "-vframes", "1", + "-y", picturePrefix + "squidward0.png"); + + for (int i = 1; i < 6; i++) { + ArrayList cmdLine = new ArrayList(); + cmdLine.add("convert"); + cmdLine.add(picturePrefix + "squidward0.png"); + switch (toolBox.randomInt(0, 4)) { + case 0: + cmdLine.add("-flop"); break; case 1: - effect = " -flip"; + cmdLine.add("-flip"); break; case 2: - effect = " -implode -" + randomInt(1,3); + cmdLine.add("-implode"); + cmdLine.add((toolBox.randomInt(0, 1) == 0 ? "-" : "") + toolBox.randomInt(1, 3)); break; case 3: - effect = " -implode " + randomInt(1,3); + cmdLine.add("-swirl"); + cmdLine.add((toolBox.randomInt(0, 1) == 0 ? "-" : "") + toolBox.randomInt(1, 180)); break; case 4: - effect = " -swirl " + randomInt(1,180); - break; - case 5: - effect = " -swirl -" + randomInt(1,180); - break; - case 6: - effect = " -channel RGB -negate"; + cmdLine.add("-channel"); + cmdLine.add("RGB"); + cmdLine.add("-negate"); break; //case 7: - // effect = " -virtual-pixel Black +distort Cylinder2Plane " + randomInt(1,90); + // effect = " -virtual-pixel Black +distort Cylinder2Plane " + toolBox.randomInt(1,90); // break; } - commands.add(toolBox.MAGICK - + " convert " + toolBox.TEMP + "squidward0.png" - + effect - + " " + toolBox.TEMP + "squidward" + i + ".png" - ); + cmdLine.add(picturePrefix + "squidward" + i + ".png"); + toolBox.execMagick(cmdLine.toArray(new String[cmdLine.size()])); } - commands.add(toolBox.MAGICK - + " convert -size 640x480 canvas:black " + toolBox.TEMP + "black.png"); - - if (new File(toolBox.TEMP + "concatsquidward.txt").exists()) - new File(toolBox.TEMP + "concatsquidward.txt").delete(); - PrintWriter writer = new PrintWriter(toolBox.TEMP + "concatsquidward.txt", "UTF-8"); + toolBox.execMagick("convert", "-size", "640x480", "canvas:black", + picturePrefix + "black.png"); + + File squidwardScript = new File(picturePrefix + "concatsquidward.txt"); + if (squidwardScript.exists()) + squidwardScript.delete(); + PrintWriter writer = new PrintWriter(squidwardScript, "UTF-8"); writer.write - ("file 'squidward0.png'\n" + + ("file " + pictureNum + "-squidward0.png\n" + "duration 0.467\n" + - "file 'squidward1.png'\n" + + "file " + pictureNum + "-squidward1.png\n" + "duration 0.434\n" + - "file 'squidward2.png'\n" + + "file " + pictureNum + "-squidward2.png\n" + "duration 0.4\n" + - "file 'black.png'\n" + + "file " + pictureNum + "-black.png'\n" + "duration 0.834\n" + - "file 'squidward3.png'\n" + + "file " + pictureNum + "-squidward3.png\n" + "duration 0.467\n" + - "file 'squidward4.png'\n" + + "file " + pictureNum + "-squidward4.png\n" + "duration 0.4\n" + - "file 'squidward5.png'\n" + + "file " + pictureNum + "-squidward5.png\n" + "duration 0.467"); writer.close(); - - commands.add(toolBox.FFMPEG - + " -f concat" - + " -i " + toolBox.TEMP + "concatsquidward.txt" - + " -i " + toolBox.RESOURCES + "squidward/music.wav" - + " -map 0:v:0 -map 1:a:0" - + " -vf \"scale=640x480,setsar=1:1\"" - + " -pix_fmt yuv420p" - + " -y " + video); - - //String command = "lib/ffmpeg -i " + toolBox.TEMP + "temp.mp4 -i sounds/"+randomSound+" -c:v copy -map 0:v:0 -map 1:a:0 -shortest "+ video; - for (String command : commands) { - System.out.println("Executing: " + command); - CommandLine cmdLine = CommandLine.parse(command); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); - } - + /* Using "\\" slashes breaks concat (and thus squidward) on + * Windows breaks + */ + toolBox.execFFmpeg("-f", "concat", + "-i", picturePrefix + "concatsquidward.txt", + "-i", toolBox.RESOURCES + "squidward/music.wav", + "-map", "0:v:0", "-map", "1:a:0", "-pix_fmt", "yuv420p", + "-y", video); + temp.delete(); - for (int i=0; i<6; i++) { - new File(toolBox.TEMP + "squidward"+i+".png").delete(); - } - new File(toolBox.TEMP + "black.png").delete(); - new File(toolBox.TEMP + "concatsquidward.txt").delete(); + for (int i = 0; i < 6; i++) + new File(picturePrefix + "squidward" + i + ".png").delete(); + + new File(picturePrefix + "black.png").delete(); + squidwardScript.delete(); } catch (Exception ex) {System.out.println(new Object() {}.getClass().getEnclosingMethod().getName() + "\n" +ex);} - } - - public static int randomInt(int min, int max) { - return new Random().nextInt((max - min) + 1) + min; } } diff --git a/src/zone/arctic/ytpplus/TimeStamp.java b/src/zone/arctic/ytpplus/TimeStamp.java index f10e8d8..10427cc 100644 --- a/src/zone/arctic/ytpplus/TimeStamp.java +++ b/src/zone/arctic/ytpplus/TimeStamp.java @@ -16,14 +16,14 @@ public class TimeStamp { private int HOURS; private int MINUTES; private double SECONDS; - + public TimeStamp(String time) { String[] parts = time.split(":"); this.HOURS = Integer.parseInt(parts[0]); this.MINUTES = Integer.parseInt(parts[1]); this.SECONDS = Double.parseDouble(parts[2]); } - + public TimeStamp(double l) { double ms = l*1000; @@ -34,17 +34,17 @@ public TimeStamp(double l) { double minutes = x % 60; x /= 60; double hours = x % 24; - + this.HOURS = (int)hours; this.MINUTES = (int)minutes; - this.SECONDS = seconds + (millis/1000); - + this.SECONDS = (int)seconds + (millis/1000); + } - + public double getLengthSec() { return SECONDS + (MINUTES*60) + (HOURS*60*60); } - + public int getHours() { return HOURS; } @@ -54,13 +54,13 @@ public int getMinutes() { public double getSeconds() { return SECONDS; } - + public void getDeets() { System.out.println("HOURS: " + this.HOURS); System.out.println("MIN: " + this.MINUTES); System.out.println("SEC: " + this.SECONDS); } - + public String getTimeStamp() { return this.HOURS + ":" + this.MINUTES + ":" + this.SECONDS; } diff --git a/src/zone/arctic/ytpplus/Utilities.java b/src/zone/arctic/ytpplus/Utilities.java index d9b5872..83e3100 100644 --- a/src/zone/arctic/ytpplus/Utilities.java +++ b/src/zone/arctic/ytpplus/Utilities.java @@ -3,6 +3,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; +import java.util.Random; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; @@ -12,62 +13,38 @@ * @author benb */ public class Utilities { - + public String FFPROBE; public String FFMPEG; public String MAGICK; - + public String TEMP = ""; public String SOURCES = ""; public String SOUNDS = ""; public String MUSIC = ""; public String RESOURCES = ""; + private Random random = new Random(); - /** - * Return the length of a video (in seconds) - * - * @param video input video filename to work with - * @return Video length as a string (output from ffprobe) - */ - public String getVideoLength(String video){ - try { - Runtime rt = Runtime.getRuntime(); - Process proc = rt.exec(FFPROBE - + " -v error" - + " -sexagesimal" - + " -show_entries format=duration" - + " -of default=noprint_wrappers=1:nokey=1" - + " \"" + video + "\""); - BufferedReader stdInput = new BufferedReader(new - InputStreamReader(proc.getInputStream())); - String s; - Thread.sleep(100); - while ((s = stdInput.readLine()) != null) { - return s; - } - } catch (Exception ex) {ex.printStackTrace(); return "";} - return ""; - } - public String getLength(String file) { try { Runtime rt = Runtime.getRuntime(); - Process proc = rt.exec(FFPROBE - + " -i " + file - + " -show_entries format=duration" - + " -v quiet" - + " -of csv=\"p=0\""); - BufferedReader stdInput = new BufferedReader(new - InputStreamReader(proc.getInputStream())); + Process proc = rt.exec(new String[] { + FFPROBE, + "-i", file, + "-show_entries", "format=duration", + "-v", "error", + "-of", "csv=p=0" + }); + BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); String s; - Thread.sleep(100); + proc.waitFor(); while ((s = stdInput.readLine()) != null) { return s; } } catch (Exception ex) {System.out.println(ex); return "";} return ""; } - + /** * Snip a video file between the start and end time, and save it to an output file. * @@ -78,25 +55,23 @@ public String getLength(String file) { */ public void snipVideo(String video, TimeStamp startTime, TimeStamp endTime, String output){ try { - String command1 = FFMPEG - + " -i " + video - + " -ss " + startTime.getTimeStamp() - + " -to " + endTime.getTimeStamp() - + " -ac 1" - + " -ar 44100" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -y" - + " " + output + ".mp4"; - CommandLine cmdLine = CommandLine.parse(command1); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + int exitValue = execFFmpeg( + "-ss", startTime.getTimeStamp(), + "-i", video, + "-to", endTime.getTimeStamp(), + "-copyts", + "-ac", "1", + "-ar", "44100", + "-vf", "scale=640x480,setsar=1:1,fps=fps=30", + "-y", output + ); if (exitValue==1) { System.out.println("ERROR"); System.exit(0); } } catch (Exception ex) {System.out.println(ex);} - } - + } + /** * Copies a video and encodes it in the proper format without changes. * @@ -105,70 +80,82 @@ public void snipVideo(String video, TimeStamp startTime, TimeStamp endTime, Stri */ public void copyVideo(String video, String output){ try { - String command1 = FFMPEG - + " -i " + video - + " -ar 44100" - + " -ac 1" - //+ " -filter:v fps=fps=30,setsar=1:1" - + " -vf scale=640x480,setsar=1:1,fps=fps=30" - + " -y" - + " " + output + ".mp4"; - CommandLine cmdLine = CommandLine.parse(command1); - DefaultExecutor executor = new DefaultExecutor(); - int exitValue = executor.execute(cmdLine); + int exitValue = execFFmpeg( + "-i", video, + "-ac", "1", + "-ar", "44100", + "-vf", "scale=640x480,setsar=1:1,fps=fps=30", + "-y", output + ); if (exitValue==1) { System.out.println("ERROR"); System.exit(0); } } catch (Exception ex) {System.out.println(ex);} - } - + } + /** * Concatenate videos by count * * @param count number of input videos to concatenate * @param out output video filename */ - public void concatenateVideo(int count, String out ) { + public void concatenateVideo(int count, String out) { try { File export = new File(out); - + if (export.exists()) export.delete(); - - String command1 = FFMPEG; - - for (int i=0; i sourceList = new ArrayList(); public volatile boolean done = false; public volatile double doneCount = 0; - + public YTPGenerator(String output) { this.OUTPUT_FILE = output; //configurate(); } - + public YTPGenerator(String output, double min, double max) { this.OUTPUT_FILE = output; this.MIN_STREAM_DURATION = min; @@ -84,7 +84,7 @@ public YTPGenerator(String output, double min, double max, int maxclips) { this.MAX_CLIPS = maxclips; //configurate(); } - + public void setMaxClips(int clips) { this.MAX_CLIPS = clips; } @@ -94,7 +94,7 @@ public void setMinDuration(double min) { public void setMaxDuration(double max) { this.MAX_STREAM_DURATION = max; } - + public void addSource(String sourceName) { sourceList.add(sourceName); } @@ -114,7 +114,7 @@ public void run() { System.out.println("No sources added..."); return; } - + System.out.println("poop_1"); File out = new File(OUTPUT_FILE); if (out.exists()) { @@ -124,9 +124,8 @@ public void run() { try { PrintWriter writer = new PrintWriter(toolBox.TEMP+"concat.txt", "UTF-8"); - for (int i = 0; i < MAX_CLIPS; i++) { - doneCount = (double) i/MAX_CLIPS; - String sourceToPick = sourceList.get(randomInt(0, sourceList.size() - 1)); + IntStream.range(0, MAX_CLIPS).parallel().forEach(i -> { + String sourceToPick = sourceList.get(toolBox.randomInt(0, sourceList.size() - 1)); System.out.println(sourceToPick); System.out.println(toolBox.getLength(sourceToPick)); TimeStamp boy = new TimeStamp(Double.parseDouble(toolBox.getLength(sourceToPick))); @@ -137,16 +136,16 @@ public void run() { TimeStamp endOfClip = new TimeStamp(startOfClip.getLengthSec() + randomDouble(MIN_STREAM_DURATION, MAX_STREAM_DURATION)); System.out.println("Beginning of clip " + i + ": " + startOfClip.getTimeStamp()); System.out.println("Ending of clip " + i + ": " + endOfClip.getTimeStamp() + ", in seconds: "); - if (randomInt(0, 15) == 15 && insertTransitionClips==true) { + String clipToWorkWith = toolBox.TEMP+"video" + i + ".mp4"; + if (toolBox.randomInt(0, 15) == 15 && insertTransitionClips==true) { System.out.println("Tryina use a diff source"); - toolBox.copyVideo(toolBox.SOURCES + effectsFactory.pickSource(), toolBox.TEMP+"video" + i); + toolBox.copyVideo(effectsFactory.pickSource(), clipToWorkWith); } else { - toolBox.snipVideo(sourceToPick, startOfClip, endOfClip, toolBox.TEMP+"video" + i); + toolBox.snipVideo(sourceToPick, startOfClip, endOfClip, clipToWorkWith); } //Add a random effect to the video - int effect = randomInt(0, 16); + int effect = toolBox.randomInt(0, 16); System.out.println("STARTING EFFECT ON CLIP " + i + " EFFECT" + effect); - String clipToWorkWith = toolBox.TEMP+"video" + i + ".mp4"; switch (effect) { case 1: //random sound @@ -191,14 +190,15 @@ public void run() { effectsFactory.effect_Dance(clipToWorkWith); break; case 11: - if (effect11==true) + if (effect11==true && toolBox.randomInt(0, 99) < 50) effectsFactory.effect_Squidward(clipToWorkWith); break; default: + //toolBox.convertVideo(clipToWorkWith); break; } - - } + doneCount += 1.0 / MAX_CLIPS; + }); for (int i = 0; i < MAX_CLIPS; i++) { if (new File(toolBox.TEMP+"video" + i + ".mp4").exists()) { writer.write("file 'video" + i + ".mp4'\n"); //writing to same folder @@ -211,35 +211,29 @@ public void run() { } catch (Exception ex) { ex.printStackTrace(); } - //for (int i=0; i<100; i++) { cleanUp(); rmDir(new File(toolBox.TEMP)); done = true; } }; vidThread.start(); - + } - - + + public boolean isDone() { return done; } - + public static double randomDouble(double min, double max) { double finalVal = -1; while (finalVal<0) { double x = (Math.random() * ((max - min) + 0)) + min; - finalVal=Math.round(x * 100.0) / 100.0; + finalVal=Math.round(x * 100.0) / 100.0; } return finalVal; } - - public static int randomInt(int min, int max) { - return new Random().nextInt((max - min) + 1) + min; - } - - + public void cleanUp() { //Create concatenation text file File text = new File(toolBox.TEMP+"concat.txt"); @@ -247,7 +241,7 @@ public void cleanUp() { text.delete(); File mp4 = new File(toolBox.TEMP + "temp.mp4"); if (mp4.exists()) - mp4.delete(); + mp4.delete(); for (int i=0; i