Добавлена поддержка потоковой передачи видео с сервера и синхронизация текущего времени воспроизведения

This commit is contained in:
kromskii2 committed 2025-01-05 12:07:15 +07:00
1 parent d931cd50da
commit 0b6b6e23da
3 files changed
+141 -86

No files matched your search

+35 -6
View File
@@ -1,19 +1,48 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;
// Middleware для обслуживания статических файлов из папки "src" и корня приложения
app.use('/src', express.static(path.join(__dirname, 'src')));
// Путь к видеофайлу
const videoPath = path.join(__dirname, 'movie.mp4'); // Замените 'movie.mp4' на ваш фильм
const serverStartTime = Date.now(); // Время запуска сервера
// Маршрут для главной страницы
app.use('/', express.static(path.join(__dirname, '')));
// Главная страница
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
// Маршрут для видео
app.get('/video', (req, res) => {
const range = req.headers.range;
if (!range) {
return res.status(400).send('Requires Range header');
}
const videoSize = fs.statSync(videoPath).size;
const CHUNK_SIZE = 10 ** 6; // 1MB
const start = Number(range.replace(/\D/g, ''));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
const videoStream = fs.createReadStream(videoPath, { start, end });
res.writeHead(206, {
'Content-Range': `bytes ${start}-${end}/${videoSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': end - start + 1,
'Content-Type': 'video/mp4',
});
videoStream.pipe(res);
});
// API для получения текущего времени воспроизведения
app.get('/current-time', (req, res) => {
const elapsedTime = (Date.now() - serverStartTime) / 1000; // Время с момента запуска сервера в секундах
res.json({ currentTime: elapsedTime });
});
// Запуск сервера
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://0.0.0.0:${PORT}`);
console.log(`Server is running on http://localhost:${PORT}`);
});