From 7b20b9339deb004fbbbbc004a21f7072e1ced158 Mon Sep 17 00:00:00 2001 From: TC Date: Thu, 3 Dec 2020 18:55:27 +0100 Subject: [PATCH] Download plugin - advanced options (folder, FFmpeg args) --- plugins/downloader/front.js | 7 +++++-- plugins/downloader/youtube-dl.js | 29 ++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index a342c77f..00459281 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -7,6 +7,7 @@ let progress = null; const downloadButton = ElementFromFile( templatePath(__dirname, "download.html") ); +let pluginOptions = {}; const observer = new MutationObserver((mutations, observer) => { if (!menu) { @@ -43,11 +44,13 @@ global.download = () => { triggerAction(CHANNEL, ACTIONS.ERROR, error); reinit(); }, - reinit + reinit, + pluginOptions ); }; -function observeMenu() { +function observeMenu(options) { + pluginOptions = { ...pluginOptions, ...options }; observer.observe(document, { childList: true, subtree: true, diff --git a/plugins/downloader/youtube-dl.js b/plugins/downloader/youtube-dl.js index 540f24d8..3b0c7688 100644 --- a/plugins/downloader/youtube-dl.js +++ b/plugins/downloader/youtube-dl.js @@ -19,7 +19,13 @@ const ffmpeg = createFFmpeg({ progress: () => {}, // console.log, }); -const downloadVideoToMP3 = (videoUrl, sendFeedback, sendError, reinit) => { +const downloadVideoToMP3 = ( + videoUrl, + sendFeedback, + sendError, + reinit, + options +) => { sendFeedback("Downloading…"); let videoName = "YouTube Music - Unknown title"; @@ -54,11 +60,18 @@ const downloadVideoToMP3 = (videoUrl, sendFeedback, sendError, reinit) => { .on("error", sendError) .on("end", () => { const buffer = Buffer.concat(chunks); - toMP3(videoName, buffer, sendFeedback, sendError, reinit); + toMP3(videoName, buffer, sendFeedback, sendError, reinit, options); }); }; -const toMP3 = async (videoName, buffer, sendFeedback, sendError, reinit) => { +const toMP3 = async ( + videoName, + buffer, + sendFeedback, + sendError, + reinit, + options +) => { const safeVideoName = randomBytes(32).toString("hex"); try { @@ -71,11 +84,17 @@ const toMP3 = async (videoName, buffer, sendFeedback, sendError, reinit) => { ffmpeg.FS("writeFile", safeVideoName, buffer); sendFeedback("Converting…"); - await ffmpeg.run("-i", safeVideoName, safeVideoName + ".mp3"); + await ffmpeg.run( + "-i", + safeVideoName, + ...options.ffmpegArgs, + safeVideoName + ".mp3" + ); + const folder = options.downloadFolder || downloadsFolder(); const filename = filenamify(videoName + ".mp3", { replacement: "_" }); writeFileSync( - join(downloadsFolder(), filename), + join(folder, filename), ffmpeg.FS("readFile", safeVideoName + ".mp3") );