]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
6a675b0ef505e9c0850deda6306f4d363bb87a4c
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
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 { LiveManager } from '@server/lib/live-manager'
7 import { generateVideoMiniature } from '@server/lib/thumbnail'
8 import { publishAndFederateIfNeeded } from '@server/lib/video'
9 import { getHLSDirectory } from '@server/lib/video-paths'
10 import { generateHlsPlaylistFromTS } from '@server/lib/video-transcoding'
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'
18
19 async function processVideoLiveEnding (job: Bull.Job) {
20 const payload = job.data as VideoLiveEndingPayload
21
22 function logError () {
23 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
24 }
25
26 const video = await VideoModel.load(payload.videoId)
27 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
28
29 if (!video || !live) {
30 logError()
31 return
32 }
33
34 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
35 if (!streamingPlaylist) {
36 logError()
37 return
38 }
39
40 LiveManager.Instance.cleanupShaSegments(video.uuid)
41
42 if (live.saveReplay !== true) {
43 return cleanupLive(video, streamingPlaylist)
44 }
45
46 return saveLive(video, live)
47 }
48
49 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
50 const hlsDirectory = getHLSDirectory(video)
51
52 await remove(hlsDirectory)
53
54 await streamingPlaylist.destroy()
55 }
56
57 // ---------------------------------------------------------------------------
58
59 export {
60 processVideoLiveEnding,
61 cleanupLive
62 }
63
64 // ---------------------------------------------------------------------------
65
66 async function saveLive (video: MVideo, live: MVideoLive) {
67 const hlsDirectory = getHLSDirectory(video, false)
68 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
69
70 const rootFiles = await readdir(hlsDirectory)
71
72 const playlistFiles: string[] = []
73
74 for (const file of rootFiles) {
75 // Move remaining files in the replay directory
76 if (file.endsWith('.ts')) {
77 await LiveManager.Instance.addSegmentToReplay(hlsDirectory, join(hlsDirectory, file))
78 }
79
80 if (file.endsWith('.m3u8') && file !== 'master.m3u8') {
81 playlistFiles.push(file)
82 }
83 }
84
85 await cleanupLiveFiles(hlsDirectory)
86
87 await live.destroy()
88
89 video.isLive = false
90 // Reinit views
91 video.views = 0
92 video.state = VideoState.TO_TRANSCODE
93
94 await video.save()
95
96 // Remove old HLS playlist video files
97 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
98
99 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
100 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
101 hlsPlaylist.VideoFiles = []
102
103 let durationDone: boolean
104
105 for (const playlistFile of playlistFiles) {
106 const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile)
107 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
108
109 const probe = await ffprobePromise(concatenatedTsFilePath)
110 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
111
112 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
113
114 const outputPath = await generateHlsPlaylistFromTS({
115 video: videoWithFiles,
116 concatenatedTsFilePath,
117 resolution: videoFileResolution,
118 isPortraitMode,
119 isAAC: audioStream?.codec_name === 'aac'
120 })
121
122 if (!durationDone) {
123 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
124 await videoWithFiles.save()
125
126 durationDone = true
127 }
128 }
129
130 await remove(replayDirectory)
131
132 // Regenerate the thumbnail & preview?
133 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
134 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
135 }
136
137 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
138 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
139 }
140
141 await publishAndFederateIfNeeded(videoWithFiles, true)
142 }
143
144 async function cleanupLiveFiles (hlsDirectory: string) {
145 if (!await pathExists(hlsDirectory)) return
146
147 const files = await readdir(hlsDirectory)
148
149 for (const filename of files) {
150 if (
151 filename.endsWith('.ts') ||
152 filename.endsWith('.m3u8') ||
153 filename.endsWith('.mpd') ||
154 filename.endsWith('.m4s') ||
155 filename.endsWith('.tmp')
156 ) {
157 const p = join(hlsDirectory, filename)
158
159 remove(p)
160 .catch(err => logger.error('Cannot remove %s.', p, { err }))
161 }
162 }
163 }