1 import { Job } from 'bullmq'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo } from '@server/helpers/ffmpeg'
5 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7 import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live'
8 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths'
9 import { generateVideoMiniature } from '@server/lib/thumbnail'
10 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/transcoding'
11 import { moveToNextState } from '@server/lib/video-state'
12 import { VideoModel } from '@server/models/video/video'
13 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14 import { VideoFileModel } from '@server/models/video/video-file'
15 import { VideoLiveModel } from '@server/models/video/video-live'
16 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
17 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
18 import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
19 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
20 import { logger, loggerTagsFactory } from '../../../helpers/logger'
21 import { VideoPathManager } from '@server/lib/video-path-manager'
22 import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
24 const lTags = loggerTagsFactory('live', 'job')
26 async function processVideoLiveEnding (job: Job) {
27 const payload = job.data as VideoLiveEndingPayload
29 logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
31 function logError () {
32 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
35 const video = await VideoModel.load(payload.videoId)
36 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
37 const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId)
39 if (!video || !live || !liveSession) {
44 const permanentLive = live.permanentLive
46 liveSession.endingProcessed = true
47 await liveSession.save()
49 if (liveSession.saveReplay !== true) {
50 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
54 await saveReplayToExternalVideo({
57 publishedAt: payload.publishedAt,
58 replayDirectory: payload.replayDirectory
61 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
64 return replaceLiveByReplay({
69 replayDirectory: payload.replayDirectory
73 // ---------------------------------------------------------------------------
76 processVideoLiveEnding
79 // ---------------------------------------------------------------------------
81 async function saveReplayToExternalVideo (options: {
83 liveSession: MVideoLiveSession
85 replayDirectory: string
87 const { liveVideo, liveSession, publishedAt, replayDirectory } = options
89 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
91 const replayVideo = new VideoModel({
92 name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`,
94 state: VideoState.TO_TRANSCODE,
97 remote: liveVideo.remote,
98 category: liveVideo.category,
99 licence: liveVideo.licence,
100 language: liveVideo.language,
101 commentsEnabled: liveVideo.commentsEnabled,
102 downloadEnabled: liveVideo.downloadEnabled,
103 waitTranscoding: true,
104 nsfw: liveVideo.nsfw,
105 description: liveVideo.description,
106 support: liveVideo.support,
107 privacy: replaySettings.privacy,
108 channelId: liveVideo.channelId
109 }) as MVideoWithAllFiles
111 replayVideo.Thumbnails = []
112 replayVideo.VideoFiles = []
113 replayVideo.VideoStreamingPlaylists = []
115 replayVideo.url = getLocalVideoActivityPubUrl(replayVideo)
117 await replayVideo.save()
119 liveSession.replayVideoId = replayVideo.id
120 await liveSession.save()
122 // If live is blacklisted, also blacklist the replay
123 const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id)
125 await VideoBlacklistModel.create({
126 videoId: replayVideo.id,
127 unfederated: blacklist.unfederated,
128 reason: blacklist.reason,
133 await assignReplayFilesToVideo({ video: replayVideo, replayDirectory })
135 await remove(replayDirectory)
137 for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
138 const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
139 await replayVideo.addAndSaveThumbnail(image)
142 await moveToNextState({ video: replayVideo, isNewVideo: true })
145 async function replaceLiveByReplay (options: {
147 liveSession: MVideoLiveSession
149 permanentLive: boolean
150 replayDirectory: string
152 const { video, liveSession, live, permanentLive, replayDirectory } = options
154 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
155 const videoWithFiles = await VideoModel.loadFull(video.id)
156 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
158 await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist)
162 videoWithFiles.isLive = false
163 videoWithFiles.privacy = replaySettings.privacy
164 videoWithFiles.waitTranscoding = true
165 videoWithFiles.state = VideoState.TO_TRANSCODE
167 await videoWithFiles.save()
169 liveSession.replayVideoId = videoWithFiles.id
170 await liveSession.save()
172 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
175 hlsPlaylist.VideoFiles = []
176 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
177 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
178 await hlsPlaylist.save()
180 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
182 if (permanentLive) { // Remove session replay
183 await remove(replayDirectory)
184 } else { // We won't stream again in this live, we can delete the base replay directory
185 await remove(getLiveReplayBaseDirectory(videoWithFiles))
188 // Regenerate the thumbnail & preview?
189 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
190 const miniature = await generateVideoMiniature({
191 video: videoWithFiles,
192 videoFile: videoWithFiles.getMaxQualityFile(),
193 type: ThumbnailType.MINIATURE
195 await videoWithFiles.addAndSaveThumbnail(miniature)
198 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
199 const preview = await generateVideoMiniature({
200 video: videoWithFiles,
201 videoFile: videoWithFiles.getMaxQualityFile(),
202 type: ThumbnailType.PREVIEW
204 await videoWithFiles.addAndSaveThumbnail(preview)
207 // We consider this is a new video
208 await moveToNextState({ video: videoWithFiles, isNewVideo: true })
211 async function assignReplayFilesToVideo (options: {
213 replayDirectory: string
215 const { video, replayDirectory } = options
217 const concatenatedTsFiles = await readdir(replayDirectory)
219 for (const concatenatedTsFile of concatenatedTsFiles) {
220 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
222 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
224 const probe = await ffprobePromise(concatenatedTsFilePath)
225 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
226 const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe)
229 await generateHlsPlaylistResolutionFromTS({
231 inputFileMutexReleaser,
232 concatenatedTsFilePath,
234 isAAC: audioStream?.codec_name === 'aac'
237 logger.error('Cannot generate HLS playlist resolution from TS files.', { err })
240 inputFileMutexReleaser()
246 async function cleanupLiveAndFederate (options: {
248 permanentLive: boolean
249 streamingPlaylistId: number
251 const { permanentLive, video, streamingPlaylistId } = options
253 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
255 if (streamingPlaylist) {
257 await cleanupAndDestroyPermanentLive(video, streamingPlaylist)
259 await cleanupUnsavedNormalLive(video, streamingPlaylist)
264 const fullVideo = await VideoModel.loadFull(video.id)
265 return federateVideoIfNeeded(fullVideo, false, undefined)
267 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })