From 4608e7ca93d7b819cf75695d9ae727d35ecd70eb Mon Sep 17 00:00:00 2001 From: Syl Date: Tue, 28 May 2024 16:34:10 -0500 Subject: [PATCH] tweak music detection and fix certain errors relating to it --- build.js | 2 +- html/afterload/conversionOptions.js | 18 +++++++++++++++--- util/ytdlp.js | 4 ++-- util/ytdlpUtil/qualitySorter.js | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/build.js b/build.js index e4e4652..b700828 100644 --- a/build.js +++ b/build.js @@ -356,7 +356,7 @@ if(require.main == module) { console.log(`Spawning npm with args "${procArgs.join(` `)}"`); - const proc = child_process.spawn(npm, procArgs, { stdio: "inherit" }); + const proc = child_process.spawn(npm, procArgs, { stdio: "inherit", shell: true }); proc.on(`close`, async (code) => { console.log(`Build closed with code ${code}`); diff --git a/html/afterload/conversionOptions.js b/html/afterload/conversionOptions.js index 98efb1f..957c42b 100644 --- a/html/afterload/conversionOptions.js +++ b/html/afterload/conversionOptions.js @@ -602,7 +602,9 @@ const conversionOptions = (node, info, colorScheme) => { metaButtons.forEach(m => { if(m.id == `opt-saveMusicData` && !auth.spotify) { - setupDisabledButtonWithReason(m, `Spotify not set up!`, `To save extra data such as the BPM / initial key of a song, ezytdl will need to cross reference it with Spotify. Please set up Spotify in the settings and try again!`) + m.setAttribute(`value`, `false`); + m.style.display = `none`; + //setupDisabledButtonWithReason(m, `Spotify not set up!`, `To save extra data such as the BPM / initial key of a song, ezytdl will need to cross reference it with Spotify. Please set up Spotify in the settings and try again!`) } else { const icon = m.querySelector(`#icon`); @@ -638,7 +640,11 @@ const conversionOptions = (node, info, colorScheme) => { easing: `easeOutExpo`, }) } - } + }; + + if(m.id == `opt-saveMusicData` && !(info.genres || info.genre)) { + while(m.getAttribute(`value`) != `false`) m.onclick(); + }; } }); @@ -731,7 +737,13 @@ const conversionOptions = (node, info, colorScheme) => { } } } else { - metaButtons.forEach(m => setupDisabledButtonWithReason(m, `FFmpeg not found!`, `FFmpeg is required to add metadata. Please install FFmpeg and try again.`)); + metaButtons.forEach(m => { + if(m.id == `opt-saveMusicData` && !auth.spotify) { + m.setAttribute(`value`, `false`); + m.style.display = `none`; + //setupDisabledButtonWithReason(m, `Spotify not set up!`, `To save extra data such as the BPM / initial key of a song, ezytdl will need to cross reference it with Spotify. Please set up Spotify in the settings and try again!`) + } else setupDisabledButtonWithReason(m, `FFmpeg not found!`, `FFmpeg is required to add metadata. Please install FFmpeg and try again.`) + }); console.log(node.querySelector(`#confirmDownload`).parentElement, node.querySelector(`#convertDownload`)) diff --git a/util/ytdlp.js b/util/ytdlp.js index 5cab6c8..8493c34 100644 --- a/util/ytdlp.js +++ b/util/ytdlp.js @@ -3236,7 +3236,7 @@ const ytdlpObj = { const originalTitle = thisInfo.media_metadata.general.title; const originalArtist = thisInfo.media_metadata.general.artist; - resultsInfo.entries = resultsInfo.entries.map(result => { + resultsInfo.entries = resultsInfo.entries?.map(result => { const { title, artist } = result.media_metadata.general; result.similarities = {}; @@ -3305,7 +3305,7 @@ const ytdlpObj = { return result; } else return undefined; // if this result doesn't have a duration, AND other results have durations, don't return it } else return undefined; - }); + }) || []; resultsInfo.entries = resultsInfo.entries.filter(a => a && a !== undefined && typeof a == `object` && typeof a.similarity == `number`).sort((a, b) => b.similarity - a.similarity); diff --git a/util/ytdlpUtil/qualitySorter.js b/util/ytdlpUtil/qualitySorter.js index 3b5e084..e4331a4 100644 --- a/util/ytdlpUtil/qualitySorter.js +++ b/util/ytdlpUtil/qualitySorter.js @@ -1,3 +1,5 @@ +const numRegex = /-?\d+(\.\d+)?/g; + const sorters = { // go by a.quality and b.quality, higher should go first // if there is no quality, send before the ones with quality @@ -19,6 +21,26 @@ const sorters = { }, filter: [`audio`, `video`] }, + formatID: { + func: (a,b) => { + const aID = Number(a.format_id?.match(numRegex)?.[0] || undefined); + const bID = Number(b.format_id?.match(numRegex)?.[0] || undefined); + if(aID || bID) { + if(aID == bID) { + return 0; + } else if(aID > bID) { + return -1; + } else if(aID < bID) { + return 1; + } else if(aID && !bID) { + return -1; + } else if(bID && !aID) { + return 1; + } else return 0; + } else return 0; + }, + filter: [`audio`, `video`] + }, videoQuality: { func: (a,b) => { if(a.width || a.height || b.width || b.height) {