X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fjob-queue%2Fhandlers%2Fvideo-live-ending.ts;h=8a3ee09a2290210354bedfb8b2895fdadd3ab020;hb=e15bc9dcd59ebb6102d1455450d2e8bbf9f49c27;hp=0e1bfb240fe835c787a01c8d60c2a28c8158d97d;hpb=4fae2b1f300c1f027629569817262f60873a663a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts index 0e1bfb240..8a3ee09a2 100644 --- a/server/lib/job-queue/handlers/video-live-ending.ts +++ b/server/lib/job-queue/handlers/video-live-ending.ts @@ -1,7 +1,7 @@ -import { Job } from 'bull' +import { Job } from 'bullmq' import { readdir, remove } from 'fs-extra' import { join } from 'path' -import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamDuration } from '@server/helpers/ffmpeg' +import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo } from '@server/helpers/ffmpeg' import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' import { cleanupPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live' @@ -30,26 +30,36 @@ async function processVideoLiveEnding (job: Job) { logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags()) } - const liveVideo = await VideoModel.load(payload.videoId) + const video = await VideoModel.load(payload.videoId) const live = await VideoLiveModel.loadByVideoId(payload.videoId) const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId) - if (!liveVideo || !live || !liveSession) { + const permanentLive = live.permanentLive + + if (!video || !live || !liveSession) { logError() return } - if (live.saveReplay !== true) { - return cleanupLiveAndFederate({ live, video: liveVideo, streamingPlaylistId: payload.streamingPlaylistId }) + liveSession.endingProcessed = true + await liveSession.save() + + if (liveSession.saveReplay !== true) { + return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) } - if (live.permanentLive) { - await saveReplayToExternalVideo({ liveVideo, liveSession, publishedAt: payload.publishedAt, replayDirectory: payload.replayDirectory }) + if (permanentLive) { + await saveReplayToExternalVideo({ + liveVideo: video, + liveSession, + publishedAt: payload.publishedAt, + replayDirectory: payload.replayDirectory + }) - return cleanupLiveAndFederate({ live, video: liveVideo, streamingPlaylistId: payload.streamingPlaylistId }) + return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId }) } - return replaceLiveByReplay({ liveVideo, live, liveSession, replayDirectory: payload.replayDirectory }) + return replaceLiveByReplay({ video, liveSession, live, permanentLive, replayDirectory: payload.replayDirectory }) } // --------------------------------------------------------------------------- @@ -68,7 +78,7 @@ async function saveReplayToExternalVideo (options: { }) { const { liveVideo, liveSession, publishedAt, replayDirectory } = options - const video = new VideoModel({ + const replayVideo = new VideoModel({ name: `${liveVideo.name} - ${new Date(publishedAt).toLocaleString()}`, isLive: false, state: VideoState.TO_TRANSCODE, @@ -88,63 +98,64 @@ async function saveReplayToExternalVideo (options: { channelId: liveVideo.channelId }) as MVideoWithAllFiles - video.Thumbnails = [] - video.VideoFiles = [] - video.VideoStreamingPlaylists = [] + replayVideo.Thumbnails = [] + replayVideo.VideoFiles = [] + replayVideo.VideoStreamingPlaylists = [] - video.url = getLocalVideoActivityPubUrl(video) + replayVideo.url = getLocalVideoActivityPubUrl(replayVideo) - await video.save() + await replayVideo.save() - liveSession.replayVideoId = video.id + liveSession.replayVideoId = replayVideo.id await liveSession.save() // If live is blacklisted, also blacklist the replay const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id) if (blacklist) { await VideoBlacklistModel.create({ - videoId: video.id, + videoId: replayVideo.id, unfederated: blacklist.unfederated, reason: blacklist.reason, type: blacklist.type }) } - await assignReplayFilesToVideo({ video, replayDirectory }) + await assignReplayFilesToVideo({ video: replayVideo, replayDirectory }) await remove(replayDirectory) for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) { - const image = await generateVideoMiniature({ video, videoFile: video.getMaxQualityFile(), type }) - await video.addAndSaveThumbnail(image) + const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type }) + await replayVideo.addAndSaveThumbnail(image) } - await moveToNextState({ video, isNewVideo: true }) + await moveToNextState({ video: replayVideo, isNewVideo: true }) } async function replaceLiveByReplay (options: { - liveVideo: MVideo + video: MVideo liveSession: MVideoLiveSession live: MVideoLive + permanentLive: boolean replayDirectory: string }) { - const { liveVideo, liveSession, live, replayDirectory } = options + const { video, liveSession, live, permanentLive, replayDirectory } = options - await cleanupTMPLiveFiles(liveVideo) + await cleanupTMPLiveFiles(video) await live.destroy() - liveVideo.isLive = false - liveVideo.waitTranscoding = true - liveVideo.state = VideoState.TO_TRANSCODE + video.isLive = false + video.waitTranscoding = true + video.state = VideoState.TO_TRANSCODE - await liveVideo.save() + await video.save() - liveSession.replayVideoId = liveVideo.id + liveSession.replayVideoId = video.id await liveSession.save() // Remove old HLS playlist video files - const videoWithFiles = await VideoModel.loadFull(liveVideo.id) + const videoWithFiles = await VideoModel.loadFull(video.id) const hlsPlaylist = videoWithFiles.getHLSPlaylist() await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) @@ -157,7 +168,7 @@ async function replaceLiveByReplay (options: { await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory }) - if (live.permanentLive) { // Remove session replay + if (permanentLive) { // Remove session replay await remove(replayDirectory) } else { // We won't stream again in this live, we can delete the base replay directory await remove(getLiveReplayBaseDirectory(videoWithFiles)) @@ -192,8 +203,6 @@ async function assignReplayFilesToVideo (options: { }) { const { video, replayDirectory } = options - let durationDone = false - const concatenatedTsFiles = await readdir(replayDirectory) for (const concatenatedTsFile of concatenatedTsFiles) { @@ -201,39 +210,30 @@ async function assignReplayFilesToVideo (options: { const probe = await ffprobePromise(concatenatedTsFilePath) const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) + const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe) - const { resolution, isPortraitMode } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe) - - const { resolutionPlaylistPath: outputPath } = await generateHlsPlaylistResolutionFromTS({ + await generateHlsPlaylistResolutionFromTS({ video, concatenatedTsFilePath, resolution, - isPortraitMode, isAAC: audioStream?.codec_name === 'aac' }) - - if (!durationDone) { - video.duration = await getVideoStreamDuration(outputPath) - await video.save() - - durationDone = true - } } return video } async function cleanupLiveAndFederate (options: { - live: MVideoLive video: MVideo + permanentLive: boolean streamingPlaylistId: number }) { - const { live, video, streamingPlaylistId } = options + const { permanentLive, video, streamingPlaylistId } = options const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId) if (streamingPlaylist) { - if (live.permanentLive) { + if (permanentLive) { await cleanupPermanentLive(video, streamingPlaylist) } else { await cleanupUnsavedNormalLive(video, streamingPlaylist)