unix4artists/node/index.js

84 lines
1.5 KiB
JavaScript

'use strict';
const PORT = typeof process.env.PORT !== 'undefined' ? parseInt(process.env.PORT) : 3000;
const express = require('express');
const bodyParser = require('body-parser');
const { execSync } = require('child_process');
const app = express();
const HOME = `<html>
<head><title>FFMPEG demo</title></head>
<body>
<form method="POST">
<input name="title" />
<input type="submit" />
</form>
</body>
</html>`;
const VIDEO = `<html>
<head><title>FFMPEG demo</title></head>
<body>
<video controls autoplay>
<source src="/title.mp4" type="video/mp4" />
</video>
</body>
</html>`
function generateTitle (text) {
const title = `convert \
-size 720x480 \
-background black \
-fill white \
-gravity Center \
-weight 700 \
-pointsize 100 \
label:"${text}" \
title.png`;
const ffmpeg = `ffmpeg -y \
-loop 1 \
-framerate 24 \
-f image2 \
-i title.png \
-s 720x480 \
-vf "fade=t=in:st=0:d=5,fade=t=out:st=10:d=5" \
-c:v libx264 \
-preset slow \
-crf 22 \
-f mp4 \
-strict -2 \
-pix_fmt yuv420p \
-t 15 \
-an \
title.mp4`;
const cleanup = `rm title.png`;
execSync(title);
execSync(ffmpeg);
execSync(cleanup);
}
app.use(express.static('./'));
app.use(bodyParser.urlencoded({ extended : true }));
app.get('/', (req, res, next) => {
res.end(HOME);
return next();
});
app.post('/', (req, res, next) => {
generateTitle(req.body.title);
res.end(VIDEO);
return next();
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
execSync('rm -f title.mp4');
});