1 import * as Bull from 'bull'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { publishAndFederateIfNeeded } from '@server/lib/video'
6 import { getHLSDirectory } from '@server/lib/video-paths'
7 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
8 import { VideoModel } from '@server/models/video/video'
9 import { VideoFileModel } from '@server/models/video/video-file'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
12 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
13 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
14 import { logger } from '../../../helpers/logger'
15 import { generateVideoMiniature } from '@server/lib/thumbnail'
17 async function processVideoLiveEnding (job: Bull.Job) {
18 const payload = job.data as VideoLiveEndingPayload
20 function logError () {
21 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
24 const video = await VideoModel.load(payload.videoId)
25 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
27 if (!video || !live) {
32 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
33 if (!streamingPlaylist) {
38 if (live.saveReplay !== true) {
39 return cleanupLive(video, streamingPlaylist)
42 return saveLive(video, live)
45 // ---------------------------------------------------------------------------
48 processVideoLiveEnding
51 // ---------------------------------------------------------------------------
53 async function saveLive (video: MVideo, live: MVideoLive) {
54 const hlsDirectory = getHLSDirectory(video, false)
55 const files = await readdir(hlsDirectory)
57 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
58 const resolutions: number[] = []
61 for (const playlistFile of playlistFiles) {
62 const playlistPath = join(hlsDirectory, playlistFile)
63 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
65 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
67 // Playlist name is for example 3.m3u8
68 // Segments names are 3-0.ts 3-1.ts etc
69 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
71 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
72 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
75 duration = await getDurationFromVideoFile(mp4TmpPath)
78 resolutions.push(videoFileResolution)
81 await cleanupLiveFiles(hlsDirectory)
86 video.state = VideoState.TO_TRANSCODE
87 video.duration = duration
91 // Remove old HLS playlist video files
92 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
94 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
95 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
96 hlsPlaylist.VideoFiles = []
98 for (const resolution of resolutions) {
99 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
100 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
102 await generateHlsPlaylist({
103 video: videoWithFiles,
105 resolution: resolution,
110 await remove(videoInputPath)
113 // Regenerate the thumbnail & preview?
114 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
115 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
118 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
119 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
122 await publishAndFederateIfNeeded(video, true)
125 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
126 const hlsDirectory = getHLSDirectory(video)
128 await remove(hlsDirectory)
130 streamingPlaylist.destroy()
131 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
134 async function cleanupLiveFiles (hlsDirectory: string) {
135 const files = await readdir(hlsDirectory)
137 for (const filename of files) {
139 filename.endsWith('.ts') ||
140 filename.endsWith('.m3u8') ||
141 filename.endsWith('.mpd') ||
142 filename.endsWith('.m4s') ||
143 filename.endsWith('.tmp')
145 const p = join(hlsDirectory, filename)
148 .catch(err => logger.error('Cannot remove %s.', p, { err }))
153 function buildMP4TmpPath (basePath: string, resolution: number) {
154 return join(basePath, resolution + '-tmp.mp4')