-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (86 loc) · 2.77 KB
/
server.js
File metadata and controls
106 lines (86 loc) · 2.77 KB
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
101
102
103
104
105
106
const express = require('express');
const fs = require('fs');
const path = require('path');
const morgan = require('morgan');
const NodeCache = require('node-cache');
const app = express();
const port = 3000;
const videoHistory = [];
const cache = new NodeCache({ stdTTL: 3600 });
const BITRATE = 128 * 1024 / 8;
const CHUNK_DURATION = 30;
const CHUNK_SIZE = BITRATE * CHUNK_DURATION;
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(morgan('combined'));
app.get('/videos', (req, res) => {
const videoDir = path.join(__dirname, 'videos');
fs.readdir(videoDir, (err, files) => {
if (err) {
return res.status(500).send('Unable to scan videos directory');
}
const videos = files.filter(file => file.endsWith('.mp4'));
res.json(videos);
});
});
app.get('/video/:name', (req, res) => {
const videoName = req.params.name;
const filePath = path.resolve(__dirname, 'videos', videoName);
const cachedVideo = cache.get(videoName);
if (cachedVideo) {
console.log('Serving from cache');
sendVideoStream(cachedVideo, req, res, { size: fs.statSync(cachedVideo).size });
return;
}
fs.stat(filePath, (err, stat) => {
if (err) {
return res.status(404).send('Video not found');
}
cache.set(videoName, filePath);
sendVideoStream(filePath, req, res, stat);
});
});
function sendVideoStream(filePath, req, res, stat) {
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = Math.min(start + CHUNK_SIZE - 1, fileSize - 1);
const chunksize = (end - start) + 1;
const file = fs.createReadStream(filePath, { start, end });
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
const chunkDuration = (chunksize / BITRATE).toFixed(2);
console.log(`Serving video chunk: ${filePath}, range: ${start}-${end}, duration: ${chunkDuration} seconds`);
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
console.log(`Serving full video: ${filePath}`);
res.writeHead(200, head);
fs.createReadStream(filePath).pipe(res);
}
}
app.post('/history', (req, res) => {
const { video } = req.body;
if (video) {
videoHistory.push(video);
res.status(200).send('Video added to history');
} else {
res.status(400).send('Invalid request');
}
});
app.get('/history', (req, res) => {
res.json(videoHistory);
});
app.listen(port, '0.0.0.0', () => {
console.log(`Server is running on http://0.0.0.0:${port}`);
});