1 import * as Bull from 'bull'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { publishAndFederateIfNeeded } from '@server/lib/video'
6 import { getHLSDirectory } from '@server/lib/video-paths'
7 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
8 import { VideoModel } from '@server/models/video/video'
9 import { VideoFileModel } from '@server/models/video/video-file'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
12 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
13 import { VideoLiveEndingPayload, VideoState } from '@shared/models'
14 import { logger } from '../../../helpers/logger'
16 async function processVideoLiveEnding (job: Bull.Job) {
17 const payload = job.data as VideoLiveEndingPayload
19 function logError () {
20 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
23 const video = await VideoModel.load(payload.videoId)
24 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
26 if (!video || !live) {
31 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
32 if (!streamingPlaylist) {
37 if (live.saveReplay !== true) {
38 return cleanupLive(video, streamingPlaylist)
41 return saveLive(video, live)
44 // ---------------------------------------------------------------------------
47 processVideoLiveEnding
50 // ---------------------------------------------------------------------------
52 async function saveLive (video: MVideo, live: MVideoLive) {
53 const hlsDirectory = getHLSDirectory(video, false)
54 const files = await readdir(hlsDirectory)
56 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
57 const resolutions: number[] = []
60 for (const playlistFile of playlistFiles) {
61 const playlistPath = join(hlsDirectory, playlistFile)
62 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
64 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
66 // Playlist name is for example 3.m3u8
67 // Segments names are 3-0.ts 3-1.ts etc
68 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
70 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
71 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
73 for (const file of segmentFiles) {
74 await remove(join(hlsDirectory, file))
78 duration = await getDurationFromVideoFile(mp4TmpPath)
81 resolutions.push(videoFileResolution)
84 await cleanupLiveFiles(hlsDirectory)
89 video.state = VideoState.TO_TRANSCODE
90 video.duration = duration
94 // Remove old HLS playlist video files
95 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
97 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
98 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
99 hlsPlaylist.VideoFiles = []
101 for (const resolution of resolutions) {
102 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
103 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
105 await generateHlsPlaylist({
106 video: videoWithFiles,
108 resolution: resolution,
113 await remove(videoInputPath)
116 await publishAndFederateIfNeeded(video, true)
119 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
120 const hlsDirectory = getHLSDirectory(video, false)
122 await remove(hlsDirectory)
124 streamingPlaylist.destroy()
125 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
128 async function cleanupLiveFiles (hlsDirectory: string) {
129 const files = await readdir(hlsDirectory)
131 for (const filename of files) {
133 filename.endsWith('.ts') ||
134 filename.endsWith('.m3u8') ||
135 filename.endsWith('.mpd') ||
136 filename.endsWith('.m4s') ||
137 filename.endsWith('.tmp')
139 const p = join(hlsDirectory, filename)
142 .catch(err => logger.error('Cannot remove %s.', p, { err }))
147 function buildMP4TmpPath (basePath: string, resolution: number) {
148 return join(basePath, resolution + '-tmp.mp4')