]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
Export encoders options in a dedicated struct
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
1 import * as Bull from 'bull'
2 import { 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 { generateVideoMiniature } from '@server/lib/thumbnail'
7 import { publishAndFederateIfNeeded } from '@server/lib/video'
8 import { getHLSDirectory } from '@server/lib/video-paths'
9 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
10 import { VideoModel } from '@server/models/video/video'
11 import { VideoFileModel } from '@server/models/video/video-file'
12 import { VideoLiveModel } from '@server/models/video/video-live'
13 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
14 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
15 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
16 import { logger } from '../../../helpers/logger'
17
18 async function processVideoLiveEnding (job: Bull.Job) {
19 const payload = job.data as VideoLiveEndingPayload
20
21 function logError () {
22 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
23 }
24
25 const video = await VideoModel.load(payload.videoId)
26 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
27
28 if (!video || !live) {
29 logError()
30 return
31 }
32
33 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
34 if (!streamingPlaylist) {
35 logError()
36 return
37 }
38
39 if (live.saveReplay !== true) {
40 return cleanupLive(video, streamingPlaylist)
41 }
42
43 return saveLive(video, live)
44 }
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 processVideoLiveEnding
50 }
51
52 // ---------------------------------------------------------------------------
53
54 async function saveLive (video: MVideo, live: MVideoLive) {
55 const hlsDirectory = getHLSDirectory(video, false)
56 const files = await readdir(hlsDirectory)
57
58 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
59 const resolutions: number[] = []
60 let duration: number
61
62 for (const playlistFile of playlistFiles) {
63 const playlistPath = join(hlsDirectory, playlistFile)
64 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
65
66 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
67
68 // Playlist name is for example 3.m3u8
69 // Segments names are 3-0.ts 3-1.ts etc
70 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
71
72 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
73 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
74
75 if (!duration) {
76 duration = await getDurationFromVideoFile(mp4TmpPath)
77 }
78
79 resolutions.push(videoFileResolution)
80 }
81
82 await cleanupLiveFiles(hlsDirectory)
83
84 await live.destroy()
85
86 video.isLive = false
87 // Reinit views
88 video.views = 0
89 video.state = VideoState.TO_TRANSCODE
90 video.duration = duration
91
92 await video.save()
93
94 // Remove old HLS playlist video files
95 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
96
97 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
98 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
99 hlsPlaylist.VideoFiles = []
100
101 for (const resolution of resolutions) {
102 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
103 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
104
105 await generateHlsPlaylist({
106 video: videoWithFiles,
107 videoInputPath,
108 resolution: resolution,
109 copyCodecs: true,
110 isPortraitMode
111 })
112
113 await remove(videoInputPath)
114 }
115
116 // Regenerate the thumbnail & preview?
117 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
118 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
119 }
120
121 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
122 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
123 }
124
125 await publishAndFederateIfNeeded(video, true)
126 }
127
128 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
129 const hlsDirectory = getHLSDirectory(video)
130
131 await remove(hlsDirectory)
132
133 streamingPlaylist.destroy()
134 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
135 }
136
137 async function cleanupLiveFiles (hlsDirectory: string) {
138 const files = await readdir(hlsDirectory)
139
140 for (const filename of files) {
141 if (
142 filename.endsWith('.ts') ||
143 filename.endsWith('.m3u8') ||
144 filename.endsWith('.mpd') ||
145 filename.endsWith('.m4s') ||
146 filename.endsWith('.tmp')
147 ) {
148 const p = join(hlsDirectory, filename)
149
150 remove(p)
151 .catch(err => logger.error('Cannot remove %s.', p, { err }))
152 }
153 }
154 }
155
156 function buildMP4TmpPath (basePath: string, resolution: number) {
157 return join(basePath, resolution + '-tmp.mp4')
158 }