-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
329 lines (286 loc) · 10.7 KB
/
index.mjs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env node
import ffmpeg from 'fluent-ffmpeg';
import path from 'path';
import fs from 'fs';
import os from 'os';
import {Chalk} from 'chalk';
import Table from 'cli-table';
import { pathToFileURL } from 'url';
const chalk = new Chalk({level: 2});
const minutes = 60 // size of minutes unit in seconds
const sampleTypes = {
u16 : 'unsigned 16 bits',
s16 : 'signed 16 bits',
s16p : 'signed 16 bits, planar',
flt : 'float',
fltp : 'float, planar',
dbl : 'double',
dblp : 'double, planar',
}
let lastTableHeight = 0;
let clearScreen = false;
// Determine the path to the configuration file
const configDir = path.join(os.homedir(), '.radioArchive');
const configPath = path.join(configDir, '.radioArchive.config.js');
// Ensure the configuration file exists
if (!fs.existsSync(configPath)) {
console.error(`Configuration file does not exist: ${configPath}`);
process.exit(1);
}
let stations, radioArchiveLoc
// Load the configuration file
import(pathToFileURL(configPath)).then(config => {
stations = config.default.stations;
radioArchiveLoc = config.default.radioArchiveLoc;
// Start all recordings immediately
startAllRecordings();
// Prepare folder for tomorrow and set an interval to check every hour
prepareNextDayFolder(stations);
setInterval(() => {
prepareNextDayFolder(stations);
}, 1*minutes*1000);
setInterval(() => {
updateTerminalInterface();
}, 1000);
// Add an interval to monitor the streams
setInterval(() => {
monitorStreams();
}, 5000); // Check every 5 seconds
}).catch(error => {
console.error('Error loading configuration:', error.message);
});
// Function to update the terminal interface
function updateTerminalInterface() {
const now = new Date()
const height = process.stdout.rows;
if(height!=lastTableHeight || clearScreen){
lastTableHeight = height
clearScreen=false;
// Clear the entire screen
process.stdout.write('\u001b[2J');
}
// Move cursor to the beginning of the screen
process.stdout.write('\u001b[H');
// Display additional information at the top
console.log('======================================================');
console.log(chalk.redBright('Stream Recorder'));
// console.log(chalk.white('Started at: ') + chalk.green(startedAt.toString()));
// if(restartingAt) {
// const restartDuration = Math.ceil((restartingAt-now)/1000);
// const restartMinutes = Math.floor(restartDuration / 60);
// const restartSeconds = Math.floor(restartDuration % 60);
// const restartText = chalk.green(restartMinutes+':'+restartSeconds);
// console.log(chalk.white('Restarting at: ') + chalk.green(restartingAt.toString()));
// console.log(chalk.white('Restarting in: ') + chalk.green(restartMinutes+':'+restartSeconds));
// }
// console.log('-------------------------');
// Create a new table
const table = new Table({
head: ['Station', 'Status', 'Progress', 'Codec Data', 'Started At', 'Next Restart'],
colWidths: [17, 20, 17, 30, 17, 17]
});
// Populate the table with station data
stations.forEach(station => {
const status = station?.status ?? "Starting..."
const tableData = [
chalk.cyanBright(station.name),
(status==="Recording..." ? chalk.red('• ') : '') + chalk.green(status),
chalk.green(station?.progress?.timemark ?? "N/A"),
];
if(station?.codecData){
const data = station.codecData;
// tableData.push(data.format)
let sampleFormat = data.audio_details[3];
sampleFormat = sampleTypes?.[sampleFormat] ?? sampleFormat;
tableData.push(`Format: ${data.format}
Sample Rate: ${data.audio_details[1]}
Channels: ${data.audio_details[2]}
Sample Format: ${sampleFormat}
Bitrate: ${data.audio_details[4]}`);
}else{
tableData.push('')
}
if (station?.startedAt) {
tableData.push(station.startedAt.toLocaleTimeString());
let restartStatus = "N/A"
if(station?.nextRestart) {
restartStatus = station.nextRestart.toLocaleTimeString();
const restartingAt = station.nextRestart
const restartDuration = Math.ceil((restartingAt-now)/1000);
const restartMinutes = Math.floor(restartDuration / 60);
const restartSeconds = String(Math.floor(restartDuration % 60)).padStart(2,"0");
const restartText = chalk.green("T-"+restartMinutes+':'+restartSeconds);
restartStatus += "\n" + restartText;
}
tableData.push(restartStatus);
} else {
tableData.push("N/A");
tableData.push("N/A");
}
table.push(tableData);
});
// Display the table
console.log(table.toString());
// Additional information at the bottom
console.log('======================================================');
console.log(chalk.yellowBright('Press Ctrl+C to exit.'));
// console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);
}
// Function to read a playlist file and extract stream url value
function extractStreamUrl(filePath) {
if(filePath.endsWith('.pls')){
return extractStreamUrlFromPls(filePath);
}
if(filePath.endsWith('.m3u')){
return extractStreamUrlFromM3u(filePath);
}
}
// Function to read .pls file and extract File1 value
function extractStreamUrlFromM3u(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
return content.trim()
}
// Function to read .pls file and extract File1 value
function extractStreamUrlFromPls(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const match = content.match(/^File1=(.*)$/m);
return match ? match[1] : null;
}
// Function to record a stream
function recordStream(station, url, outputFilename) {
return ffmpeg(url)
.inputOptions([
// '-user_agent', '"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"' // Set the User-Agent
])
.audioCodec('copy') // Copy the audio stream without re-encoding
// .format('mp3') // Output format
.format('segment') // Use the segment muxer
.outputOptions([
`-segment_time ${30*minutes}`, // Segment duration in seconds
'-reset_timestamps 1', // Reset timestamps at the beginning of each segment
'-strftime 1' // Use strftime in the output filename
])
.on('start', (commandLine) => {
station.status = "Started"
clearScreen=true;
// console.log(`[${station.name}] Spawned Ffmpeg with command: ` + commandLine);
})
.on('codecData', function(data) {
station.codecData = data;
// station.codecData = JSON.stringify(data,null,2);
// station.codecData = `Format: ${data.format}
// Sample Rate: ${data.audio_details[1]}
// Channels: ${data.audio_details[2]}
// Sample Format: ${data.audio_details[3]}
// Bitrate: ${data.audio_details[4]}`
// console.log(`[${station.name}] Input is ` + JSON.stringify(data,null,2) );
clearScreen=true;
})
.on('error', (err) => {
station.status = "ERROR!"
// console.log(`[${station.name}] An error occurred: ${err.message}`);
})
.on('progress', function(progress) {
station.status = "Recording..."
station.progress = progress;
station.lastProgressTime = new Date(); // Update the last progress time
updateTerminalInterface()
})
.on('end', () => {
station.status = "Stopped"
// console.log(`Recording saved to ${outputFilename}`);
})
.save(`${outputFilename}`); // Save the stream to a file
}
// Function to ensure directory exists
function ensureDir(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
function prepareNextDayFolder(stations) {
const now = new Date()
const year = String(now.getFullYear())
const month = String(now.getMonth()+1).padStart(2,"0")
const day = String(now.getDate()).padStart(2,"0");
const dayName = now.toLocaleString('en-US', { weekday: 'long' });
const monthName = now.toLocaleString('en-US', { month: 'long' });
stations.forEach((station)=>{
const tomorrowDirFancy = path.join(radioArchiveLoc,station.name,year,`${month}-${monthName}`,`${day}-${dayName}`);
const tomorrowDir = path.join(radioArchiveLoc,station.name,year,month,day);
ensureDir(tomorrowDirFancy);
ensureDir(tomorrowDir);
})
}
function startStationRecording(station,isTimelyRestart=false){
// Extract the stream URL from the .pls file
const streamUrl = extractStreamUrl(path.join(configDir, station.file));
if (streamUrl) {
// Start recording
const outputFilename = path.join(radioArchiveLoc, station.name, `/%Y/%m/%d/${station.name}_%Y-%m-%d_%H-%M-%S.mp3`);
station.streamRecording = recordStream(station, streamUrl, outputFilename);
station.startedAt = new Date(); // Update the startedAt time
if(!isTimelyRestart){
station.nextRestart = new Date(station.startedAt.getTime() + timeUntilNextStart()); // Next restart time
// Schedule to start all recordings again at the top of the next hour
station.restartTimeout = setTimeout(() => {
station.streamRecording.kill()
startStationRecording(station, true)
}, timeUntilNextStart());
}else{
station.nextRestart = null
}
} else {
console.log('Could not extract stream URL from station playlist');
}
}
function startAllRecordings() {
stations.forEach((station) => {
startStationRecording(station)
});
clearScreen=true;
}
// Function to calculate time until the next hour in milliseconds
function getNextStart(){
const now = new Date();
const nextStart = new Date(now);
// If current minutes are less than 30, set the next start time to the 30-minute mark of the current hour
if (now.getMinutes() < 30) {
nextStart.setMinutes(30);
} else {
// Otherwise, set the next start time to the top of the next hour
nextStart.setHours(now.getHours() + 1);
nextStart.setMinutes(0);
}
nextStart.setSeconds(0);
nextStart.setMilliseconds(0);
return nextStart;
}
function timeUntilNextStart(nextStart=null) {
const now = new Date();
if(!nextStart) nextStart = getNextStart();
return nextStart - now;
}
// Function to monitor and restart streams if necessary
function monitorStreams() {
stations.forEach((station) => {
if (!station.lastProgressTime) {
station.lastProgressTime = new Date();
}
const now = new Date();
const timeSinceLastProgress = now - station.lastProgressTime;
if ((station.status === "ERROR!" || station.status==="Recording..." || station.status==="Stopped") && timeSinceLastProgress > 10000) // 10 seconds
{
// Kill the existing ffmpeg process for this station
if (station.streamRecording) {
station.streamRecording.kill();
}
if (station.restartTimeout) {
clearTimeout(station.restartTimeout)
}
station.status = 'Restarting...';
// Restart the stream
startStationRecording(station);
}
});
}