1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const fs = require('fs'); const path = require('path');
const musicFolderPath = './source/music';
fs.readdir(musicFolderPath, (err, files) => { if (err) { console.error('无法读取文件夹:', err); return; }
const audioFiles = files.filter(file => { const ext = path.extname(file).toLowerCase(); return ext === '.mp3'; });
const playlist = audioFiles.map(file => { const pubilcMusicPath = '/music'; const match = file.match(/^(.+?) - (.+?)\.(mp3)$/); if (match) { const songName = match[1]; const artistName = match[2]; const fixPath = (filePath) => filePath.replace(/\\/g, '/'); return { name: songName, artist: artistName, url: fixPath(path.join(pubilcMusicPath, file)), cover: fixPath(path.join(pubilcMusicPath, `${songName}.png`)), lrc: fixPath(path.join(pubilcMusicPath, `${songName} - ${artistName}.lrc`)), }; } return { name: file, artist: '未知艺术家', url: path.join(pubilcMusicPath, file), cover: 'default-cover.png', lrc: '', }; });
const playlistJson = JSON.stringify(playlist, null, 2); fs.writeFileSync('./source/music/playlist.json', playlistJson, 'utf-8'); console.log('歌单生成完毕!'); });
|