2021-08-18 00:01:11 +02:00
|
|
|
const fs = require('fs-extra')
|
|
|
|
|
2021-11-17 00:37:49 +01:00
|
|
|
function getPlaylistStr(segmentName, duration, segmentLength, hlsSegmentType, token) {
|
2021-10-01 01:52:32 +02:00
|
|
|
var ext = hlsSegmentType === 'fmp4' ? 'm4s' : 'ts'
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
var lines = [
|
|
|
|
'#EXTM3U',
|
|
|
|
'#EXT-X-VERSION:3',
|
|
|
|
'#EXT-X-ALLOW-CACHE:NO',
|
|
|
|
'#EXT-X-TARGETDURATION:6',
|
|
|
|
'#EXT-X-MEDIA-SEQUENCE:0',
|
|
|
|
'#EXT-X-PLAYLIST-TYPE:VOD'
|
|
|
|
]
|
2021-10-01 01:52:32 +02:00
|
|
|
if (hlsSegmentType === 'fmp4') {
|
|
|
|
lines.push('#EXT-X-MAP:URI="init.mp4"')
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var numSegments = Math.floor(duration / segmentLength)
|
|
|
|
var lastSegment = duration - (numSegments * segmentLength)
|
|
|
|
for (let i = 0; i < numSegments; i++) {
|
|
|
|
lines.push(`#EXTINF:6,`)
|
2021-11-17 00:37:49 +01:00
|
|
|
lines.push(`${segmentName}-${i}.${ext}?token=${token}`)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
if (lastSegment > 0) {
|
|
|
|
lines.push(`#EXTINF:${lastSegment},`)
|
2021-11-17 00:37:49 +01:00
|
|
|
lines.push(`${segmentName}-${numSegments}.${ext}?token=${token}`)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
lines.push('#EXT-X-ENDLIST')
|
|
|
|
return lines.join('\n')
|
|
|
|
}
|
|
|
|
|
2021-11-17 00:37:49 +01:00
|
|
|
function generatePlaylist(outputPath, segmentName, duration, segmentLength, hlsSegmentType, token) {
|
|
|
|
var playlistStr = getPlaylistStr(segmentName, duration, segmentLength, hlsSegmentType, token)
|
2021-08-18 00:01:11 +02:00
|
|
|
return fs.writeFile(outputPath, playlistStr)
|
|
|
|
}
|
|
|
|
module.exports = generatePlaylist
|