-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·43 lines (38 loc) · 1.27 KB
/
index.js
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
#!/usr/bin/env node
const ffmpeg = require('fluent-ffmpeg');
const express = require('express');
const cors = require('cors');
const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || 3000;
const DEBUG = (process.env.DEBUG || 'false').toLowerCase() === 'true';
const app = express();
app.use(cors());
app.get('*', (req, res) => {
const { source: origImage, debug: showDebug = 'false' } = req.query || {};
if (origImage) {
const shouldShowDebug = DEBUG || showDebug.toLowerCase() === 'true';
let startTime;
if (shouldShowDebug) startTime = process.hrtime();
res.contentType('image/webp');
ffmpeg(origImage)
.toFormat('webp')
.outputOptions([
'-q:v 70',
'-lossless 0',
'-loop 0',
])
.on('end', err => {
if (shouldShowDebug) {
const end = process.hrtime(startTime);
console.info(`Finished converting ${origImage}`);
console.info('Execution time: %ds %dms', end[0], (end[1] / 1000000));
}
if (err) console.log(err);
})
.on('error', err => {
console.error('conversion error:\n', err.message)
})
.pipe(res, { end: true, });
}
});
app.listen(PORT, HOST, () => { console.log(`\nRunning on http://${HOST}:${PORT}\n`); });