-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
100 lines (76 loc) · 2.45 KB
/
server.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Require the framework and instantiate it
const debug = require('debug')('ffmpegstreamer:server');
const debugStderr = require('debug')('ffmpegstreamerstderr:server');
const debugErr = require('debug')('ffmpegstreamer:server:error');
const express = require('express');
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const fsPromises = fs.promises;
const app = express();
const port = process.env.PORT || 3000;
app.use(async (req, res) => {
const pathParsed = path.parse(req.path);
if (pathParsed.ext !== '.mp4') {
debugErr('Not .mp4');
debugErr('ParsedPath: %o', pathParsed);
return res.sendStatus(404);
}
const { start, end } = req.query;
let duration = false;
if (start && end) {
duration = end - start;
}
const dirName = dir => dir === '/' ? dir : dir + '/';
const dir = './data' + dirName(pathParsed.dir);
const fullPath = `${dir}${pathParsed.base}`;
try {
await fsPromises.access(fullPath, fs.constants.R_OK);
} catch (error) {
debugErr('Could not find file: %s', fullPath);
debugErr('ParsedPath: %o', pathParsed);
return res.sendStatus(404);
}
// Build ffmpeg command
const opts = [];
if (start) opts.push('-ss', parseInt(start));
opts.push('-i', fullPath);
opts.push('-movflags', 'frag_keyframe+empty_moov+delay_moov');
if (duration) opts.push('-t', duration);
const finalOpts = [
...opts,
'-codec', 'copy',
'-avoid_negative_ts', '1',
'-f', 'mp4',
'-'
];
const ffmpeg = spawn('ffmpeg', finalOpts);
ffmpeg.on('exit', () => res.end());
ffmpeg.stderr.on('data', function (data) {
debugStderr('stderr: %s', data);
});
res.setHeader('Content-Type', 'video/mp4');
debug('Started to stream file "%s" Start: %s, End: %s', fullPath, start, end);
ffmpeg.stdout.pipe(res);
})
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
app.on('error', onError);
app.listen(port, () => debug(`Ffmpeg streamer app listening at http://${process.env.HOSTNAME || 'localhost'}:${port}`))