mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-10 10:11:46 +00:00
Download plugin - advanced options (folder, FFmpeg args)
This commit is contained in:
@ -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,
|
||||
|
||||
@ -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")
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user