works
This commit is contained in:
		
							parent
							
								
									777d60ab82
								
							
						
					
					
						commit
						d2022e4f79
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -1,2 +1,3 @@
 | 
			
		|||
node_modules
 | 
			
		||||
dist/
 | 
			
		||||
tmp/
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								assets/comic.ttf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/comic.ttf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								assets/spongebob.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/spongebob.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 268 KiB  | 
							
								
								
									
										64
									
								
								src/ffmpeg.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/ffmpeg.ts
									
									
									
									
									
										Normal 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);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/index.ts
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -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);
 | 
			
		||||
		Loading…
	
		Reference in a new issue