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, MVideoLive } 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, live)
33 // ---------------------------------------------------------------------------
36 processVideoLiveEnding
39 // ---------------------------------------------------------------------------
41 async function saveLive (video: MVideo, live: MVideoLive) {
42 const hlsDirectory = getHLSDirectory(video, false)
43 const files = await readdir(hlsDirectory)
45 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
46 const resolutions: number[] = []
48 for (const playlistFile of playlistFiles) {
49 const playlistPath = join(hlsDirectory, playlistFile)
50 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
52 const mp4TmpName = buildMP4TmpName(videoFileResolution)
54 // Playlist name is for example 3.m3u8
55 // Segments names are 3-0.ts 3-1.ts etc
56 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
58 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
59 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpName)
61 resolutions.push(videoFileResolution)
64 await cleanupLiveFiles(hlsDirectory)
69 video.state = VideoState.TO_TRANSCODE
72 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
74 for (const resolution of resolutions) {
75 const videoInputPath = buildMP4TmpName(resolution)
76 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
78 await generateHlsPlaylist({
79 video: videoWithFiles,
81 resolution: resolution,
87 video.state = VideoState.PUBLISHED
91 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
92 const hlsDirectory = getHLSDirectory(video, false)
94 await cleanupLiveFiles(hlsDirectory)
96 streamingPlaylist.destroy()
97 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
100 async function cleanupLiveFiles (hlsDirectory: string) {
101 const files = await readdir(hlsDirectory)
103 for (const filename of files) {
105 filename.endsWith('.ts') ||
106 filename.endsWith('.m3u8') ||
107 filename.endsWith('.mpd') ||
108 filename.endsWith('.m4s') ||
109 filename.endsWith('.tmp')
111 const p = join(hlsDirectory, filename)
114 .catch(err => logger.error('Cannot remove %s.', p, { err }))
119 function buildMP4TmpName (resolution: number) {
120 return resolution + '-tmp.mp4'