From 54d3f925e6199e97956db443bfa1fffa10b554e8 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:53:19 +0200 Subject: [PATCH] add trackId to album downloads --- plugins/downloader/back-downloader.js | 18 +++++++++--------- plugins/downloader/menu.js | 9 ++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/downloader/back-downloader.js b/plugins/downloader/back-downloader.js index 9cf89c96..63923301 100644 --- a/plugins/downloader/back-downloader.js +++ b/plugins/downloader/back-downloader.js @@ -24,8 +24,9 @@ module.exports = async (options_) => { ipcMain.handle("download-song", (_, url) => downloadSong(url)); }; -async function downloadSong(url, playlistFolder = undefined) { +async function downloadSong(url, playlistFolder = undefined, trackId = undefined) { const metadata = await getMetadata(url); + metadata.trackId = trackId; const stream = await yt.download(metadata.id, { type: 'audio', // audio, video or video+audio @@ -53,7 +54,7 @@ async function downloadSong(url, playlistFolder = undefined) { } if (!presets[options.preset]) { - const fileBuffer = await toMP3(iterableStream, filePath, metadata); + const fileBuffer = await toMP3(iterableStream, metadata); console.info('writing id3 tags...'); // DELETE writeFileSync(filePath, await writeID3(fileBuffer, metadata)); console.info('done writing id3 tags!'); // DELETE @@ -78,7 +79,6 @@ function getIdFromUrl(url) { async function getMetadata(url) { const id = getIdFromUrl(url); const info = await yt.music.getInfo(id); - //console.log('got info:' + JSON.stringify(info, null, 2)); // DELETE return { id: info.basic_info.id, @@ -120,6 +120,9 @@ async function writeID3(buffer, metadata) { }); } } + if (metadata.trackId) { + writer.setFrame("TRCK", metadata.trackId); + } writer.addTag(); return Buffer.from(writer.arrayBuffer); } catch (e) { @@ -138,7 +141,7 @@ const ffmpeg = require("@ffmpeg/ffmpeg").createFFmpeg({ const ffmpegMutex = new Mutex(); -async function toMP3(stream, filePath, metadata, extension = "mp3") { +async function toMP3(stream, metadata, extension = "mp3") { const chunks = []; for await (const chunk of stream) { chunks.push(chunk); @@ -165,13 +168,9 @@ async function toMP3(stream, filePath, metadata, extension = "mp3") { safeVideoName + "." + extension ); - const fileBuffer = ffmpeg.FS("readFile", safeVideoName + "." + extension); - - await writeID3(fileBuffer, metadata); - // sendFeedback("Saving…"); - return fileBuffer; + return ffmpeg.FS("readFile", safeVideoName + "." + extension); } catch (e) { sendError(e); } finally { @@ -210,5 +209,6 @@ function getFFmpegMetadataArgs(metadata) { ...(metadata.title ? ["-metadata", `title=${metadata.title}`] : []), ...(metadata.artist ? ["-metadata", `artist=${metadata.artist}`] : []), ...(metadata.album ? ["-metadata", `album=${metadata.album}`] : []), + ...(metadata.trackId ? ["-metadata", `track=${metadata.trackId}`] : []), ]; }; diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index b133394e..81959e35 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -96,6 +96,10 @@ async function downloadPlaylist(givenUrl, win, options) { sendError(e); return; } + let isAlbum = playlist.title.startsWith('Album - '); + if (isAlbum) { + playlist.title = playlist.title.slice(8); + } const safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' }); const folder = getFolder(options.downloadFolder); @@ -143,9 +147,12 @@ async function downloadPlaylist(givenUrl, win, options) { } }); + let counter = 1; + try { for (const song of playlist.items) { - await downloadSong(song.url, playlistFolder).catch((e) => sendError(e)); + const trackId = isAlbum ? counter++ : undefined; + await downloadSong(song.url, playlistFolder, trackId).catch((e) => sendError(e)); } } catch (e) { sendError(e);