Update server.js
Browse files
server.js
CHANGED
|
@@ -68,14 +68,13 @@ function startStream(channel) {
|
|
| 68 |
if (!queues[channel] || queues[channel].length === 0) {
|
| 69 |
console.log(`No more videos in queue for channel ${channel}`);
|
| 70 |
delete streams[channel];
|
| 71 |
-
delete timestamps[channel];
|
| 72 |
activeChannels.delete(channel);
|
| 73 |
saveQueues();
|
| 74 |
saveTimestamps();
|
| 75 |
return;
|
| 76 |
}
|
| 77 |
|
| 78 |
-
// Stop existing FFmpeg process before starting a new one
|
| 79 |
if (streams[channel]) {
|
| 80 |
console.log(`Stopping existing stream for channel ${channel}`);
|
| 81 |
try {
|
|
@@ -90,16 +89,15 @@ function startStream(channel) {
|
|
| 90 |
const outputFolder = path.join(hlsFolder, channel);
|
| 91 |
if (!fs.existsSync(outputFolder)) fs.mkdirSync(outputFolder, { recursive: true });
|
| 92 |
|
| 93 |
-
//
|
| 94 |
let startTime = timestamps[channel] || 0;
|
| 95 |
-
timestamps[channel] = startTime; // Prevents accidental resets
|
| 96 |
saveQueues();
|
| 97 |
saveTimestamps();
|
| 98 |
|
| 99 |
console.log(`Starting live stream for ${channel} from ${videoUrl} at ${startTime} seconds`);
|
| 100 |
|
| 101 |
streams[channel] = ffmpeg(videoUrl)
|
| 102 |
-
.inputOptions(startTime > 0 ? [`-ss ${startTime}`] : [])
|
| 103 |
.output(`${outputFolder}/index.m3u8`)
|
| 104 |
.addOptions([
|
| 105 |
"-c:v libx264",
|
|
@@ -108,15 +106,15 @@ function startStream(channel) {
|
|
| 108 |
"-b:v 1000k",
|
| 109 |
"-c:a aac",
|
| 110 |
"-b:a 128k",
|
| 111 |
-
"-hls_time 4",
|
| 112 |
-
"-hls_list_size 30",
|
| 113 |
"-hls_flags append_list+independent_segments",
|
| 114 |
"-hls_segment_type mpegts",
|
| 115 |
-
"-hls_delete_threshold 50",
|
| 116 |
-
"-g 24",
|
| 117 |
-
"-r 24",
|
| 118 |
-
"-bufsize 4000k",
|
| 119 |
-
"-maxrate 1200k",
|
| 120 |
"-strict -2",
|
| 121 |
])
|
| 122 |
.on("start", () => {
|
|
@@ -129,7 +127,6 @@ function startStream(channel) {
|
|
| 129 |
delete streams[channel];
|
| 130 |
delete timestamps[channel];
|
| 131 |
|
| 132 |
-
// Start next video if available
|
| 133 |
if (queues[channel] && queues[channel].length > 0) {
|
| 134 |
startStream(channel);
|
| 135 |
} else {
|
|
@@ -138,8 +135,8 @@ function startStream(channel) {
|
|
| 138 |
})
|
| 139 |
.on("error", (err) => {
|
| 140 |
console.error(`FFmpeg error for channel ${channel}:`, err);
|
|
|
|
| 141 |
|
| 142 |
-
// If a critical error occurs (e.g., missing file), skip video and continue
|
| 143 |
if (err.message.includes("Invalid data found") || err.message.includes("No such file")) {
|
| 144 |
console.log(`Skipping video due to critical error: ${videoUrl}`);
|
| 145 |
delete timestamps[channel];
|
|
@@ -147,7 +144,6 @@ function startStream(channel) {
|
|
| 147 |
|
| 148 |
delete streams[channel];
|
| 149 |
|
| 150 |
-
// Restart only if more videos exist
|
| 151 |
if (queues[channel] && queues[channel].length > 0) {
|
| 152 |
startStream(channel);
|
| 153 |
} else {
|
|
@@ -156,14 +152,6 @@ function startStream(channel) {
|
|
| 156 |
})
|
| 157 |
.run();
|
| 158 |
|
| 159 |
-
// Improved elapsed time tracking using FFmpeg metadata
|
| 160 |
-
ffmpeg.ffprobe(videoUrl, (err, metadata) => {
|
| 161 |
-
if (!err && metadata.format) {
|
| 162 |
-
timestamps[channel] = Math.max(0, startTime); // Ensures timestamps don't reset incorrectly
|
| 163 |
-
saveTimestamps();
|
| 164 |
-
}
|
| 165 |
-
});
|
| 166 |
-
|
| 167 |
// Start tracking elapsed time
|
| 168 |
trackElapsedTime(channel);
|
| 169 |
}
|
|
@@ -172,7 +160,6 @@ function startStream(channel) {
|
|
| 172 |
const timeTrackers = {}; // Store active interval IDs
|
| 173 |
|
| 174 |
function trackElapsedTime(channel) {
|
| 175 |
-
// Clear any existing interval before starting a new one
|
| 176 |
if (timeTrackers[channel]) {
|
| 177 |
clearInterval(timeTrackers[channel]);
|
| 178 |
}
|
|
@@ -184,6 +171,7 @@ function trackElapsedTime(channel) {
|
|
| 184 |
return;
|
| 185 |
}
|
| 186 |
|
|
|
|
| 187 |
timestamps[channel] = (timestamps[channel] || 0) + 1;
|
| 188 |
saveTimestamps();
|
| 189 |
}, 1000);
|
|
@@ -224,8 +212,12 @@ app.use("/hls", express.static(hlsFolder));
|
|
| 224 |
app.get("/timestamp", (req, res) => {
|
| 225 |
const { channel } = req.query;
|
| 226 |
|
| 227 |
-
if (!channel
|
| 228 |
-
return res.status(400).json({ error: "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
|
| 231 |
res.json({ elapsed: timestamps[channel] });
|
|
|
|
| 68 |
if (!queues[channel] || queues[channel].length === 0) {
|
| 69 |
console.log(`No more videos in queue for channel ${channel}`);
|
| 70 |
delete streams[channel];
|
| 71 |
+
delete timestamps[channel]; // Reset when queue is empty
|
| 72 |
activeChannels.delete(channel);
|
| 73 |
saveQueues();
|
| 74 |
saveTimestamps();
|
| 75 |
return;
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
if (streams[channel]) {
|
| 79 |
console.log(`Stopping existing stream for channel ${channel}`);
|
| 80 |
try {
|
|
|
|
| 89 |
const outputFolder = path.join(hlsFolder, channel);
|
| 90 |
if (!fs.existsSync(outputFolder)) fs.mkdirSync(outputFolder, { recursive: true });
|
| 91 |
|
| 92 |
+
// Keep previous timestamp unless starting a new video
|
| 93 |
let startTime = timestamps[channel] || 0;
|
|
|
|
| 94 |
saveQueues();
|
| 95 |
saveTimestamps();
|
| 96 |
|
| 97 |
console.log(`Starting live stream for ${channel} from ${videoUrl} at ${startTime} seconds`);
|
| 98 |
|
| 99 |
streams[channel] = ffmpeg(videoUrl)
|
| 100 |
+
.inputOptions(startTime > 0 ? [`-ss ${startTime}`] : [])
|
| 101 |
.output(`${outputFolder}/index.m3u8`)
|
| 102 |
.addOptions([
|
| 103 |
"-c:v libx264",
|
|
|
|
| 106 |
"-b:v 1000k",
|
| 107 |
"-c:a aac",
|
| 108 |
"-b:a 128k",
|
| 109 |
+
"-hls_time 4",
|
| 110 |
+
"-hls_list_size 30",
|
| 111 |
"-hls_flags append_list+independent_segments",
|
| 112 |
"-hls_segment_type mpegts",
|
| 113 |
+
"-hls_delete_threshold 50",
|
| 114 |
+
"-g 24",
|
| 115 |
+
"-r 24",
|
| 116 |
+
"-bufsize 4000k",
|
| 117 |
+
"-maxrate 1200k",
|
| 118 |
"-strict -2",
|
| 119 |
])
|
| 120 |
.on("start", () => {
|
|
|
|
| 127 |
delete streams[channel];
|
| 128 |
delete timestamps[channel];
|
| 129 |
|
|
|
|
| 130 |
if (queues[channel] && queues[channel].length > 0) {
|
| 131 |
startStream(channel);
|
| 132 |
} else {
|
|
|
|
| 135 |
})
|
| 136 |
.on("error", (err) => {
|
| 137 |
console.error(`FFmpeg error for channel ${channel}:`, err);
|
| 138 |
+
saveTimestamps(); // Save timestamp before restarting
|
| 139 |
|
|
|
|
| 140 |
if (err.message.includes("Invalid data found") || err.message.includes("No such file")) {
|
| 141 |
console.log(`Skipping video due to critical error: ${videoUrl}`);
|
| 142 |
delete timestamps[channel];
|
|
|
|
| 144 |
|
| 145 |
delete streams[channel];
|
| 146 |
|
|
|
|
| 147 |
if (queues[channel] && queues[channel].length > 0) {
|
| 148 |
startStream(channel);
|
| 149 |
} else {
|
|
|
|
| 152 |
})
|
| 153 |
.run();
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
// Start tracking elapsed time
|
| 156 |
trackElapsedTime(channel);
|
| 157 |
}
|
|
|
|
| 160 |
const timeTrackers = {}; // Store active interval IDs
|
| 161 |
|
| 162 |
function trackElapsedTime(channel) {
|
|
|
|
| 163 |
if (timeTrackers[channel]) {
|
| 164 |
clearInterval(timeTrackers[channel]);
|
| 165 |
}
|
|
|
|
| 171 |
return;
|
| 172 |
}
|
| 173 |
|
| 174 |
+
// Increment only if stream is active
|
| 175 |
timestamps[channel] = (timestamps[channel] || 0) + 1;
|
| 176 |
saveTimestamps();
|
| 177 |
}, 1000);
|
|
|
|
| 212 |
app.get("/timestamp", (req, res) => {
|
| 213 |
const { channel } = req.query;
|
| 214 |
|
| 215 |
+
if (!channel) {
|
| 216 |
+
return res.status(400).json({ error: "Channel is required." });
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
if (timestamps[channel] === undefined) {
|
| 220 |
+
return res.json({ elapsed: 0 }); // Ensures a valid response even if no timestamp exists
|
| 221 |
}
|
| 222 |
|
| 223 |
res.json({ elapsed: timestamps[channel] });
|