Merge pull request #245 from th-ch/fix-downloader-metadata

Fix downloader metadata
This commit is contained in:
th-ch
2021-04-29 23:05:17 +02:00
committed by GitHub
3 changed files with 39 additions and 27 deletions

View File

@ -1,6 +1,8 @@
const { writeFileSync } = require("fs");
const { join } = require("path"); const { join } = require("path");
const { dialog } = require("electron"); const ID3Writer = require("browser-id3-writer");
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");
@ -38,6 +40,34 @@ function handle(win) {
console.log("Unknown action: " + action); console.log("Unknown action: " + action);
} }
}); });
ipcMain.on("add-metadata", (event, filePath, songBuffer, currentMetadata) => {
let fileBuffer = songBuffer;
const songMetadata = { ...metadata, ...currentMetadata };
try {
const coverBuffer = songMetadata.image.toPNG();
const writer = new ID3Writer(songBuffer);
// Create the metadata tags
writer
.setFrame("TIT2", songMetadata.title)
.setFrame("TPE1", [songMetadata.artist])
.setFrame("APIC", {
type: 3,
data: coverBuffer,
description: "",
});
writer.addTag();
fileBuffer = Buffer.from(writer.arrayBuffer);
} catch (error) {
sendError(win, error);
}
writeFileSync(filePath, fileBuffer);
// Notify the youtube-dl file
event.reply("add-metadata-done");
});
} }
module.exports = handle; module.exports = handle;

View File

@ -44,7 +44,7 @@ global.download = () => {
.getAttribute("href"); .getAttribute("href");
videoUrl = !videoUrl videoUrl = !videoUrl
? global.songInfo.url || window.location.href ? global.songInfo.url || window.location.href
: baseUrl + videoUrl; : baseUrl + "/" + videoUrl;
downloadVideoToMP3( downloadVideoToMP3(
videoUrl, videoUrl,

View File

@ -1,9 +1,7 @@
const { randomBytes } = require("crypto"); const { randomBytes } = require("crypto");
const { writeFileSync } = require("fs");
const { join } = require("path"); const { join } = require("path");
const Mutex = require("async-mutex").Mutex; const Mutex = require("async-mutex").Mutex;
const ID3Writer = require("browser-id3-writer");
const { ipcRenderer } = require("electron"); const { ipcRenderer } = require("electron");
const is = require("electron-is"); const is = require("electron-is");
const filenamify = require("filenamify"); const filenamify = require("filenamify");
@ -126,35 +124,19 @@ const toMP3 = async (
: videoName; : videoName;
const filename = filenamify(name + "." + extension, { const filename = filenamify(name + "." + extension, {
replacement: "_", replacement: "_",
maxLength: 255,
}); });
const filePath = join(folder, subfolder, filename); const filePath = join(folder, subfolder, filename);
const fileBuffer = ffmpeg.FS("readFile", safeVideoName + "." + extension); const fileBuffer = ffmpeg.FS("readFile", safeVideoName + "." + extension);
// Add the metadata // Add the metadata
try { sendFeedback("Adding metadata…");
const writer = new ID3Writer(fileBuffer); ipcRenderer.send("add-metadata", filePath, fileBuffer, {
if (metadata.image) { artist: metadata.artist,
const coverBuffer = metadata.image.toPNG(); title: metadata.title,
// Create the metadata tags
writer
.setFrame("TIT2", metadata.title)
.setFrame("TPE1", [metadata.artist])
.setFrame("APIC", {
type: 3,
data: coverBuffer,
description: "",
}); });
writer.addTag(); ipcRenderer.once("add-metadata-done", reinit);
}
writeFileSync(filePath, Buffer.from(writer.arrayBuffer));
} catch (error) {
sendError(error);
} finally {
reinit();
}
} catch (e) { } catch (e) {
sendError(e); sendError(e);
} finally { } finally {