diff options
| author | Donald Shan <xxpinapelzxx@gmail.com> | 2022-10-22 17:37:25 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-22 17:37:25 -0700 |
| commit | 888e64227a1e7e2ec74d2b9d96751309417bd677 (patch) | |
| tree | 9095367741f25496ec266b9978129238f306d73e | |
| parent | f1402e94fc814a402b5c156e7197b2e11d9f325c (diff) | |
added ability to download parts of a video
Specify in the following fomrat:
url,startTime-endTime
startTime and endTime should be a timestamp format -> HH:MM:SS
| -rw-r--r-- | src/main/java/Main.java | 88 |
1 files changed, 83 insertions, 5 deletions
diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c324ccd..a22ba1e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,7 +2,10 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; +import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.Arrays; + import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.tag.FieldKey; @@ -11,6 +14,7 @@ import org.jaudiotagger.tag.datatype.Artwork; import javax.swing.*; import javax.swing.text.DefaultCaret; + public class Main extends JFrame { String textPath = ""; JPanel panel = new JPanel(); @@ -36,10 +40,26 @@ public class Main extends JFrame { private void downloadAndTag(){ //Main loop ran for checking list of songs, downloading mp3 files, and applying tags ArrayList<String> songs = fileUtil.txtToArrayList(textPath); progress = 0; + String timeAppend = ""; + boolean partFlag = false; for(int i = 0;i<songs.size();i++) { try { fileUtil.deleteAllFilesDir("downloaded"); - youtubeToMP3(songs.get(i)); + ArrayList<String> splitStamp = null; + try{ + splitStamp = new ArrayList<>(Arrays.asList(songs.get(i).split(","))); + } + catch(Exception e) { + + } + if(splitStamp.size()>=2){ + timeAppend = youtubeToMP3Part(splitStamp.get(0),splitStamp.get(1)); + partFlag = true; + } + else{ + youtubeToMP3Full(songs.get(i)); + } + String info[] = fileUtil.parseJson(fileUtil.jsonToString(fileUtil.findJsonFile("downloaded"))); //title,uploader String uploader = info[1]; String title = info[0]; @@ -52,7 +72,15 @@ public class Main extends JFrame { tag.addField(cover); f.commit(); fileUtil.deleteFile("img.jpg"); - fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/" + fileUtil.removeNonAlphaNumeric(info[0]) + " ["+info[2]+ "].mp3"); + if(partFlag){ + fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/" + + fileUtil.removeNonAlphaNumeric(info[0]) + + " ["+info[2]+ "]"+timeAppend+".mp3"); + } + else{ + fileUtil.moveFile(fileUtil.findMP3File("downloaded").getAbsolutePath(), "completed/" + + fileUtil.removeNonAlphaNumeric(info[0]) + " ["+info[2]+ "].mp3"); + } outputArea.setText(outputArea.getText()+"\n"+"Moved file to Completed Folder"); progress = i; System.out.println("Current Progress " + calculatePercentage(i+1,songs.size())); @@ -73,7 +101,7 @@ public class Main extends JFrame { JOptionPane.showMessageDialog(null, message, "JUST YOUR FRIENDLY NEIGHBORLY REMINDER", JOptionPane.WARNING_MESSAGE); } - public static void youtubeToMP3(String url) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd + public static void youtubeToMP3Full(String url) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd try { ProcessBuilder builder = new ProcessBuilder( "yt-dlp.exe", @@ -100,7 +128,57 @@ public class Main extends JFrame { } catch (Exception e) { e.printStackTrace(); } + } + public static String youtubeToMP3Part(String url,String stamp) {//Download mp3 of youtube video using yt-dlp.exe. Ran from cmd + System.out.println(url + " " + stamp); + ArrayList<String> times = new ArrayList<>(Arrays.asList(stamp.split("-"))); + ArrayList<String> startTimeComponents = new ArrayList<>(Arrays.asList(times.get(0).split(":"))); + ArrayList<String> endTimeComponents = new ArrayList<>(Arrays.asList(times.get(1).split(":"))); + int startSec = 0; + int endSec = 0; + if(startTimeComponents.size()==3){ + startSec = Integer.parseInt(startTimeComponents.get(0))*60*60+Integer.parseInt(startTimeComponents.get(1))*60+Integer.parseInt(startTimeComponents.get(2)); + } + else if(startTimeComponents.size()==2){ + startSec = Integer.parseInt(startTimeComponents.get(0))*60+Integer.parseInt(startTimeComponents.get(1)); + } + if(endTimeComponents.size()==3){ + endSec = Integer.parseInt(endTimeComponents.get(0))*60*60+Integer.parseInt(endTimeComponents.get(1))*60+Integer.parseInt(endTimeComponents.get(2)); + } + else if(endTimeComponents.size()==2){ + endSec = Integer.parseInt(endTimeComponents.get(0))*60+Integer.parseInt(endTimeComponents.get(1)); + } + try { + ProcessBuilder builder = new ProcessBuilder( + "yt-dlp.exe", + "--extract-audio", + "--audio-format", "mp3", + "--audio-quality", "0", + "--output", "downloaded/%(title)s_%(id)s.mp3", + "--ffmpeg-location","ffmpeg.exe", + "--write-info-json","--download-sections","\"*"+startSec+"-"+endSec+"\"", + "--force-keyframes-at-cuts", + url + ); + builder.redirectErrorStream(true); + Process p = builder.start(); + BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); + String line; + while (true) { + line = r.readLine(); + if (line == null) { + break; + } + outputArea.setText(outputArea.getText()+"\n"+line); + System.out.println(line); + } + } catch (Exception e) { + e.printStackTrace(); + } + return startSec+"to"+endSec; + } + private void initializeComponents(){//Initiate GUI components this.setDefaultCloseOperation(EXIT_ON_CLOSE); @@ -109,7 +187,7 @@ public class Main extends JFrame { panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS)); scrollPane = new JScrollPane(outputArea); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); - outputArea.setEditable(false); + outputArea.setEditable(true); outputArea.setLineWrap(true); DefaultCaret caret = (DefaultCaret)outputArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); @@ -176,4 +254,4 @@ public class Main extends JFrame { } -}
\ No newline at end of file +} |
