2025-10-25 23:43:54 +08:00
|
|
|
|
import { createHash } from "crypto";
|
|
|
|
|
|
import { readFile } from "fs/promises";
|
2024-09-26 11:57:23 +08:00
|
|
|
|
|
2025-10-25 23:43:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 生成文件唯一ID
|
|
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
|
* @returns 唯一ID
|
|
|
|
|
|
*/
|
2024-09-26 11:57:23 +08:00
|
|
|
|
export const getFileID = (filePath: string): number => {
|
|
|
|
|
|
// SHA-256
|
2025-10-25 23:43:54 +08:00
|
|
|
|
const hash = createHash("sha256");
|
2024-09-26 11:57:23 +08:00
|
|
|
|
hash.update(filePath);
|
|
|
|
|
|
const digest = hash.digest("hex");
|
|
|
|
|
|
// 将哈希值的前 16 位转换为十进制数字
|
|
|
|
|
|
const uniqueId = parseInt(digest.substring(0, 16), 16);
|
|
|
|
|
|
return Number(uniqueId.toString().padStart(16, "0"));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-25 23:43:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 生成文件 MD5
|
|
|
|
|
|
* @param path 文件路径
|
|
|
|
|
|
* @returns MD5值
|
|
|
|
|
|
*/
|
2024-09-26 11:57:23 +08:00
|
|
|
|
export const getFileMD5 = async (path: string): Promise<string> => {
|
2025-10-25 23:43:54 +08:00
|
|
|
|
const data = await readFile(path);
|
|
|
|
|
|
const hash = createHash("md5");
|
2024-09-26 11:57:23 +08:00
|
|
|
|
hash.update(data);
|
|
|
|
|
|
return hash.digest("hex");
|
|
|
|
|
|
};
|
2025-10-19 01:36:13 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 将 music-metadata 库中的歌词数组转换为LRC格式字符串
|
|
|
|
|
|
* @param lyrics 歌词数组,每个元素包含时间戳(毫秒)和歌词文本
|
|
|
|
|
|
* @returns LRC格式的字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const metaDataLyricsArrayToLrc = (
|
|
|
|
|
|
lyrics: {
|
|
|
|
|
|
text: string;
|
|
|
|
|
|
timestamp?: number;
|
|
|
|
|
|
}[],
|
|
|
|
|
|
): string => {
|
|
|
|
|
|
return lyrics
|
|
|
|
|
|
.map(({ timestamp, text }) => {
|
|
|
|
|
|
if (!timestamp) return "";
|
|
|
|
|
|
const totalSeconds = Math.floor(timestamp / 1000);
|
|
|
|
|
|
const minutes = Math.floor(totalSeconds / 60);
|
|
|
|
|
|
const seconds = totalSeconds % 60;
|
|
|
|
|
|
const centiseconds = Math.floor((timestamp % 1000) / 10);
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化为两位数字
|
|
|
|
|
|
const mm = String(minutes).padStart(2, "0");
|
|
|
|
|
|
const ss = String(seconds).padStart(2, "0");
|
|
|
|
|
|
const cs = String(centiseconds).padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
|
|
return `[${mm}:${ss}.${cs}]${text}`;
|
|
|
|
|
|
})
|
|
|
|
|
|
.join("\n");
|
|
|
|
|
|
};
|