]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
6e1076d8f2340767a2d73f31779f38e98ca663cb
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
1 import * as Bull from 'bull'
2 import { copy, readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
6 import { VIDEO_LIVE } from '@server/initializers/constants'
7 import { generateVideoMiniature } from '@server/lib/thumbnail'
8 import { publishAndFederateIfNeeded } from '@server/lib/video'
9 import { getHLSDirectory } from '@server/lib/video-paths'
10 import { generateHlsPlaylist } 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 if (live.saveReplay !== true) {
41 return cleanupLive(video, streamingPlaylist)
42 }
43
44 return saveLive(video, live)
45 }
46
47 // ---------------------------------------------------------------------------
48
49 export {
50 processVideoLiveEnding
51 }
52
53 // ---------------------------------------------------------------------------
54
55 async function saveLive (video: MVideo, live: MVideoLive) {
56 const hlsDirectory = getHLSDirectory(video, false)
57 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
58
59 const rootFiles = await readdir(hlsDirectory)
60
61 const playlistFiles: string[] = []
62
63 for (const file of rootFiles) {
64 // Move remaining files in the replay directory
65 if (file.endsWith('.ts') || file.endsWith('.m3u8')) {
66 await copy(join(hlsDirectory, file), join(replayDirectory, file))
67 }
68
69 if (file.endsWith('.m3u8') && file !== 'master.m3u8') {
70 playlistFiles.push(file)
71 }
72 }
73
74 const replayFiles = await readdir(replayDirectory)
75
76 const resolutions: number[] = []
77 let duration: number
78
79 for (const playlistFile of playlistFiles) {
80 const playlistPath = join(replayDirectory, playlistFile)
81 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
82
83 // Put the final mp4 in the hls directory, and not in the replay directory
84 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
85
86 // Playlist name is for example 3.m3u8
87 // Segments names are 3-0.ts 3-1.ts etc
88 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
89
90 const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
91 await hlsPlaylistToFragmentedMP4(replayDirectory, segmentFiles, mp4TmpPath)
92
93 if (!duration) {
94 duration = await getDurationFromVideoFile(mp4TmpPath)
95 }
96
97 resolutions.push(videoFileResolution)
98 }
99
100 await cleanupLiveFiles(hlsDirectory)
101
102 await live.destroy()
103
104 video.isLive = false
105 // Reinit views
106 video.views = 0
107 video.state = VideoState.TO_TRANSCODE
108 video.duration = duration
109
110 await video.save()
111
112 // Remove old HLS playlist video files
113 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
114
115 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
116 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
117 hlsPlaylist.VideoFiles = []
118
119 for (const resolution of resolutions) {
120 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
121 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
122
123 await generateHlsPlaylist({
124 video: videoWithFiles,
125 videoInputPath,
126 resolution: resolution,
127 copyCodecs: true,
128 isPortraitMode
129 })
130
131 await remove(videoInputPath)
132 }
133
134 // Regenerate the thumbnail & preview?
135 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
136 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
137 }
138
139 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
140 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
141 }
142
143 await publishAndFederateIfNeeded(video, true)
144 }
145
146 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
147 const hlsDirectory = getHLSDirectory(video)
148
149 await remove(hlsDirectory)
150
151 streamingPlaylist.destroy()
152 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
153 }
154
155 async function cleanupLiveFiles (hlsDirectory: string) {
156 const files = await readdir(hlsDirectory)
157
158 for (const filename of files) {
159 if (
160 filename.endsWith('.ts') ||
161 filename.endsWith('.m3u8') ||
162 filename.endsWith('.mpd') ||
163 filename.endsWith('.m4s') ||
164 filename.endsWith('.tmp') ||
165 filename === VIDEO_LIVE.REPLAY_DIRECTORY
166 ) {
167 const p = join(hlsDirectory, filename)
168
169 remove(p)
170 .catch(err => logger.error('Cannot remove %s.', p, { err }))
171 }
172 }
173 }
174
175 function buildMP4TmpPath (basePath: string, resolution: number) {
176 return join(basePath, resolution + '-tmp.mp4')
177 }