mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-14 03:41:46 +00:00
add trackId to album downloads
This commit is contained in:
@ -24,8 +24,9 @@ module.exports = async (options_) => {
|
|||||||
ipcMain.handle("download-song", (_, url) => downloadSong(url));
|
ipcMain.handle("download-song", (_, url) => downloadSong(url));
|
||||||
};
|
};
|
||||||
|
|
||||||
async function downloadSong(url, playlistFolder = undefined) {
|
async function downloadSong(url, playlistFolder = undefined, trackId = undefined) {
|
||||||
const metadata = await getMetadata(url);
|
const metadata = await getMetadata(url);
|
||||||
|
metadata.trackId = trackId;
|
||||||
|
|
||||||
const stream = await yt.download(metadata.id, {
|
const stream = await yt.download(metadata.id, {
|
||||||
type: 'audio', // audio, video or video+audio
|
type: 'audio', // audio, video or video+audio
|
||||||
@ -53,7 +54,7 @@ async function downloadSong(url, playlistFolder = undefined) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!presets[options.preset]) {
|
if (!presets[options.preset]) {
|
||||||
const fileBuffer = await toMP3(iterableStream, filePath, metadata);
|
const fileBuffer = await toMP3(iterableStream, metadata);
|
||||||
console.info('writing id3 tags...'); // DELETE
|
console.info('writing id3 tags...'); // DELETE
|
||||||
writeFileSync(filePath, await writeID3(fileBuffer, metadata));
|
writeFileSync(filePath, await writeID3(fileBuffer, metadata));
|
||||||
console.info('done writing id3 tags!'); // DELETE
|
console.info('done writing id3 tags!'); // DELETE
|
||||||
@ -78,7 +79,6 @@ function getIdFromUrl(url) {
|
|||||||
async function getMetadata(url) {
|
async function getMetadata(url) {
|
||||||
const id = getIdFromUrl(url);
|
const id = getIdFromUrl(url);
|
||||||
const info = await yt.music.getInfo(id);
|
const info = await yt.music.getInfo(id);
|
||||||
//console.log('got info:' + JSON.stringify(info, null, 2)); // DELETE
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: info.basic_info.id,
|
id: info.basic_info.id,
|
||||||
@ -120,6 +120,9 @@ async function writeID3(buffer, metadata) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (metadata.trackId) {
|
||||||
|
writer.setFrame("TRCK", metadata.trackId);
|
||||||
|
}
|
||||||
writer.addTag();
|
writer.addTag();
|
||||||
return Buffer.from(writer.arrayBuffer);
|
return Buffer.from(writer.arrayBuffer);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -138,7 +141,7 @@ const ffmpeg = require("@ffmpeg/ffmpeg").createFFmpeg({
|
|||||||
|
|
||||||
const ffmpegMutex = new Mutex();
|
const ffmpegMutex = new Mutex();
|
||||||
|
|
||||||
async function toMP3(stream, filePath, metadata, extension = "mp3") {
|
async function toMP3(stream, metadata, extension = "mp3") {
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
@ -165,13 +168,9 @@ async function toMP3(stream, filePath, metadata, extension = "mp3") {
|
|||||||
safeVideoName + "." + extension
|
safeVideoName + "." + extension
|
||||||
);
|
);
|
||||||
|
|
||||||
const fileBuffer = ffmpeg.FS("readFile", safeVideoName + "." + extension);
|
|
||||||
|
|
||||||
await writeID3(fileBuffer, metadata);
|
|
||||||
|
|
||||||
// sendFeedback("Saving…");
|
// sendFeedback("Saving…");
|
||||||
|
|
||||||
return fileBuffer;
|
return ffmpeg.FS("readFile", safeVideoName + "." + extension);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sendError(e);
|
sendError(e);
|
||||||
} finally {
|
} finally {
|
||||||
@ -210,5 +209,6 @@ function getFFmpegMetadataArgs(metadata) {
|
|||||||
...(metadata.title ? ["-metadata", `title=${metadata.title}`] : []),
|
...(metadata.title ? ["-metadata", `title=${metadata.title}`] : []),
|
||||||
...(metadata.artist ? ["-metadata", `artist=${metadata.artist}`] : []),
|
...(metadata.artist ? ["-metadata", `artist=${metadata.artist}`] : []),
|
||||||
...(metadata.album ? ["-metadata", `album=${metadata.album}`] : []),
|
...(metadata.album ? ["-metadata", `album=${metadata.album}`] : []),
|
||||||
|
...(metadata.trackId ? ["-metadata", `track=${metadata.trackId}`] : []),
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|||||||
@ -96,6 +96,10 @@ async function downloadPlaylist(givenUrl, win, options) {
|
|||||||
sendError(e);
|
sendError(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let isAlbum = playlist.title.startsWith('Album - ');
|
||||||
|
if (isAlbum) {
|
||||||
|
playlist.title = playlist.title.slice(8);
|
||||||
|
}
|
||||||
const safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' });
|
const safePlaylistTitle = filenamify(playlist.title, { replacement: ' ' });
|
||||||
|
|
||||||
const folder = getFolder(options.downloadFolder);
|
const folder = getFolder(options.downloadFolder);
|
||||||
@ -143,9 +147,12 @@ async function downloadPlaylist(givenUrl, win, options) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let counter = 1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const song of playlist.items) {
|
for (const song of playlist.items) {
|
||||||
await downloadSong(song.url, playlistFolder).catch((e) => sendError(e));
|
const trackId = isAlbum ? counter++ : undefined;
|
||||||
|
await downloadSong(song.url, playlistFolder, trackId).catch((e) => sendError(e));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sendError(e);
|
sendError(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user