-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (61 loc) · 2.29 KB
/
Copy pathserver.js
File metadata and controls
70 lines (61 loc) · 2.29 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const app = express();
const PORT = 3000;
// Serve the HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
//frontend call : 'http://localhost:3000/bad'
app.get('/bad-stream', (req, res) => {
const filePath = path.join(__dirname, '/songs/songs.mp3');
if (fs.existsSync(filePath)) {
res.setHeader('Content-Type', 'audio/mpeg');
res.setHeader('Content-Disposition', 'attachment; filename="songs.mp3"');
fs.createReadStream(filePath).pipe(res);
} else {
res.status(404).send('MP3 file not found');
}
});
//sending file in chunks and combine while use
//frontend call : 'http://localhost:3000/good-stream/hls/playlist.m3u8'
app.get('/good-stream/hls/:file', (req, res) => {
const filePath = path.join(__dirname, 'hls', req.params.file);
if (fs.existsSync(filePath)) {
const ext = path.extname(req.params.file);
if (ext === '.m3u8') {
res.setHeader('Content-Type', 'application/vnd.apple.mpegurl');
} else if (ext === '.ts') {
res.setHeader('Content-Type', 'video/mp2t');
}
res.sendFile(filePath);
} else {
res.status(404).send('File not found');
}
});
// Generate HLS files for audio (Bad Stream - Good Practice)
const generateHLS = (inputFile, outputFolder) => {
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
}
const command = `ffmpeg -i ${inputFile} -vn -acodec copy -hls_time 9 -hls_list_size 0 -f hls ${outputFolder}/playlist.m3u8`;
child_process.exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error generating HLS: ${error.message}`);
return;
}
console.log('HLS files generated successfully');
});
};
//calling generateHLS if HLS don't exist
const inputFile = path.join(__dirname, '/songs/songs.mp3');
const outputFolder = path.join(__dirname, 'hls');
if (!fs.existsSync(path.join(outputFolder, 'playlist.m3u8'))) {
console.log('Generating HLS files...');
generateHLS(inputFile, outputFolder);
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});