master
janderedev 2021-08-25 21:23:28 +02:00
parent 777d60ab82
commit d2022e4f79
Signed by: Lea
GPG Key ID: 5D5E18ACB990F57A
6 changed files with 74 additions and 10 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
dist/
tmp/

BIN
assets/comic.ttf Normal file

Binary file not shown.

BIN
assets/spongebob.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

BIN
out.mp4 Normal file

Binary file not shown.

64
src/ffmpeg.ts Normal file
View File

@ -0,0 +1,64 @@
import FFmpeg from 'fluent-ffmpeg';
import path from 'path';
import fs from 'fs';
import EventEmitter from 'events';
let inProgress: Array<string> = [];
let funnyListener = new EventEmitter();
export default function(ip: string): Promise<string> {
return new Promise((resolve, reject) => {
let outPath = path.join(__dirname, '..', 'tmp', ip + '.mp4');
if (inProgress.indexOf(ip) > -1) {
console.log('Sending video for ' + ip + ' (awaiting)');
funnyListener.on(ip, () => {
resolve(outPath);
});
} else {
if (fs.existsSync(outPath)) {
console.log('Sending video for ' + ip);
resolve(outPath);
return;
}
console.log('Generating video for: ' + ip);
inProgress.push(ip);
let command = FFmpeg();
command
.input(path.join(__dirname, '..', 'assets', 'spongebob.png'))
.loop(1)
.input(path.join(__dirname, '..', 'assets', 'coconut.wav'))
.videoFilter(
"drawtext=fontfile=assets/comic.ttf:"
+ `text='${ip.replace(new RegExp(':', 'g'), '\\:')}':`
+ "fontcolor=white:"
+ `fontsize=${ip.length > 15 ? '80' : '150'}:`
+ "box=1:"
+ "boxcolor=black@0.3:"
+ "boxborderw=5:"
+ "x=(w-text_w)/2:y=(h-text_h)/2:"
+ "enable='gte(t,8)'")
.addOption('-t 0:21')
.addOption('-tune stillimage')
.addOption('-pix_fmt yuv420p')
.addOption('-shortest')
.addOption('-preset veryfast')
.videoCodec('libx264')
.audioCodec('aac')
.output(outPath);
command.run();
command.on('end', () => {
funnyListener.emit(ip);
funnyListener.removeAllListeners(ip);
inProgress.splice(inProgress.indexOf(ip), 1);
resolve(outPath);
});
}
});
}

View File

@ -1,18 +1,17 @@
import Express from 'express';
import ffmpeg from 'fluent-ffmpeg';
import fs from 'fs';
import path from 'path';
import makeVideo from './ffmpeg';
let p = path.join(__dirname, '..', 'tmp');
if (!fs.existsSync(p)) fs.mkdirSync(p);
const app = Express();
let h = 0;
app.get('/', (req, res) => {
h++;
res.send('aaa: ' + h);
});
app.get('/video', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'assets', 'coconut.wav'));
app.get('/', async (req, res) => {
let ip = req.header('X-Forwarded-For')?.split(', ')[0] || req.ip;
let filePath = await makeVideo(ip);
res.sendFile(filePath);
});
app.listen(6969);