const fs = require('fs'); const path = require('path'); const iconv = require('iconv-lite');//安装第三方库转换编码格式 const readline = require('readline'); const chalk = require('chalk') // var filename = path.join(__dirname, './../lyrics/晴天.lrc'); var filename = path.join(__dirname, './lyric.lrc'); var streamReader = fs.createReadStream(filename).pipe(iconv.decodeStream('utf8')) // gbk console.clear(); playMusic(); setTimeout(main, 2600); // 音频加载时间 function main() { // 利用readline读取 var rl = readline.createInterface({ input: streamReader }); var begin = new Date().getTime(); rl.on('line', (line) => { task(line, begin); }); } var displayLyric = [], index = 0; var regex = /\[(\d{2})\:(\d{2})\.(\d{2,3})\][ ]*(.+)[ ]*/; function task(line, begin) { var matches = regex.exec(line); if (matches) { var m = parseFloat(matches[1]); var s = parseFloat(matches[2]); var f = parseFloat(matches[3]); var lyric = matches[4]; displayLyric.push(lyric); var offset = new Date().getTime() - begin; setTimeout(() => { console.clear(); console.log(chalk.dim(displayLyric[index - 2] || "")); console.log(chalk.hex('#b3b3b3').visible(displayLyric[index - 1] || "")); console.log(chalk.hex('#FFFFFF').bold(displayLyric[index])); console.log(chalk.hex('#b3b3b3').visible(displayLyric[index + 1] || "")); console.log(chalk.dim(displayLyric[index + 2] || "")); index++; // console.log(lyric); }, m * 60 * 1000 + s * 1000 + f - offset); } else { // 不是一行歌词 // console.log("err", line); } } function playMusic() { var player = require('play-sound')(opts = {}) player.play('./music.m4a', function (err) { if (err) throw err; console.log("消愁 - 毛不易"); }); }