Skip to content

Commit

Permalink
download using youtubei,js instead of ytdl-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed Mar 3, 2023
1 parent 7bdbab5 commit be489f7
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 308 deletions.
5 changes: 0 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ if(config.get("options.singleInstanceLock")){
});
}

app.commandLine.appendSwitch(
"js-flags",
// WebAssembly flags
"--experimental-wasm-threads"
);
app.commandLine.appendSwitch("enable-features", "SharedArrayBuffer"); // Required for downloader
app.allowRendererProcessReuse = true; // https://github.com/electron/electron/issues/18397
if (config.get("options.disableHardwareAcceleration")) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,18 @@
"electron-store": "^8.1.0",
"electron-unhandled": "^4.0.1",
"electron-updater": "^5.3.0",
"file-type": "^18.2.1",
"filenamify": "^4.3.0",
"howler": "^2.2.3",
"html-to-text": "^9.0.3",
"md5": "^2.3.0",
"mpris-service": "^2.1.2",
"node-fetch": "^2.6.8",
"node-id3": "^0.2.6",
"node-notifier": "^10.0.1",
"simple-youtube-age-restriction-bypass": "https://gitpkg.now.sh/api/pkg.tgz?url=zerodytrash/Simple-YouTube-Age-Restriction-Bypass&commit=v2.5.4",
"vudio": "^2.1.1",
"youtubei.js": "^2.9.0",
"youtubei.js": "^3.1.1",
"ytdl-core": "^4.11.1",
"ytpl": "^2.3.0"
},
Expand Down
52 changes: 5 additions & 47 deletions plugins/downloader/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ const sendError = (win, error) => {

let nowPlayingMetadata = {};

function handle(win) {

function handle(win, options) {
injectCSS(win.webContents, join(__dirname, "style.css"));

require("./youtube-dl")(options);

registerCallback((info) => {
nowPlayingMetadata = info;
});
Expand All @@ -46,52 +50,6 @@ function handle(win) {
console.log("Unknown action: " + action);
}
});

ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => {
let fileBuffer = songBuffer;
const songMetadata = currentMetadata.imageSrcYTPL ? // This means metadata come from ytpl.getInfo();
{
...currentMetadata,
image: cropMaxWidth(await getImage(currentMetadata.imageSrcYTPL))
} :
{ ...nowPlayingMetadata, ...currentMetadata };

try {
const coverBuffer = songMetadata.image && !songMetadata.image.isEmpty() ?
songMetadata.image.toPNG() : null;

const writer = new ID3Writer(songBuffer);

// Create the metadata tags
writer
.setFrame("TIT2", songMetadata.title)
.setFrame("TPE1", [songMetadata.artist]);
if (coverBuffer) {
writer.setFrame("APIC", {
type: 3,
data: coverBuffer,
description: ""
});
}
if (isEnabled("lyrics-genius")) {
const lyrics = await fetchFromGenius(songMetadata);
if (lyrics) {
writer.setFrame("USLT", {
description: lyrics,
lyrics: lyrics,
});
}
}
writer.addTag();
fileBuffer = Buffer.from(writer.arrayBuffer);
} catch (error) {
sendError(win, error);
}

writeFileSync(filePath, fileBuffer);
// Notify the youtube-dl file
event.reply("add-metadata-done");
});
}

module.exports = handle;
Expand Down
24 changes: 2 additions & 22 deletions plugins/downloader/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { defaultConfig } = require("../../config");
const { getSongMenu } = require("../../providers/dom-elements");
const { ElementFromFile, templatePath, triggerAction } = require("../utils");
const { ACTIONS, CHANNEL } = require("./actions.js");
const { downloadVideoToMP3 } = require("./youtube-dl");

let menu = null;
let progress = null;
Expand Down Expand Up @@ -61,28 +60,9 @@ global.download = () => {
videoUrl = metadata.url || window.location.href;
}

downloadVideoToMP3(
videoUrl,
(feedback, ratio = undefined) => {
if (!progress) {
console.warn("Cannot update progress");
} else {
progress.innerHTML = feedback;
}
if (ratio) {
triggerAction(CHANNEL, ACTIONS.PROGRESS, ratio);
}
},
(error) => {
triggerAction(CHANNEL, ACTIONS.ERROR, error);
reinit();
},
reinit,
pluginOptions,
metadata
);
ipcRenderer.invoke('download-song', videoUrl).finally(() => triggerAction(CHANNEL, ACTIONS.PROGRESS, -1));
return;
};
// });

function observeMenu(options) {
pluginOptions = { ...pluginOptions, ...options };
Expand Down
31 changes: 19 additions & 12 deletions plugins/downloader/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const filenamify = require('filenamify');

const { setMenuOptions } = require("../../config/plugins");
const { sendError } = require("./back");
const { downloadSong } = require("./youtube-dl");
const { defaultMenuDownloadLabel, getFolder, presets, setBadge } = require("./utils");

let downloadLabel = defaultMenuDownloadLabel;
Expand Down Expand Up @@ -95,7 +96,7 @@ async function downloadPlaylist(givenUrl, win, options) {
sendError(win, e);
return;
}
const safePlaylistTitle = filenamify(playlist.title, {replacement: ' '});
const safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' });

const folder = getFolder(options.downloadFolder);
const playlistFolder = join(folder, safePlaylistTitle);
Expand Down Expand Up @@ -128,24 +129,30 @@ async function downloadPlaylist(givenUrl, win, options) {
setBadge(playlist.items.length);

let dirWatcher = chokidar.watch(playlistFolder);
dirWatcher.on('add', () => {
downloadCount += 1;
if (downloadCount >= playlist.items.length) {
const closeDirWatcher = () => {
if (dirWatcher) {
win.setProgressBar(-1); // close progress bar
setBadge(0); // close badge counter
dirWatcher.close().then(() => (dirWatcher = null));
}
};
dirWatcher.on('add', () => {
downloadCount += 1;
if (downloadCount >= playlist.items.length) {
closeDirWatcher();
} else {
win.setProgressBar(downloadCount / playlist.items.length);
setBadge(playlist.items.length - downloadCount);
}
});

playlist.items.forEach((song) => {
win.webContents.send(
"downloader-download-playlist",
song.url,
safePlaylistTitle,
options
);
});
try {
for (const song of playlist.items) {
await downloadSong(song.url, playlistFolder).catch((e) => sendError(win, e));
}
} catch (e) {
sendError(win, e);
} finally {
closeDirWatcher();
}
}
6 changes: 3 additions & 3 deletions plugins/downloader/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const electron = require("electron");
const { app } = require("electron");
const is = require('electron-is');

module.exports.getFolder = customFolder => customFolder || electron.app.getPath("downloads");
module.exports.getFolder = customFolder => customFolder || app.getPath("downloads");
module.exports.defaultMenuDownloadLabel = "Download playlist";

const orderedQualityList = ["maxresdefault", "hqdefault", "mqdefault", "sdddefault"];
Expand Down Expand Up @@ -41,6 +41,6 @@ module.exports.presets = {

module.exports.setBadge = n => {
if (is.linux() || is.macOS()) {
electron.app.setBadgeCount(n);
app.setBadgeCount(n);
}
}

0 comments on commit be489f7

Please sign in to comment.