Set title/artist metadata in downloader

This commit is contained in:
TC
2021-01-14 23:01:26 +01:00
parent 25fd48697b
commit 2861473097
4 changed files with 36 additions and 0 deletions

View File

@ -1,6 +1,7 @@
const CHANNEL = "downloader"; const CHANNEL = "downloader";
const ACTIONS = { const ACTIONS = {
ERROR: "error", ERROR: "error",
METADATA: "metadata",
}; };
module.exports = { module.exports = {

View File

@ -2,6 +2,7 @@ const { join } = require("path");
const { dialog } = require("electron"); const { dialog } = require("electron");
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");
@ -16,14 +17,26 @@ const sendError = (win, err) => {
dialog.showMessageBox(dialogOpts); dialog.showMessageBox(dialogOpts);
}; };
let metadata = {};
function handle(win) { function handle(win) {
injectCSS(win.webContents, join(__dirname, "style.css")); injectCSS(win.webContents, join(__dirname, "style.css"));
const registerCallback = getSongInfo(win);
registerCallback((info) => {
metadata = {
...info,
image: info.image ? info.image.toDataURL() : undefined,
};
});
listenAction(CHANNEL, (event, action, error) => { listenAction(CHANNEL, (event, action, error) => {
switch (action) { switch (action) {
case ACTIONS.ERROR: case ACTIONS.ERROR:
sendError(win, error); sendError(win, error);
break; break;
case ACTIONS.METADATA:
event.returnValue = JSON.stringify(metadata);
break;
default: default:
console.log("Unknown action: " + action); console.log("Unknown action: " + action);
} }

View File

@ -12,6 +12,9 @@ const filenamify = require("filenamify");
const FFmpeg = require("@ffmpeg/ffmpeg/dist/ffmpeg.min"); const FFmpeg = require("@ffmpeg/ffmpeg/dist/ffmpeg.min");
const ytdl = require("ytdl-core"); const ytdl = require("ytdl-core");
const { triggerActionSync } = require("../utils");
const { ACTIONS, CHANNEL } = require("./actions.js");
const { createFFmpeg } = FFmpeg; const { createFFmpeg } = FFmpeg;
const ffmpeg = createFFmpeg({ const ffmpeg = createFFmpeg({
log: false, log: false,
@ -93,6 +96,7 @@ const toMP3 = async (
await ffmpeg.run( await ffmpeg.run(
"-i", "-i",
safeVideoName, safeVideoName,
...getFFmpegMetadataArgs(),
...(options.ffmpegArgs || []), ...(options.ffmpegArgs || []),
safeVideoName + "." + extension safeVideoName + "." + extension
); );
@ -112,6 +116,20 @@ const toMP3 = async (
} }
}; };
const getFFmpegMetadataArgs = () => {
const metadata = JSON.parse(triggerActionSync(CHANNEL, ACTIONS.METADATA));
if (!metadata) {
return;
}
return [
"-metadata",
`title=${metadata.title}`,
"-metadata",
`artist=${metadata.artist}`,
];
};
module.exports = { module.exports = {
downloadVideoToMP3, downloadVideoToMP3,
}; };

View File

@ -24,6 +24,10 @@ module.exports.triggerAction = (channel, action, ...args) => {
return ipcRenderer.send(channel, action, ...args); return ipcRenderer.send(channel, action, ...args);
}; };
module.exports.triggerActionSync = (channel, action, ...args) => {
return ipcRenderer.sendSync(channel, action, ...args);
};
module.exports.listenAction = (channel, callback) => { module.exports.listenAction = (channel, callback) => {
return ipcMain.on(channel, callback); return ipcMain.on(channel, callback);
}; };