Skip to content

Commit adbda45

Browse files
committed
🐞 fix: 修复下载歌曲元信息不正确导致无法正常播放 #113
1 parent 984d747 commit adbda45

File tree

7 files changed

+276
-78
lines changed

7 files changed

+276
-78
lines changed

electron/main/mainIpcMain.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ipcMain, dialog, app, clipboard, shell } from "electron";
2+
import { File, Picture, Id3v2Settings } from "node-taglib-sharp";
23
import { readDirAsync } from "@main/utils/readDirAsync";
34
import { parseFile } from "music-metadata";
45
import { download } from "electron-dl";
56
import getNeteaseMusicUrl from "@main/utils/getNeteaseMusicUrl";
6-
import NodeID3 from "node-id3";
77
import axios from "axios";
88
import fs from "fs/promises";
99

@@ -193,37 +193,42 @@ const mainIpcMain = (win) => {
193193
});
194194

195195
// 下载文件至指定目录
196-
ipcMain.handle("downloadFile", async (_, data, song, songName, songType, path) => {
196+
ipcMain.handle("downloadFile", async (_, songData, options) => {
197197
try {
198+
const { url, data, lyric, name, type } = JSON.parse(songData);
199+
const { path, downloadMeta, downloadCover, downloadLyrics } = JSON.parse(options);
198200
if (fs.access(path)) {
199-
const songData = JSON.parse(song);
200-
console.info("开始下载:", songData, data);
201+
console.info("开始下载:", name, url);
201202
// 下载歌曲
202-
const songDownload = await download(win, data.url, {
203+
const songDownload = await download(win, url, {
203204
directory: path,
204-
filename: `${songName}.${songType}`,
205+
filename: `${name}.${type}`,
205206
});
206-
// 若不为 mp3,则不进行元信息写入
207-
if (songType !== "mp3") return true;
207+
// 若关闭,则不进行元信息写入
208+
if (!downloadMeta) return true;
208209
// 下载封面
209-
const coverDownload = await download(win, songData.cover, {
210+
const coverDownload = await download(win, data.cover, {
210211
directory: path,
211-
filename: `${songName}.jpg`,
212+
filename: `${name}.jpg`,
212213
});
213-
// 生成歌曲文件的元数据
214-
const songTag = {
215-
title: songData.name,
216-
artist: Array.isArray(songData.artists)
217-
? songData.artists.map((ar) => ar.name).join(" / ")
218-
: songData.artists || "未知歌手",
219-
album: songData.album?.name || songData.album,
220-
image: coverDownload.getSavePath(),
221-
};
214+
// 读取歌曲文件
215+
const songFile = File.createFromPath(songDownload.getSavePath());
216+
// 生成图片信息
217+
const songCover = Picture.fromPath(coverDownload.getSavePath());
222218
// 保存修改后的元数据
223-
const isSuccess = NodeID3.write(songTag, songDownload.getSavePath());
219+
Id3v2Settings.forceDefaultVersion = true;
220+
Id3v2Settings.defaultVersion = 3;
221+
songFile.tag.title = data.name || "未知曲目";
222+
songFile.tag.album = data.album?.name || "未知专辑";
223+
songFile.tag.performers = data?.artists?.map((ar) => ar.name) || ["未知艺术家"];
224+
if (downloadLyrics) songFile.tag.lyrics = lyric;
225+
if (downloadCover) songFile.tag.pictures = [songCover];
226+
// 保存元信息
227+
songFile.save();
228+
songFile.dispose();
224229
// 删除封面
225230
await fs.unlink(coverDownload.getSavePath());
226-
return isSuccess;
231+
return true;
227232
} else {
228233
console.log(`目录不存在:${path}`);
229234
return false;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"js-cookie": "^3.0.5",
4242
"localforage": "^1.10.0",
4343
"music-metadata": "7.14.0",
44-
"node-id3": "^0.2.6",
44+
"node-taglib-sharp": "^5.2.3",
4545
"pinia": "^2.1.7",
4646
"pinia-plugin-persistedstate": "^3.2.1",
4747
"plyr": "^3.7.8",

0 commit comments

Comments
 (0)