1 import { Job } from 'bullmq'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
5 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
6 import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live'
7 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths'
8 import { generateVideoMiniature } from '@server/lib/thumbnail'
9 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/hls-transcoding'
10 import { VideoPathManager } from '@server/lib/video-path-manager'
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 { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
17 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
18 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
19 import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
20 import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg'
21 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
22 import { logger, loggerTagsFactory } from '../../../helpers/logger'
23 import { peertubeTruncate } from '@server/helpers/core-utils'
24 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
26 const lTags = loggerTagsFactory('live', 'job')
28 async function processVideoLiveEnding (job: Job) {
29 const payload = job.data as VideoLiveEndingPayload
31 logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
33 function logError () {
34 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
37 const video = await VideoModel.load(payload.videoId)
38 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
39 const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId)
41 if (!video || !live || !liveSession) {
46 const permanentLive = live.permanentLive
48 liveSession.endingProcessed = true
49 await liveSession.save()
51 if (liveSession.saveReplay !== true) {
52 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
56 await saveReplayToExternalVideo({
59 publishedAt: payload.publishedAt,
60 replayDirectory: payload.replayDirectory
63 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
66 return replaceLiveByReplay({
71 replayDirectory: payload.replayDirectory
75 // ---------------------------------------------------------------------------
78 processVideoLiveEnding
81 // ---------------------------------------------------------------------------
83 async function saveReplayToExternalVideo (options: {
85 liveSession: MVideoLiveSession
87 replayDirectory: string
89 const { liveVideo, liveSession, publishedAt, replayDirectory } = options
91 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
93 const videoNameSuffix = ` - ${new Date(publishedAt).toLocaleString()}`
94 const truncatedVideoName = peertubeTruncate(liveVideo.name, {
95 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max - videoNameSuffix.length
98 const replayVideo = new VideoModel({
99 name: truncatedVideoName + videoNameSuffix,
101 state: VideoState.TO_TRANSCODE,
104 remote: liveVideo.remote,
105 category: liveVideo.category,
106 licence: liveVideo.licence,
107 language: liveVideo.language,
108 commentsEnabled: liveVideo.commentsEnabled,
109 downloadEnabled: liveVideo.downloadEnabled,
110 waitTranscoding: true,
111 nsfw: liveVideo.nsfw,
112 description: liveVideo.description,
113 support: liveVideo.support,
114 privacy: replaySettings.privacy,
115 channelId: liveVideo.channelId
116 }) as MVideoWithAllFiles
118 replayVideo.Thumbnails = []
119 replayVideo.VideoFiles = []
120 replayVideo.VideoStreamingPlaylists = []
122 replayVideo.url = getLocalVideoActivityPubUrl(replayVideo)
124 await replayVideo.save()
126 liveSession.replayVideoId = replayVideo.id
127 await liveSession.save()
129 // If live is blacklisted, also blacklist the replay
130 const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id)
132 await VideoBlacklistModel.create({
133 videoId: replayVideo.id,
134 unfederated: blacklist.unfederated,
135 reason: blacklist.reason,
140 await assignReplayFilesToVideo({ video: replayVideo, replayDirectory })
142 await remove(replayDirectory)
144 for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
145 const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
146 await replayVideo.addAndSaveThumbnail(image)
149 await moveToNextState({ video: replayVideo, isNewVideo: true })
152 async function replaceLiveByReplay (options: {
154 liveSession: MVideoLiveSession
156 permanentLive: boolean
157 replayDirectory: string
159 const { video, liveSession, live, permanentLive, replayDirectory } = options
161 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
162 const videoWithFiles = await VideoModel.loadFull(video.id)
163 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
165 await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist)
169 videoWithFiles.isLive = false
170 videoWithFiles.privacy = replaySettings.privacy
171 videoWithFiles.waitTranscoding = true
172 videoWithFiles.state = VideoState.TO_TRANSCODE
174 await videoWithFiles.save()
176 liveSession.replayVideoId = videoWithFiles.id
177 await liveSession.save()
179 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
182 hlsPlaylist.VideoFiles = []
183 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
184 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
185 await hlsPlaylist.save()
187 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
189 if (permanentLive) { // Remove session replay
190 await remove(replayDirectory)
191 } else { // We won't stream again in this live, we can delete the base replay directory
192 await remove(getLiveReplayBaseDirectory(videoWithFiles))
195 // Regenerate the thumbnail & preview?
196 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
197 const miniature = await generateVideoMiniature({
198 video: videoWithFiles,
199 videoFile: videoWithFiles.getMaxQualityFile(),
200 type: ThumbnailType.MINIATURE
202 await videoWithFiles.addAndSaveThumbnail(miniature)
205 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
206 const preview = await generateVideoMiniature({
207 video: videoWithFiles,
208 videoFile: videoWithFiles.getMaxQualityFile(),
209 type: ThumbnailType.PREVIEW
211 await videoWithFiles.addAndSaveThumbnail(preview)
214 // We consider this is a new video
215 await moveToNextState({ video: videoWithFiles, isNewVideo: true })
218 async function assignReplayFilesToVideo (options: {
220 replayDirectory: string
222 const { video, replayDirectory } = options
224 const concatenatedTsFiles = await readdir(replayDirectory)
226 for (const concatenatedTsFile of concatenatedTsFiles) {
227 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
230 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
232 const probe = await ffprobePromise(concatenatedTsFilePath)
233 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
234 const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe)
235 const fps = await getVideoStreamFPS(concatenatedTsFilePath, probe)
238 await generateHlsPlaylistResolutionFromTS({
240 inputFileMutexReleaser,
241 concatenatedTsFilePath,
244 isAAC: audioStream?.codec_name === 'aac'
247 logger.error('Cannot generate HLS playlist resolution from TS files.', { err })
250 inputFileMutexReleaser()
256 async function cleanupLiveAndFederate (options: {
258 permanentLive: boolean
259 streamingPlaylistId: number
261 const { permanentLive, video, streamingPlaylistId } = options
263 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
265 if (streamingPlaylist) {
267 await cleanupAndDestroyPermanentLive(video, streamingPlaylist)
269 await cleanupUnsavedNormalLive(video, streamingPlaylist)
274 const fullVideo = await VideoModel.loadFull(video.id)
275 return federateVideoIfNeeded(fullVideo, false, undefined)
277 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })