2021-12-23 14:58:24 +00:00
|
|
|
|
import { IAudioMetadata, parseBlob as metaParseBlob } from 'music-metadata-browser';
|
2021-12-21 22:34:18 +00:00
|
|
|
|
import iconv from 'iconv-lite';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
GetCoverFromFile,
|
|
|
|
|
GetImageFromURL,
|
|
|
|
|
GetMetaFromFile,
|
|
|
|
|
WriteMetaToFlac,
|
|
|
|
|
WriteMetaToMp3,
|
|
|
|
|
AudioMimeType,
|
2022-11-20 14:30:56 +00:00
|
|
|
|
split_regex,
|
2021-12-21 22:34:18 +00:00
|
|
|
|
} from '@/decrypt/utils';
|
2021-12-23 14:58:24 +00:00
|
|
|
|
import { getQMImageURLFromPMID, queryAlbumCover, querySongInfoById } from '@/utils/api';
|
2021-12-21 22:34:18 +00:00
|
|
|
|
|
2021-12-23 14:58:24 +00:00
|
|
|
|
interface MetaResult {
|
|
|
|
|
title: string;
|
|
|
|
|
artist: string;
|
|
|
|
|
album: string;
|
|
|
|
|
imgUrl: string;
|
|
|
|
|
blob: Blob;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param musicBlob 音乐文件(解密后)
|
|
|
|
|
* @param name 文件名
|
|
|
|
|
* @param ext 原始后缀名
|
|
|
|
|
* @param id 曲目 ID(<code>number</code>类型或纯数字组成的字符串)
|
|
|
|
|
* @returns Promise
|
|
|
|
|
*/
|
|
|
|
|
export async function extractQQMusicMeta(
|
|
|
|
|
musicBlob: Blob,
|
|
|
|
|
name: string,
|
|
|
|
|
ext: string,
|
|
|
|
|
id?: number | string,
|
|
|
|
|
): Promise<MetaResult> {
|
2021-12-21 22:34:18 +00:00
|
|
|
|
const musicMeta = await metaParseBlob(musicBlob);
|
|
|
|
|
for (let metaIdx in musicMeta.native) {
|
|
|
|
|
if (!musicMeta.native.hasOwnProperty(metaIdx)) continue;
|
|
|
|
|
if (musicMeta.native[metaIdx].some((item) => item.id === 'TCON' && item.value === '(12)')) {
|
|
|
|
|
console.warn('try using gbk encoding to decode meta');
|
2022-11-20 14:30:56 +00:00
|
|
|
|
musicMeta.common.artist = '';
|
2022-11-20 16:33:03 +00:00
|
|
|
|
if (musicMeta.common.artists) {
|
2022-11-20 14:30:56 +00:00
|
|
|
|
musicMeta.common.artist = iconv.decode(new Buffer(musicMeta.common.artist ?? ''), 'gbk');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
musicMeta.common.artists.forEach((artist) => artist = iconv.decode(new Buffer(artist ?? ''), 'gbk'));
|
|
|
|
|
musicMeta.common.artist = musicMeta.common.artists.toString();
|
|
|
|
|
}
|
2021-12-21 22:34:18 +00:00
|
|
|
|
musicMeta.common.title = iconv.decode(new Buffer(musicMeta.common.title ?? ''), 'gbk');
|
|
|
|
|
musicMeta.common.album = iconv.decode(new Buffer(musicMeta.common.album ?? ''), 'gbk');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-20 14:30:56 +00:00
|
|
|
|
if (id && id !== '0') {
|
2021-12-23 14:58:24 +00:00
|
|
|
|
try {
|
|
|
|
|
return fetchMetadataFromSongId(id, ext, musicMeta, musicBlob);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('在线获取曲目信息失败,回退到本地 meta 提取', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 22:34:18 +00:00
|
|
|
|
const info = GetMetaFromFile(name, musicMeta.common.title, musicMeta.common.artist);
|
2021-12-23 14:58:24 +00:00
|
|
|
|
info.artist = info.artist || '';
|
2021-12-21 22:34:18 +00:00
|
|
|
|
|
2021-12-23 14:58:24 +00:00
|
|
|
|
let imageURL = GetCoverFromFile(musicMeta);
|
|
|
|
|
if (!imageURL) {
|
|
|
|
|
imageURL = await getCoverImage(info.title, info.artist, musicMeta.common.album);
|
2021-12-21 22:34:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: info.title,
|
2022-11-20 16:33:03 +00:00
|
|
|
|
artist: info.artist,
|
2021-12-23 14:58:24 +00:00
|
|
|
|
album: musicMeta.common.album || '',
|
|
|
|
|
imgUrl: imageURL,
|
|
|
|
|
blob: await writeMetaToAudioFile({
|
|
|
|
|
title: info.title,
|
2022-11-20 14:30:56 +00:00
|
|
|
|
artists: info.artist.split(split_regex),
|
2021-12-23 14:58:24 +00:00
|
|
|
|
ext,
|
|
|
|
|
imageURL,
|
|
|
|
|
musicMeta,
|
|
|
|
|
blob: musicBlob,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchMetadataFromSongId(
|
|
|
|
|
id: number | string,
|
|
|
|
|
ext: string,
|
|
|
|
|
musicMeta: IAudioMetadata,
|
|
|
|
|
blob: Blob,
|
|
|
|
|
): Promise<MetaResult> {
|
|
|
|
|
const info = await querySongInfoById(id);
|
|
|
|
|
const imageURL = getQMImageURLFromPMID(info.track_info.album.pmid);
|
|
|
|
|
const artists = info.track_info.singer.map((singer) => singer.name);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: info.track_info.title,
|
2022-11-20 14:30:56 +00:00
|
|
|
|
artist: artists.join(','),
|
2021-12-23 14:58:24 +00:00
|
|
|
|
album: info.track_info.album.name,
|
|
|
|
|
imgUrl: imageURL,
|
|
|
|
|
|
|
|
|
|
blob: await writeMetaToAudioFile({
|
|
|
|
|
title: info.track_info.title,
|
|
|
|
|
artists,
|
|
|
|
|
ext,
|
|
|
|
|
imageURL,
|
|
|
|
|
musicMeta,
|
|
|
|
|
blob,
|
|
|
|
|
}),
|
2021-12-21 22:34:18 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getCoverImage(title: string, artist?: string, album?: string): Promise<string> {
|
|
|
|
|
try {
|
|
|
|
|
const data = await queryAlbumCover(title, artist, album);
|
2021-12-23 14:58:24 +00:00
|
|
|
|
return getQMImageURLFromPMID(data.Id, data.Type);
|
2021-12-21 22:34:18 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn(e);
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2021-12-23 14:58:24 +00:00
|
|
|
|
|
|
|
|
|
interface NewAudioMeta {
|
|
|
|
|
title: string;
|
|
|
|
|
artists: string[];
|
|
|
|
|
ext: string;
|
|
|
|
|
|
|
|
|
|
musicMeta: IAudioMetadata;
|
|
|
|
|
|
|
|
|
|
blob: Blob;
|
|
|
|
|
imageURL: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function writeMetaToAudioFile(info: NewAudioMeta): Promise<Blob> {
|
|
|
|
|
try {
|
|
|
|
|
const imageInfo = await GetImageFromURL(info.imageURL);
|
|
|
|
|
if (!imageInfo) {
|
|
|
|
|
console.warn('获取图像失败');
|
|
|
|
|
}
|
|
|
|
|
const newMeta = { picture: imageInfo?.buffer, title: info.title, artists: info.artists };
|
|
|
|
|
const buffer = Buffer.from(await info.blob.arrayBuffer());
|
|
|
|
|
const mime = AudioMimeType[info.ext] || AudioMimeType.mp3;
|
|
|
|
|
if (info.ext === 'mp3') {
|
|
|
|
|
return new Blob([WriteMetaToMp3(buffer, newMeta, info.musicMeta)], { type: mime });
|
|
|
|
|
} else if (info.ext === 'flac') {
|
|
|
|
|
return new Blob([WriteMetaToFlac(buffer, newMeta, info.musicMeta)], { type: mime });
|
|
|
|
|
} else {
|
|
|
|
|
console.info('writing metadata for ' + info.ext + ' is not being supported for now');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Error while appending cover image to file ' + e);
|
|
|
|
|
}
|
|
|
|
|
return info.blob;
|
|
|
|
|
}
|