1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

添加字幕滚动代码(lyric)

This commit is contained in:
程序员小墨 2022-11-15 14:03:10 +08:00
parent 08c7313ce0
commit 67af823cde
5 changed files with 156 additions and 4 deletions

4
lyric/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.lrc
*.m4a
*.mp3
*.flac

64
lyric/index.js Normal file
View File

@ -0,0 +1,64 @@
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("消愁 - 毛不易");
});
}

View File

@ -43,6 +43,7 @@ async function test() {
// let res = await songInfoUtils.fetch({ songId: "437608327" });
// let res = await playlistUtilsOld.fetch({ playlistId: "4980157066", debug: true });
// let res = await commentUtils.fetch({ songId: "4980157066" });
// let res = await lyricInfoUtils.fetch({ songId: "569200213" });
// let res = await testUtils.fetch();
@ -50,7 +51,7 @@ async function test() {
// let res = await artistInfoUtils.getFromDatabase({ artistId: "12023508" });
// let res = await songInfoUtils.getFromDatabase({ songId: "437608327" });
console.log(res);
// console.log(res);
}
/**

81
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"chalk": "4.1.2",
"cheerio": "^1.0.0-rc.12",
"crypto": "^1.0.1",
"dotenv": "^16.0.1",
@ -19,6 +20,7 @@
"NeteaseCloudMusicApi": "^4.8.2",
"node-schedule": "^2.1.0",
"path": "^0.12.7",
"play-sound": "^1.1.5",
"request": "^2.88.2",
"solarlunar": "^2.0.7"
}
@ -283,6 +285,18 @@
"resolved": "https://registry.npmmirror.com/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha512-4tYFyifaFfGacoiObjJegolkwSU4xQNGbVgUiNYVUxbQ2x2lUsFvY4hVgVzGiIe6WLOPqycWXA40l+PWsxthUw=="
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmmirror.com/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
}
},
"node_modules/charenc": {
"version": "0.0.2",
"resolved": "https://registry.npmmirror.com/charenc/-/charenc-0.0.2.tgz",
@ -822,6 +836,11 @@
"node": ">= 0.8"
}
},
"node_modules/find-exec": {
"version": "1.0.2",
"resolved": "https://registry.npmmirror.com/find-exec/-/find-exec-1.0.2.tgz",
"integrity": "sha512-/cs763F5i7Nm/aG+hjBv2nJB/Bl2w0tnuZd5bbfcV+WYapn8T4ez3B87H6MgvsmdheDkitj/rHi557wvE+XylQ=="
},
"node_modules/find-up": {
"version": "4.1.0",
"resolved": "https://registry.npmmirror.com/find-up/-/find-up-4.1.0.tgz",
@ -1040,6 +1059,14 @@
"node": ">= 0.4.0"
}
},
"node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmmirror.com/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"engines": {
"node": ">=8"
}
},
"node_modules/has-property-descriptors": {
"version": "1.0.0",
"resolved": "https://registry.npmmirror.com/has-property-descriptors/-/has-property-descriptors-1.0.0.tgz",
@ -1689,6 +1716,14 @@
"resolved": "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow=="
},
"node_modules/play-sound": {
"version": "1.1.5",
"resolved": "https://registry.npmmirror.com/play-sound/-/play-sound-1.1.5.tgz",
"integrity": "sha512-gbdF1iLNyL5r9Ne9YwARGMkrvfR4EL9G1ZLtFPLkI2tQt0kkHw5CHM5E6Gl/lDuuk/Uj/O5Q29Bi08jMK4egbA==",
"dependencies": {
"find-exec": "1.0.2"
}
},
"node_modules/pngjs": {
"version": "5.0.0",
"resolved": "https://registry.npmmirror.com/pngjs/-/pngjs-5.0.0.tgz",
@ -2204,6 +2239,17 @@
"node": ">=10"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmmirror.com/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/toidentifier/-/toidentifier-1.0.1.tgz",
@ -2669,6 +2715,15 @@
"resolved": "https://registry.npmmirror.com/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha512-4tYFyifaFfGacoiObjJegolkwSU4xQNGbVgUiNYVUxbQ2x2lUsFvY4hVgVzGiIe6WLOPqycWXA40l+PWsxthUw=="
},
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmmirror.com/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
}
},
"charenc": {
"version": "0.0.2",
"resolved": "https://registry.npmmirror.com/charenc/-/charenc-0.0.2.tgz",
@ -3095,6 +3150,11 @@
"unpipe": "~1.0.0"
}
},
"find-exec": {
"version": "1.0.2",
"resolved": "https://registry.npmmirror.com/find-exec/-/find-exec-1.0.2.tgz",
"integrity": "sha512-/cs763F5i7Nm/aG+hjBv2nJB/Bl2w0tnuZd5bbfcV+WYapn8T4ez3B87H6MgvsmdheDkitj/rHi557wvE+XylQ=="
},
"find-up": {
"version": "4.1.0",
"resolved": "https://registry.npmmirror.com/find-up/-/find-up-4.1.0.tgz",
@ -3264,6 +3324,11 @@
"function-bind": "^1.1.1"
}
},
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmmirror.com/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
},
"has-property-descriptors": {
"version": "1.0.0",
"resolved": "https://registry.npmmirror.com/has-property-descriptors/-/has-property-descriptors-1.0.0.tgz",
@ -3774,6 +3839,14 @@
"resolved": "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow=="
},
"play-sound": {
"version": "1.1.5",
"resolved": "https://registry.npmmirror.com/play-sound/-/play-sound-1.1.5.tgz",
"integrity": "sha512-gbdF1iLNyL5r9Ne9YwARGMkrvfR4EL9G1ZLtFPLkI2tQt0kkHw5CHM5E6Gl/lDuuk/Uj/O5Q29Bi08jMK4egbA==",
"requires": {
"find-exec": "1.0.2"
}
},
"pngjs": {
"version": "5.0.0",
"resolved": "https://registry.npmmirror.com/pngjs/-/pngjs-5.0.0.tgz",
@ -4191,6 +4264,14 @@
"peek-readable": "^4.1.0"
}
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmmirror.com/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"requires": {
"has-flag": "^4.0.0"
}
},
"toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/toidentifier/-/toidentifier-1.0.1.tgz",

View File

@ -9,17 +9,19 @@
"author": "coder-xiaomo",
"license": "MIT",
"dependencies": {
"chalk": "4.1.2",
"cheerio": "^1.0.0-rc.12",
"crypto": "^1.0.1",
"dotenv": "^16.0.1",
"fs": "^0.0.1-security",
"iconv-lite": "^0.6.3",
"minimist": "^1.2.6",
"mysql": "^2.18.1",
"NeteaseCloudMusicApi": "^4.8.2",
"node-schedule": "^2.1.0",
"path": "^0.12.7",
"play-sound": "^1.1.5",
"request": "^2.88.2",
"solarlunar": "^2.0.7",
"dotenv": "^16.0.1",
"iconv-lite": "^0.6.3"
"solarlunar": "^2.0.7"
}
}