mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-23 15:47:23 +08:00
✨ feat: 歌曲右键菜单添加下载歌词功能及下载设置中保存歌词文件选项
- 右键菜单新增"下载歌词"选项,支持获取歌词并保存为 .lrc 文件 - 如有翻译歌词会自动合并到 LRC 文件中 - 下载设置面板新增"单独保存歌词文件"开关 - 开启后下载歌曲时自动在同目录生成同名 .lrc 歌词文件 - 主进程新增 save-lyric-file IPC handler - 完成 5 种语言的国际化翻译
This commit is contained in:
@@ -246,6 +246,33 @@ export function initializeFileManager() {
|
||||
};
|
||||
});
|
||||
|
||||
// 保存歌词文件
|
||||
ipcMain.handle(
|
||||
'save-lyric-file',
|
||||
async (_, { filename, lrcContent }: { filename: string; lrcContent: string }) => {
|
||||
try {
|
||||
const configStore = getStore();
|
||||
const downloadPath =
|
||||
(configStore.get('set.downloadPath') as string) || app.getPath('downloads');
|
||||
const sanitizedName = sanitizeFilename(filename);
|
||||
let filePath = path.join(downloadPath, `${sanitizedName}.lrc`);
|
||||
|
||||
// 文件已存在时添加序号
|
||||
let counter = 1;
|
||||
while (fs.existsSync(filePath)) {
|
||||
filePath = path.join(downloadPath, `${sanitizedName} (${counter}).lrc`);
|
||||
counter++;
|
||||
}
|
||||
|
||||
await fs.promises.writeFile(filePath, lrcContent, 'utf-8');
|
||||
return { success: true, path: filePath };
|
||||
} catch (error: any) {
|
||||
console.error('保存歌词文件失败:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 添加清除下载历史的处理函数
|
||||
ipcMain.on('clear-downloads-history', () => {
|
||||
downloadStore.set('history', []);
|
||||
@@ -786,6 +813,17 @@ async function downloadMusic(
|
||||
}
|
||||
}
|
||||
|
||||
// 如果启用了单独保存歌词文件,将歌词保存为 .lrc 文件
|
||||
if (lyricsContent && configStore.get('set.downloadSaveLyric')) {
|
||||
try {
|
||||
const lrcFilePath = finalFilePath.replace(/\.[^.]+$/, '.lrc');
|
||||
await fs.promises.writeFile(lrcFilePath, lyricsContent, 'utf-8');
|
||||
console.log('歌词文件已保存:', lrcFilePath);
|
||||
} catch (lrcError) {
|
||||
console.error('保存歌词文件失败:', lrcError);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存下载信息
|
||||
try {
|
||||
const songInfos = configStore.get('downloadedSongs', {}) as Record<string, any>;
|
||||
|
||||
Reference in New Issue
Block a user