fix metadata when downloading unplayed song

This commit is contained in:
Araxeus
2021-05-06 01:46:14 +03:00
parent d4811b7901
commit 844edbe2f4
3 changed files with 33 additions and 7 deletions

View File

@ -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);

View File

@ -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
);
};
// });

View File

@ -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) {