diff options
| author | Pinapelz <yukais@pinapelz.com> | 2023-12-23 19:50:35 -0800 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2023-12-23 19:50:35 -0800 |
| commit | fdd19f2d8bd451bc184b576de42eef0376bf736a (patch) | |
| tree | 982a8a95a2b264031318da4f4a395d20879a4714 | |
| parent | d7b88b355664a505a36168e60e8017191a2d3665 (diff) | |
fix: use yt-dlp sections instead of ffmpeg external downloader
| -rw-r--r-- | src/main/java/Downloader.java | 49 |
1 files changed, 22 insertions, 27 deletions
diff --git a/src/main/java/Downloader.java b/src/main/java/Downloader.java index 7a97748..4f01108 100644 --- a/src/main/java/Downloader.java +++ b/src/main/java/Downloader.java @@ -98,17 +98,14 @@ public class Downloader { String endTime = times.get(1); int startSec = timestampToSeconds(startTime); int endSec = timestampToSeconds(endTime); - String ytDlpExecutable = "yt-dlp" + (System.getProperty("os.name").startsWith("Windows") ? ".exe" : ""); - String ffmpegExecutable = "ffmpeg" + (System.getProperty("os.name").startsWith("Windows") ? ".exe" : ""); try { ProcessBuilder builder = new ProcessBuilder( - ytDlpExecutable, - "-vU", - "-f","(bestvideo+bestaudio)", - "--external-downloader", "ffmpeg", - "--external-downloader-args", "ffmpeg_i:-ss " + startSec + " -to " + endSec, - "--output", "downloaded/%(title)s_%(id)s.webm", - "--write-info-json", + "yt-dlp", + "-vU","--force-keyframes", + "-f", "bestaudio[ext=webm]", + "--download-sections","*"+startSec+"-"+endSec, + "-o", "%(title)s[%(id)s].%(ext)s", + "--write-info-json", url ); builder.directory(new File(outputDirectory)); @@ -119,29 +116,28 @@ public class Downloader { JOptionPane.showMessageDialog(null, "An error occurred while downloading using yt-dlp: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } - File downloadedFile = fileUtil.findFileWithType(outputDirectory+"/downloaded", "webm"); + File downloadedWebm = fileUtil.findFileWithType(outputDirectory, "webm"); + try{ - ProcessBuilder ffmpegBuilder = new ProcessBuilder( - ffmpegExecutable, - "-i", downloadedFile.getAbsolutePath(), + ProcessBuilder builder = new ProcessBuilder( + "ffmpeg", + "-i", downloadedWebm.getAbsolutePath(), "-vn", "-ab", "128k", "-ar", "44100", "-y", - downloadedFile.getAbsolutePath().replace(".webm", ".mp3") + downloadedWebm.getAbsolutePath().replace(".webm", ".mp3") ); - ffmpegBuilder.directory(new File(outputDirectory)); - ffmpegBuilder.redirectErrorStream(true); - Process p = ffmpegBuilder.start(); + builder.directory(new File(outputDirectory)); + builder.redirectErrorStream(true); + Process p = builder.start(); relayConsole(p); + + } catch(Exception e){ + JOptionPane.showMessageDialog(null, "An error occurred while converting the webm file to mp3: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } - catch(Exception e){ - JOptionPane.showMessageDialog(null, "An error occurred while converting to mp3: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); - } - fileUtil.deleteFile(downloadedFile.getAbsolutePath()); // Delete old webm file - String info[] = fileUtil.parseInfoJSON(fileUtil.jsonToString(fileUtil.findJsonFile(outputDirectory+"/downloaded"))); + String info[] = fileUtil.parseInfoJSON(fileUtil.jsonToString(fileUtil.findJsonFile(outputDirectory))); String uploader = info[1]; String title = info[0]; String urlID = info[2]; @@ -155,11 +151,10 @@ public class Downloader { File newFile = new File(outputDirectory + "/" + newFileName); oldFile.renameTo(newFile); } - tagMp3InDir(uploader, title, imageUrl, outputDirectory+"/downloaded"); - fileUtil.deleteFile(fileUtil.findJsonFile(outputDirectory+"/downloaded")); - File downloadedMp3 = fileUtil.findFileWithType(outputDirectory+"/downloaded", "mp3"); + tagMp3InDir(uploader, title, imageUrl, outputDirectory); + fileUtil.deleteFile(fileUtil.findJsonFile(outputDirectory)); + File downloadedMp3 = fileUtil.findFileWithType(outputDirectory, "mp3"); downloadedMp3.renameTo(new File(outputDirectory+"/"+downloadedMp3.getName())); - fileUtil.deleteFile(outputDirectory+"/downloaded"); return true; } |
