diff --git a/plugins/downloader/back.js b/plugins/downloader/back.js index 49b5eea5..2e2ad01f 100644 --- a/plugins/downloader/back.js +++ b/plugins/downloader/back.js @@ -7,6 +7,7 @@ const { dialog, ipcMain } = require("electron"); const getSongInfo = require("../../providers/song-info"); const { injectCSS, listenAction } = require("../utils"); const { ACTIONS, CHANNEL } = require("./actions.js"); +const { getImage } = require("../../providers/song-info"); const sendError = (win, err) => { const dialogOpts = { @@ -41,10 +42,12 @@ function handle(win) { } }); - ipcMain.on("add-metadata", (event, filePath, songBuffer, currentMetadata) => { + ipcMain.on("add-metadata", async (event, filePath, songBuffer, currentMetadata) => { let fileBuffer = songBuffer; const songMetadata = { ...metadata, ...currentMetadata }; - + if (!songMetadata.image && songMetadata.imageSrc) { + songMetadata.image = await getImage(songMetadata.imageSrc) + } try { const coverBuffer = songMetadata.image.toPNG(); const writer = new ID3Writer(songBuffer); diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index e4930f09..b023965c 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -38,13 +38,18 @@ const baseUrl = defaultConfig.url; // contextBridge.exposeInMainWorld("downloader", { // download: () => { global.download = () => { + let metadata; let videoUrl = getSongMenu() .querySelector("ytmusic-menu-navigation-item-renderer") .querySelector("#navigation-endpoint") .getAttribute("href"); - videoUrl = !videoUrl - ? global.songInfo.url || window.location.href - : baseUrl + "/" + videoUrl; + if (videoUrl) { + videoUrl = baseUrl + "/" + videoUrl; + metadata = null; + } else { + videoUrl = global.songInfo.url || window.location.href; + metadata = global.songInfo; + } downloadVideoToMP3( videoUrl, @@ -61,7 +66,7 @@ global.download = () => { }, reinit, pluginOptions, - global.songInfo + metadata ); }; // }); diff --git a/plugins/downloader/youtube-dl.js b/plugins/downloader/youtube-dl.js index 1a212aaf..97d49ef2 100644 --- a/plugins/downloader/youtube-dl.js +++ b/plugins/downloader/youtube-dl.js @@ -24,7 +24,14 @@ const ffmpeg = createFFmpeg({ }); const ffmpegMutex = new Mutex(); -const downloadVideoToMP3 = ( +function noTopic(channelName) { + if (channelName && channelName.endsWith(" - Topic")) { + channelName = channelName.slice(0, -8); + } + return channelName; +} + +const downloadVideoToMP3 = async ( videoUrl, sendFeedback, sendError, @@ -35,6 +42,16 @@ const downloadVideoToMP3 = ( ) => { sendFeedback("Downloading…"); + if (metadata === null) { + const info = await ytdl.getInfo(videoUrl); + const thumbnails = info.videoDetails?.author?.thumbnails; + metadata = { + artist: info.videoDetails?.media?.artist || noTopic(info.videoDetails?.author?.name) || "", + title: info.videoDetails?.media?.song || info.videoDetails?.title || "", + imageSrc: thumbnails ? thumbnails[thumbnails.length - 1].url : "" + } + } + let videoName = "YouTube Music - Unknown title"; let videoReadableStream; try { @@ -135,6 +152,7 @@ const toMP3 = async ( ipcRenderer.send("add-metadata", filePath, fileBuffer, { artist: metadata.artist, title: metadata.title, + imageSrc: metadata.imageSrc || "" }); ipcRenderer.once("add-metadata-done", reinit); } catch (e) {