mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 19:31:46 +00:00
fix metadata when downloading unplayed song
This commit is contained in:
@ -7,6 +7,7 @@ const { dialog, ipcMain } = require("electron");
|
|||||||
const getSongInfo = require("../../providers/song-info");
|
const getSongInfo = require("../../providers/song-info");
|
||||||
const { injectCSS, listenAction } = require("../utils");
|
const { injectCSS, listenAction } = require("../utils");
|
||||||
const { ACTIONS, CHANNEL } = require("./actions.js");
|
const { ACTIONS, CHANNEL } = require("./actions.js");
|
||||||
|
const { getImage } = require("../../providers/song-info");
|
||||||
|
|
||||||
const sendError = (win, err) => {
|
const sendError = (win, err) => {
|
||||||
const dialogOpts = {
|
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;
|
let fileBuffer = songBuffer;
|
||||||
const songMetadata = { ...metadata, ...currentMetadata };
|
const songMetadata = { ...metadata, ...currentMetadata };
|
||||||
|
if (!songMetadata.image && songMetadata.imageSrc) {
|
||||||
|
songMetadata.image = await getImage(songMetadata.imageSrc)
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const coverBuffer = songMetadata.image.toPNG();
|
const coverBuffer = songMetadata.image.toPNG();
|
||||||
const writer = new ID3Writer(songBuffer);
|
const writer = new ID3Writer(songBuffer);
|
||||||
|
|||||||
@ -38,13 +38,18 @@ const baseUrl = defaultConfig.url;
|
|||||||
// contextBridge.exposeInMainWorld("downloader", {
|
// contextBridge.exposeInMainWorld("downloader", {
|
||||||
// download: () => {
|
// download: () => {
|
||||||
global.download = () => {
|
global.download = () => {
|
||||||
|
let metadata;
|
||||||
let videoUrl = getSongMenu()
|
let videoUrl = getSongMenu()
|
||||||
.querySelector("ytmusic-menu-navigation-item-renderer")
|
.querySelector("ytmusic-menu-navigation-item-renderer")
|
||||||
.querySelector("#navigation-endpoint")
|
.querySelector("#navigation-endpoint")
|
||||||
.getAttribute("href");
|
.getAttribute("href");
|
||||||
videoUrl = !videoUrl
|
if (videoUrl) {
|
||||||
? global.songInfo.url || window.location.href
|
videoUrl = baseUrl + "/" + videoUrl;
|
||||||
: baseUrl + "/" + videoUrl;
|
metadata = null;
|
||||||
|
} else {
|
||||||
|
videoUrl = global.songInfo.url || window.location.href;
|
||||||
|
metadata = global.songInfo;
|
||||||
|
}
|
||||||
|
|
||||||
downloadVideoToMP3(
|
downloadVideoToMP3(
|
||||||
videoUrl,
|
videoUrl,
|
||||||
@ -61,7 +66,7 @@ global.download = () => {
|
|||||||
},
|
},
|
||||||
reinit,
|
reinit,
|
||||||
pluginOptions,
|
pluginOptions,
|
||||||
global.songInfo
|
metadata
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
// });
|
// });
|
||||||
|
|||||||
@ -24,7 +24,14 @@ const ffmpeg = createFFmpeg({
|
|||||||
});
|
});
|
||||||
const ffmpegMutex = new Mutex();
|
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,
|
videoUrl,
|
||||||
sendFeedback,
|
sendFeedback,
|
||||||
sendError,
|
sendError,
|
||||||
@ -35,6 +42,16 @@ const downloadVideoToMP3 = (
|
|||||||
) => {
|
) => {
|
||||||
sendFeedback("Downloading…");
|
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 videoName = "YouTube Music - Unknown title";
|
||||||
let videoReadableStream;
|
let videoReadableStream;
|
||||||
try {
|
try {
|
||||||
@ -135,6 +152,7 @@ const toMP3 = async (
|
|||||||
ipcRenderer.send("add-metadata", filePath, fileBuffer, {
|
ipcRenderer.send("add-metadata", filePath, fileBuffer, {
|
||||||
artist: metadata.artist,
|
artist: metadata.artist,
|
||||||
title: metadata.title,
|
title: metadata.title,
|
||||||
|
imageSrc: metadata.imageSrc || ""
|
||||||
});
|
});
|
||||||
ipcRenderer.once("add-metadata-done", reinit);
|
ipcRenderer.once("add-metadata-done", reinit);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user