1 import * as Bull from 'bull'
2 import { pathExists, readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { ffprobePromise, getAudioStream, getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
5 import { VIDEO_LIVE } from '@server/initializers/constants'
6 import { buildConcatenatedName, cleanupLive, LiveSegmentShaStore } from '@server/lib/live'
7 import { generateVideoMiniature } from '@server/lib/thumbnail'
8 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding'
9 import { publishAndFederateIfNeeded } from '@server/lib/video'
10 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHLSDirectory } from '@server/lib/video-paths'
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 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
42 if (live.saveReplay !== true) {
43 return cleanupLive(video, streamingPlaylist)
46 return saveLive(video, live, streamingPlaylist)
49 // ---------------------------------------------------------------------------
52 processVideoLiveEnding
55 // ---------------------------------------------------------------------------
57 async function saveLive (video: MVideo, live: MVideoLive, streamingPlaylist: MStreamingPlaylist) {
58 const hlsDirectory = getHLSDirectory(video, false)
59 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
61 const rootFiles = await readdir(hlsDirectory)
63 const playlistFiles = rootFiles.filter(file => {
64 return file.endsWith('.m3u8') && file !== streamingPlaylist.playlistFilename
67 await cleanupLiveFiles(hlsDirectory)
74 video.state = VideoState.TO_TRANSCODE
78 // Remove old HLS playlist video files
79 const videoWithFiles = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
81 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
82 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
85 hlsPlaylist.VideoFiles = []
86 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
87 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
88 await hlsPlaylist.save()
90 let durationDone = false
92 for (const playlistFile of playlistFiles) {
93 const concatenatedTsFile = buildConcatenatedName(playlistFile)
94 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
96 const probe = await ffprobePromise(concatenatedTsFilePath)
97 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
99 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
101 const outputPath = await generateHlsPlaylistResolutionFromTS({
102 video: videoWithFiles,
103 concatenatedTsFilePath,
104 resolution: videoFileResolution,
106 isAAC: audioStream?.codec_name === 'aac'
110 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
111 await videoWithFiles.save()
117 await remove(replayDirectory)
119 // Regenerate the thumbnail & preview?
120 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
121 await generateVideoMiniature({
122 video: videoWithFiles,
123 videoFile: videoWithFiles.getMaxQualityFile(),
124 type: ThumbnailType.MINIATURE
128 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
129 await generateVideoMiniature({
130 video: videoWithFiles,
131 videoFile: videoWithFiles.getMaxQualityFile(),
132 type: ThumbnailType.PREVIEW
136 await publishAndFederateIfNeeded(videoWithFiles, true)
139 async function cleanupLiveFiles (hlsDirectory: string) {
140 if (!await pathExists(hlsDirectory)) return
142 const files = await readdir(hlsDirectory)
144 for (const filename of files) {
146 filename.endsWith('.ts') ||
147 filename.endsWith('.m3u8') ||
148 filename.endsWith('.mpd') ||
149 filename.endsWith('.m4s') ||
150 filename.endsWith('.tmp')
152 const p = join(hlsDirectory, filename)
155 .catch(err => logger.error('Cannot remove %s.', p, { err }))