-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (108 loc) · 3.47 KB
/
app.js
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
import express from 'express';
import fs from 'fs';
import ffmpeg from 'fluent-ffmpeg';
import { CacheClient } from '@gomomento/sdk';
// Initialize Momento CacheClient
const momento = new CacheClient({ defaultTtlSeconds: 3600 });
const NAMESPACE = 'bis';
const app = express();
app.use(express.json());
// POST endpoint to trigger livestream processing
app.post('/livestreams', async (req, res) => {
const { rtmpUrl, streamName } = req.body;
if (!rtmpUrl || !streamName) {
return res.status(400).json({ error: 'RTMP url and stream name are required' });
}
const stream = streamName.replace(/[^a-zA-Z]/g, "").toLowerCase();
res.status(202).json({ stream: `${stream}_playlist.m3u8` });
startTranscodingWorkflow(rtmpUrl, stream);
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
function startTranscodingWorkflow(rtmpUrl, streamName) {
ffmpeg(rtmpUrl)
// 1080p Output
.size('1920x1080')
.videoBitrate('5000k')
.output(`${streamName}/1080p/playlist.m3u8`)
.outputOptions([
'-c:v libx264',
'-g 48',
'-sc_threshold 0',
'-f hls',
'-hls_time 1',
'-hls_list_size 0',
`-hls_segment_filename ${streamName}/1080p/${streamName}_1080p_segment%03d.ts`
])
// 720p Output
.size('1280x720')
.videoBitrate('3000k')
.output(`${streamName}/720p/playlist.m3u8`)
.outputOptions([
'-c:v libx264',
'-g 48',
'-sc_threshold 0',
'-f hls',
'-hls_time 1',
'-hls_list_size 0',
`-hls_segment_filename ${streamName}/720p/${streamName}_720p_segment%03d.ts`
])
// 480p Output
.size('854x480')
.videoBitrate('1500k')
.output(`${streamName}/480p/playlist.m3u8`)
.outputOptions([
'-c:v libx264',
'-g 48',
'-sc_threshold 0',
'-f hls',
'-hls_time 1',
'-hls_list_size 0',
`-hls_segment_filename ${streamName}/480p/${streamName}_480p_segment%03d.ts`
])
.on('end', () => {
console.log('Transcoding complete');
})
.on('error', (err) => {
console.error(`Error during transcoding: ${err.message}`);
})
.run();
watchAndUploadSegments(streamName, ['1080p', '720p', '480p']);
uploadMasterPlaylist(streamName);
}
function watchAndUploadSegments(streamName, directories) {
for (const directory of directories) {
const streamDirectory = `${streamName}/${directory}`;
if (!fs.existsSync(streamDirectory)) {
fs.mkdirSync(streamDirectory, { recursive: true });
}
fs.watch(streamDirectory, (eventType, fileName) => {
if (fileName.endsWith('.ts') || fileName.endsWith('.m3u8')) {
const location = `${streamDirectory}/${fileName}`;
const key = `${streamName}_${directory}_${fileName}`;
uploadToMomento(location, key);
}
});
}
}
async function uploadToMomento(filepath, key) {
try {
const fileData = fs.readFileSync(filepath);
await momento.set(NAMESPACE, key, fileData);
console.log(`${key} uploaded`);
} catch (error) {
console.error(`Failed to upload ${key}:`, error);
}
}
async function uploadMasterPlaylist(streamName) {
const masterPlaylist = `#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080
${streamName}_1080p_playlist.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720
${streamName}_720p_playlist.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1500000,RESOLUTION=854x480
${streamName}_480p_playlist.m3u8
`;
await momento.set(NAMESPACE, `${streamName}_playlist.m3u8`, masterPlaylist);
}