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
|
import { VideoModel } from '../models/video/video'
import { basename, dirname, join } from 'path'
import { HLS_PLAYLIST_DIRECTORY, CONFIG } from '../initializers'
import { outputJSON, pathExists, readdir, readFile, remove, writeFile, move } from 'fs-extra'
import { getVideoFileSize } from '../helpers/ffmpeg-utils'
import { sha256 } from '../helpers/core-utils'
import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
import HLSDownloader from 'hlsdownloader'
import { logger } from '../helpers/logger'
import { parse } from 'url'
async function updateMasterHLSPlaylist (video: VideoModel) {
const directory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
const masterPlaylists: string[] = [ '#EXTM3U', '#EXT-X-VERSION:3' ]
const masterPlaylistPath = join(directory, VideoStreamingPlaylistModel.getMasterHlsPlaylistFilename())
for (const file of video.VideoFiles) {
// If we did not generated a playlist for this resolution, skip
const filePlaylistPath = join(directory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
if (await pathExists(filePlaylistPath) === false) continue
const videoFilePath = video.getVideoFilePath(file)
const size = await getVideoFileSize(videoFilePath)
const bandwidth = 'BANDWIDTH=' + video.getBandwidthBits(file)
const resolution = `RESOLUTION=${size.width}x${size.height}`
let line = `#EXT-X-STREAM-INF:${bandwidth},${resolution}`
if (file.fps) line += ',FRAME-RATE=' + file.fps
masterPlaylists.push(line)
masterPlaylists.push(VideoStreamingPlaylistModel.getHlsPlaylistFilename(file.resolution))
}
await writeFile(masterPlaylistPath, masterPlaylists.join('\n') + '\n')
}
async function updateSha256Segments (video: VideoModel) {
const directory = join(HLS_PLAYLIST_DIRECTORY, video.uuid)
const files = await readdir(directory)
const json: { [filename: string]: string} = {}
for (const file of files) {
if (file.endsWith('.ts') === false) continue
const buffer = await readFile(join(directory, file))
const filename = basename(file)
json[filename] = sha256(buffer)
}
const outputPath = join(directory, VideoStreamingPlaylistModel.getHlsSha256SegmentsFilename())
await outputJSON(outputPath, json)
}
function downloadPlaylistSegments (playlistUrl: string, destinationDir: string, timeout: number) {
let timer
logger.info('Importing HLS playlist %s', playlistUrl)
const params = {
playlistURL: playlistUrl,
destination: CONFIG.STORAGE.TMP_DIR
}
const downloader = new HLSDownloader(params)
const hlsDestinationDir = join(CONFIG.STORAGE.TMP_DIR, dirname(parse(playlistUrl).pathname))
return new Promise<string>(async (res, rej) => {
downloader.startDownload(err => {
clearTimeout(timer)
if (err) {
deleteTmpDirectory(hlsDestinationDir)
return rej(err)
}
move(hlsDestinationDir, destinationDir, { overwrite: true })
.then(() => res())
.catch(err => {
deleteTmpDirectory(hlsDestinationDir)
return rej(err)
})
})
timer = setTimeout(() => {
deleteTmpDirectory(hlsDestinationDir)
return rej(new Error('HLS download timeout.'))
}, timeout)
function deleteTmpDirectory (directory: string) {
remove(directory)
.catch(err => logger.error('Cannot delete path on HLS download error.', { err }))
}
})
}
// ---------------------------------------------------------------------------
export {
updateMasterHLSPlaylist,
updateSha256Segments,
downloadPlaylistSegments
}
// ---------------------------------------------------------------------------
|