-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (67 loc) · 2.39 KB
/
index.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
'use strict';
const path = require('path');
const fs = require('fs');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
// Chunks
const chunksPath = path.join(__dirname, '/chunks.json');
const chunksObject = JSON.parse(fs.readFileSync(chunksPath, 'utf-8'));
const chunksObjectKeys = Object.keys(chunksObject.chunks);
// File with m3u8 signature
const m3u8List = path.join(__dirname, 'hls.m3u8');
let startGroup = 0;
const countGroups = chunksObjectKeys.length;
// Added delimiter mark to chunks list
const splitChunks = [];
while (startGroup < countGroups) {
const group = chunksObject.chunks[chunksObjectKeys[startGroup]];
group.map(chunk => splitChunks.push(chunk));
splitChunks[splitChunks.length - 1].delimeter = true;
startGroup += 1;
}
let indexChunk = 1;
const app = new Koa();
const router = new Router();
app.use(bodyParser());
app.use(require('koa-static')(__dirname + '/web_hls'));
app.use(require('koa-static')(__dirname + '/chunks'));
// Dynamic generation m3u8 list from chunks
router.get('/get_m3u8', async ctx => {
const chunk_1 = splitChunks[indexChunk - 2] || {};
const chunk0 = splitChunks[indexChunk - 1];
const chunk1 = splitChunks[indexChunk];
const chunk2 = splitChunks[indexChunk + 1];
if (!chunk2) {
// Chunk list ended
console.log('END');
} else {
const m3u8File =
'#EXTM3U\n' +
'#EXT-X-VERSION:3\n' +
`#EXT-X-MEDIA-SEQUENCE:${indexChunk - 1}\n` +
'#EXT-X-ALLOW-CACHE:YES\n' +
'#EXT-X-TARGETDURATION:11\n' +
`${chunk_1.delimeter ? '#EXT-X-DISCONTINUITY\n' : ''}` +
`#EXTINF:${chunk0.duration},\n` +
`http://localhost:5001/${chunk0.name}\n` +
`${chunk0.delimeter ? '#EXT-X-DISCONTINUITY\n' : ''}` +
`#EXTINF:${chunk1.duration},\n` +
`http://localhost:5001/${chunk1.name}\n` +
`${chunk1.delimeter ? '#EXT-X-DISCONTINUITY\n' : ''}` +
`#EXTINF:${chunk2.duration},\n` +
`http://localhost:5001/${chunk2.name}\n`;
// Write to file
const appendStream = fs.createWriteStream(m3u8List, {flags: 'w', encoding: 'utf8', autoClose: true});
appendStream.write(m3u8File);
appendStream.end();
console.log('Added group', indexChunk, new Date());
indexChunk += 1;
ctx.set('Content-Type', 'application/x-mpegURL');
ctx.body = m3u8File;
}
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(5001, () => console.log('Server started on http://localhost:5001'));