1 import * as Bull from 'bull'
2 import { copy, readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
6 import { VIDEO_LIVE } from '@server/initializers/constants'
7 import { generateVideoMiniature } from '@server/lib/thumbnail'
8 import { publishAndFederateIfNeeded } from '@server/lib/video'
9 import { getHLSDirectory } from '@server/lib/video-paths'
10 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
11 import { VideoModel } from '@server/models/video/video'
12 import { VideoFileModel } from '@server/models/video/video-file'
13 import { VideoLiveModel } from '@server/models/video/video-live'
14 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
15 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
16 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
17 import { logger } from '../../../helpers/logger'
19 async function processVideoLiveEnding (job: Bull.Job) {
20 const payload = job.data as VideoLiveEndingPayload
22 function logError () {
23 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
26 const video = await VideoModel.load(payload.videoId)
27 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
29 if (!video || !live) {
34 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
35 if (!streamingPlaylist) {
40 if (live.saveReplay !== true) {
41 return cleanupLive(video, streamingPlaylist)
44 return saveLive(video, live)
47 // ---------------------------------------------------------------------------
50 processVideoLiveEnding
53 // ---------------------------------------------------------------------------
55 async function saveLive (video: MVideo, live: MVideoLive) {
56 const hlsDirectory = getHLSDirectory(video, false)
57 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
59 const rootFiles = await readdir(hlsDirectory)
61 const playlistFiles: string[] = []
63 for (const file of rootFiles) {
64 // Move remaining files in the replay directory
65 if (file.endsWith('.ts') || file.endsWith('.m3u8')) {
66 await copy(join(hlsDirectory, file), join(replayDirectory, file))
69 if (file.endsWith('.m3u8') && file !== 'master.m3u8') {
70 playlistFiles.push(file)
74 const replayFiles = await readdir(replayDirectory)
76 const resolutions: number[] = []
79 for (const playlistFile of playlistFiles) {
80 const playlistPath = join(replayDirectory, playlistFile)
81 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
83 // Put the final mp4 in the hls directory, and not in the replay directory
84 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
86 // Playlist name is for example 3.m3u8
87 // Segments names are 3-0.ts 3-1.ts etc
88 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
90 const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
91 await hlsPlaylistToFragmentedMP4(replayDirectory, segmentFiles, mp4TmpPath)
94 duration = await getDurationFromVideoFile(mp4TmpPath)
97 resolutions.push(videoFileResolution)
100 await cleanupLiveFiles(hlsDirectory)
107 video.state = VideoState.TO_TRANSCODE
108 video.duration = duration
112 // Remove old HLS playlist video files
113 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
115 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
116 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
117 hlsPlaylist.VideoFiles = []
119 for (const resolution of resolutions) {
120 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
121 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
123 await generateHlsPlaylist({
124 video: videoWithFiles,
126 resolution: resolution,
131 await remove(videoInputPath)
134 // Regenerate the thumbnail & preview?
135 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
136 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
139 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
140 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
143 await publishAndFederateIfNeeded(video, true)
146 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
147 const hlsDirectory = getHLSDirectory(video)
149 await remove(hlsDirectory)
151 streamingPlaylist.destroy()
152 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
155 async function cleanupLiveFiles (hlsDirectory: string) {
156 const files = await readdir(hlsDirectory)
158 for (const filename of files) {
160 filename.endsWith('.ts') ||
161 filename.endsWith('.m3u8') ||
162 filename.endsWith('.mpd') ||
163 filename.endsWith('.m4s') ||
164 filename.endsWith('.tmp') ||
165 filename === VIDEO_LIVE.REPLAY_DIRECTORY
167 const p = join(hlsDirectory, filename)
170 .catch(err => logger.error('Cannot remove %s.', p, { err }))
175 function buildMP4TmpPath (basePath: string, resolution: number) {
176 return join(basePath, resolution + '-tmp.mp4')