By Nodejs, 建议,拿对比度高的视频去制作,效果更好
视频转图片
1 2 3 4 5 6 7 8 9
| brew install ffmpeg
ffmpeg -h
ffmpeg -i ./assets/60f1bcc6321e9fda43c173c84b0391.mp4 -r 15 ./assets/images/%d.png
|
转化步骤
完整源码里已经标注步骤
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| const fs = require("fs"); const { join, extname } = require("path"); const { read, intToRGBA } = require("jimp"); const { echo, exit } = require("shelljs");
const floder = join(__dirname, "./assets/images/");
const charts = `@&$%=+!^*~- `;
const maxIndex = charts.length - 1; const limitIndex = (num) => (num > maxIndex ? maxIndex : num);
fs.readdir(floder, undefined, (err, data) => { if (err) throw err;
const files = data.filter((url) => extname(url) === ".png"); files.sort((a, b) => parseInt(a) - parseInt(b));
play(files); });
const play = (files) => { const printFileName = files.shift();
print(printFileName);
if (files.length) { setTimeout(() => { play(files); }, parseInt(1000 / 15));
return; } exit(1); };
const print = (fileName) => { let subCharts = "";
const fileURL = join(floder, fileName);
read(fileURL).then((image) => { const width = image.getWidth(); const height = image.getHeight();
const xScale = width / 220; const yScale = height / 40;
for (let y = 0; y < height; y += yScale) { for (let x = 0; x < width; x += xScale) { const { r, g, b } = intToRGBA(image.getPixelColor(x, y));
const gray = (0.3 * r + 0.59 * g + 0.11 * b).toFixed(2);
const grayScale = parseFloat(gray / 255).toFixed(2); const grayDegree = parseInt(grayScale * charts.length); const index = Math.floor(limitIndex(grayDegree + 0.5));
subCharts += charts[index]; } subCharts += "\n"; }
echo(subCharts); }); };
|
素材链接
感谢 大佬 提供 go 版本的教程