Update server.js
Browse files
server.js
CHANGED
|
@@ -14,9 +14,10 @@ app.use(express.static("public")); // Serve frontend
|
|
| 14 |
const hlsFolder = path.join(__dirname, "hls");
|
| 15 |
if (!fs.existsSync(hlsFolder)) fs.mkdirSync(hlsFolder);
|
| 16 |
|
| 17 |
-
const streams = {};
|
| 18 |
-
const timestamps = {};
|
| 19 |
-
const queues = {};
|
|
|
|
| 20 |
|
| 21 |
// Add a video to the queue
|
| 22 |
app.get("/add", (req, res) => {
|
|
@@ -28,6 +29,7 @@ app.get("/add", (req, res) => {
|
|
| 28 |
|
| 29 |
if (!queues[channel]) queues[channel] = [];
|
| 30 |
queues[channel].push(video);
|
|
|
|
| 31 |
|
| 32 |
console.log(`Added video to channel ${channel}: ${video}`);
|
| 33 |
|
|
@@ -39,16 +41,17 @@ app.get("/add", (req, res) => {
|
|
| 39 |
res.json({ message: "Video added to queue", queue: queues[channel] });
|
| 40 |
});
|
| 41 |
|
| 42 |
-
// Start streaming the next video in the queue
|
| 43 |
function startStream(channel) {
|
| 44 |
if (!queues[channel] || queues[channel].length === 0) {
|
| 45 |
console.log(`No more videos in queue for channel ${channel}`);
|
| 46 |
delete streams[channel];
|
| 47 |
delete timestamps[channel];
|
|
|
|
| 48 |
return;
|
| 49 |
}
|
| 50 |
|
| 51 |
-
// Stop
|
| 52 |
if (streams[channel]) {
|
| 53 |
console.log(`Stopping existing stream for channel ${channel}`);
|
| 54 |
streams[channel].kill("SIGKILL");
|
|
@@ -56,28 +59,52 @@ function startStream(channel) {
|
|
| 56 |
}
|
| 57 |
|
| 58 |
const videoUrl = queues[channel].shift(); // Get the next video
|
|
|
|
|
|
|
| 59 |
const outputFolder = path.join(hlsFolder, channel);
|
| 60 |
if (!fs.existsSync(outputFolder)) fs.mkdirSync(outputFolder, { recursive: true });
|
| 61 |
|
| 62 |
-
// Set start time
|
| 63 |
-
timestamps[channel] = Date.now();
|
| 64 |
|
| 65 |
-
console.log(`Starting live stream for ${channel} from ${videoUrl}`);
|
| 66 |
|
| 67 |
-
//
|
| 68 |
streams[channel] = ffmpeg(videoUrl)
|
| 69 |
.output(`${outputFolder}/index.m3u8`)
|
| 70 |
.addOptions([
|
| 71 |
"-c:v libx264",
|
| 72 |
"-preset veryfast",
|
| 73 |
"-crf 23",
|
| 74 |
-
"-b:v
|
|
|
|
|
|
|
| 75 |
"-c:a aac",
|
| 76 |
"-b:a 128k",
|
| 77 |
-
"-hls_time
|
| 78 |
-
"-hls_list_size
|
| 79 |
-
"-hls_flags delete_segments+append_list",
|
| 80 |
-
"-start_number 0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
])
|
| 82 |
.on("end", () => {
|
| 83 |
console.log(`Finished streaming for channel ${channel}`);
|
|
@@ -89,7 +116,7 @@ function startStream(channel) {
|
|
| 89 |
console.error(`FFmpeg error for channel ${channel}:`, err);
|
| 90 |
delete streams[channel];
|
| 91 |
delete timestamps[channel];
|
| 92 |
-
startStream(channel); // Try
|
| 93 |
})
|
| 94 |
.run();
|
| 95 |
}
|
|
@@ -107,28 +134,22 @@ app.get("/next", (req, res) => {
|
|
| 107 |
}
|
| 108 |
|
| 109 |
console.log(`Skipping to next video for channel ${channel}`);
|
| 110 |
-
streams[channel].kill("SIGKILL"); // Stop
|
| 111 |
delete streams[channel];
|
| 112 |
|
| 113 |
-
// Start next video
|
| 114 |
-
startStream(channel);
|
| 115 |
res.json({ message: "Skipped to next video" });
|
| 116 |
});
|
| 117 |
|
| 118 |
-
|
| 119 |
-
res.sendFile(path.join(__dirname, "public", "index.html"));
|
| 120 |
-
});
|
| 121 |
-
|
| 122 |
-
// Get a list of active channels
|
| 123 |
app.get("/channels", (req, res) => {
|
| 124 |
-
|
| 125 |
-
res.json({ channels: activeChannels });
|
| 126 |
});
|
| 127 |
|
| 128 |
// Serve HLS files
|
| 129 |
app.use("/hls", express.static(hlsFolder));
|
| 130 |
|
| 131 |
-
// Get
|
| 132 |
app.get("/timestamp", (req, res) => {
|
| 133 |
const { channel } = req.query;
|
| 134 |
|
|
@@ -140,4 +161,4 @@ app.get("/timestamp", (req, res) => {
|
|
| 140 |
res.json({ elapsed });
|
| 141 |
});
|
| 142 |
|
| 143 |
-
app.listen(PORT, () => console.log(`
|
|
|
|
| 14 |
const hlsFolder = path.join(__dirname, "hls");
|
| 15 |
if (!fs.existsSync(hlsFolder)) fs.mkdirSync(hlsFolder);
|
| 16 |
|
| 17 |
+
const streams = {}; // Active FFmpeg processes per channel
|
| 18 |
+
const timestamps = {}; // Track when streams started
|
| 19 |
+
const queues = {}; // Video queue per channel
|
| 20 |
+
const activeChannels = new Set(); // Store active channels
|
| 21 |
|
| 22 |
// Add a video to the queue
|
| 23 |
app.get("/add", (req, res) => {
|
|
|
|
| 29 |
|
| 30 |
if (!queues[channel]) queues[channel] = [];
|
| 31 |
queues[channel].push(video);
|
| 32 |
+
activeChannels.add(channel); // Keep track of active channels
|
| 33 |
|
| 34 |
console.log(`Added video to channel ${channel}: ${video}`);
|
| 35 |
|
|
|
|
| 41 |
res.json({ message: "Video added to queue", queue: queues[channel] });
|
| 42 |
});
|
| 43 |
|
| 44 |
+
// Start streaming the next video in the queue (Simulates live football broadcast)
|
| 45 |
function startStream(channel) {
|
| 46 |
if (!queues[channel] || queues[channel].length === 0) {
|
| 47 |
console.log(`No more videos in queue for channel ${channel}`);
|
| 48 |
delete streams[channel];
|
| 49 |
delete timestamps[channel];
|
| 50 |
+
activeChannels.delete(channel); // Remove from active channels
|
| 51 |
return;
|
| 52 |
}
|
| 53 |
|
| 54 |
+
// Stop existing FFmpeg process before starting a new one
|
| 55 |
if (streams[channel]) {
|
| 56 |
console.log(`Stopping existing stream for channel ${channel}`);
|
| 57 |
streams[channel].kill("SIGKILL");
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
const videoUrl = queues[channel].shift(); // Get the next video
|
| 62 |
+
queues[channel].push(videoUrl); // Re-add video to loop (infinite live streaming)
|
| 63 |
+
|
| 64 |
const outputFolder = path.join(hlsFolder, channel);
|
| 65 |
if (!fs.existsSync(outputFolder)) fs.mkdirSync(outputFolder, { recursive: true });
|
| 66 |
|
| 67 |
+
timestamps[channel] = Date.now(); // Set start time
|
|
|
|
| 68 |
|
| 69 |
+
console.log(`Starting live football stream for ${channel} from ${videoUrl}`);
|
| 70 |
|
| 71 |
+
// Generate multiple resolutions for adaptive streaming (ABR)
|
| 72 |
streams[channel] = ffmpeg(videoUrl)
|
| 73 |
.output(`${outputFolder}/index.m3u8`)
|
| 74 |
.addOptions([
|
| 75 |
"-c:v libx264",
|
| 76 |
"-preset veryfast",
|
| 77 |
"-crf 23",
|
| 78 |
+
"-b:v 2000k",
|
| 79 |
+
"-maxrate 2500k",
|
| 80 |
+
"-bufsize 5000k",
|
| 81 |
"-c:a aac",
|
| 82 |
"-b:a 128k",
|
| 83 |
+
"-hls_time 3", // Reduce segment time for smoother streaming
|
| 84 |
+
"-hls_list_size 10", // Keep more segments
|
| 85 |
+
"-hls_flags delete_segments+append_list",
|
| 86 |
+
"-start_number 0",
|
| 87 |
+
"-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease"
|
| 88 |
+
])
|
| 89 |
+
.output(`${outputFolder}/480p.m3u8`)
|
| 90 |
+
.addOptions([
|
| 91 |
+
"-c:v libx264",
|
| 92 |
+
"-preset veryfast",
|
| 93 |
+
"-crf 26",
|
| 94 |
+
"-b:v 1000k",
|
| 95 |
+
"-maxrate 1200k",
|
| 96 |
+
"-bufsize 3000k",
|
| 97 |
+
"-vf scale=w=854:h=480:force_original_aspect_ratio=decrease"
|
| 98 |
+
])
|
| 99 |
+
.output(`${outputFolder}/360p.m3u8`)
|
| 100 |
+
.addOptions([
|
| 101 |
+
"-c:v libx264",
|
| 102 |
+
"-preset veryfast",
|
| 103 |
+
"-crf 29",
|
| 104 |
+
"-b:v 600k",
|
| 105 |
+
"-maxrate 800k",
|
| 106 |
+
"-bufsize 2000k",
|
| 107 |
+
"-vf scale=w=640:h=360:force_original_aspect_ratio=decrease"
|
| 108 |
])
|
| 109 |
.on("end", () => {
|
| 110 |
console.log(`Finished streaming for channel ${channel}`);
|
|
|
|
| 116 |
console.error(`FFmpeg error for channel ${channel}:`, err);
|
| 117 |
delete streams[channel];
|
| 118 |
delete timestamps[channel];
|
| 119 |
+
startStream(channel); // Try next video
|
| 120 |
})
|
| 121 |
.run();
|
| 122 |
}
|
|
|
|
| 134 |
}
|
| 135 |
|
| 136 |
console.log(`Skipping to next video for channel ${channel}`);
|
| 137 |
+
streams[channel].kill("SIGKILL"); // Stop current stream
|
| 138 |
delete streams[channel];
|
| 139 |
|
| 140 |
+
startStream(channel); // Start next video
|
|
|
|
| 141 |
res.json({ message: "Skipped to next video" });
|
| 142 |
});
|
| 143 |
|
| 144 |
+
// Get active channels
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
app.get("/channels", (req, res) => {
|
| 146 |
+
res.json({ channels: Array.from(activeChannels) });
|
|
|
|
| 147 |
});
|
| 148 |
|
| 149 |
// Serve HLS files
|
| 150 |
app.use("/hls", express.static(hlsFolder));
|
| 151 |
|
| 152 |
+
// Get elapsed time since the current video started
|
| 153 |
app.get("/timestamp", (req, res) => {
|
| 154 |
const { channel } = req.query;
|
| 155 |
|
|
|
|
| 161 |
res.json({ elapsed });
|
| 162 |
});
|
| 163 |
|
| 164 |
+
app.listen(PORT, () => console.log(`⚽ Live football stream running on http://localhost:${PORT}`));
|