1 import * as Bull from 'bull'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { getHLSDirectory } from '@server/lib/video-paths'
6 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
7 import { VideoModel } from '@server/models/video/video'
8 import { VideoLiveModel } from '@server/models/video/video-live'
9 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
10 import { MStreamingPlaylist, MVideo } from '@server/types/models'
11 import { VideoLiveEndingPayload, VideoState } from '@shared/models'
12 import { logger } from '../../../helpers/logger'
14 async function processVideoLiveEnding (job: Bull.Job) {
15 const payload = job.data as VideoLiveEndingPayload
17 const video = await VideoModel.load(payload.videoId)
18 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
20 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
21 if (!video || !streamingPlaylist || !live) {
22 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
26 if (live.saveReplay !== true) {
27 return cleanupLive(video, streamingPlaylist)
30 return saveLive(video, streamingPlaylist)
33 // ---------------------------------------------------------------------------
36 processVideoLiveEnding
39 // ---------------------------------------------------------------------------
41 async function saveLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
42 const videoFiles = await streamingPlaylist.get('VideoFiles')
43 const hlsDirectory = getHLSDirectory(video, false)
45 for (const videoFile of videoFiles) {
46 const playlistPath = join(hlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(videoFile.resolution))
48 const mp4TmpName = buildMP4TmpName(videoFile.resolution)
49 await hlsPlaylistToFragmentedMP4(playlistPath, mp4TmpName)
52 await cleanupLiveFiles(hlsDirectory)
55 video.state = VideoState.TO_TRANSCODE
58 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
60 for (const videoFile of videoFiles) {
61 const videoInputPath = buildMP4TmpName(videoFile.resolution)
62 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
64 await generateHlsPlaylist({
65 video: videoWithFiles,
67 resolution: videoFile.resolution,
73 video.state = VideoState.PUBLISHED
77 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
78 const hlsDirectory = getHLSDirectory(video, false)
80 await cleanupLiveFiles(hlsDirectory)
82 streamingPlaylist.destroy()
83 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
86 async function cleanupLiveFiles (hlsDirectory: string) {
87 const files = await readdir(hlsDirectory)
89 for (const filename of files) {
91 filename.endsWith('.ts') ||
92 filename.endsWith('.m3u8') ||
93 filename.endsWith('.mpd') ||
94 filename.endsWith('.m4s') ||
95 filename.endsWith('.tmp')
97 const p = join(hlsDirectory, filename)
100 .catch(err => logger.error('Cannot remove %s.', p, { err }))
105 function buildMP4TmpName (resolution: number) {
106 return resolution + 'tmp.mp4'