]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
9eba41bf8ca0a3c3ea287aac28a3fe63a9239720
[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 { buildConcatenatedName, cleanupLive, LiveSegmentShaStore } from '@server/lib/live'
7 import { generateVideoMiniature } from '@server/lib/thumbnail'
8 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding'
9 import { publishAndFederateIfNeeded } from '@server/lib/video'
10 import { getHLSDirectory } from '@server/lib/video-paths'
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 { 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 LiveSegmentShaStore.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 // ---------------------------------------------------------------------------
50
51 export {
52 processVideoLiveEnding
53 }
54
55 // ---------------------------------------------------------------------------
56
57 async function saveLive (video: MVideo, live: MVideoLive) {
58 const hlsDirectory = getHLSDirectory(video, false)
59 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
60
61 const rootFiles = await readdir(hlsDirectory)
62
63 const playlistFiles = rootFiles.filter(file => {
64 return file.endsWith('.m3u8') && file !== 'master.m3u8'
65 })
66
67 await cleanupLiveFiles(hlsDirectory)
68
69 await live.destroy()
70
71 video.isLive = false
72 // Reinit views
73 video.views = 0
74 video.state = VideoState.TO_TRANSCODE
75
76 await video.save()
77
78 // Remove old HLS playlist video files
79 const videoWithFiles = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
80
81 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
82 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
83 hlsPlaylist.VideoFiles = []
84
85 let durationDone = false
86
87 for (const playlistFile of playlistFiles) {
88 const concatenatedTsFile = buildConcatenatedName(playlistFile)
89 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
90
91 const probe = await ffprobePromise(concatenatedTsFilePath)
92 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
93
94 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
95
96 const outputPath = await generateHlsPlaylistResolutionFromTS({
97 video: videoWithFiles,
98 concatenatedTsFilePath,
99 resolution: videoFileResolution,
100 isPortraitMode,
101 isAAC: audioStream?.codec_name === 'aac'
102 })
103
104 if (!durationDone) {
105 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
106 await videoWithFiles.save()
107
108 durationDone = true
109 }
110 }
111
112 await remove(replayDirectory)
113
114 // Regenerate the thumbnail & preview?
115 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
116 await generateVideoMiniature({
117 video: videoWithFiles,
118 videoFile: videoWithFiles.getMaxQualityFile(),
119 type: ThumbnailType.MINIATURE
120 })
121 }
122
123 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
124 await generateVideoMiniature({
125 video: videoWithFiles,
126 videoFile: videoWithFiles.getMaxQualityFile(),
127 type: ThumbnailType.PREVIEW
128 })
129 }
130
131 await publishAndFederateIfNeeded(videoWithFiles, true)
132 }
133
134 async function cleanupLiveFiles (hlsDirectory: string) {
135 if (!await pathExists(hlsDirectory)) return
136
137 const files = await readdir(hlsDirectory)
138
139 for (const filename of files) {
140 if (
141 filename.endsWith('.ts') ||
142 filename.endsWith('.m3u8') ||
143 filename.endsWith('.mpd') ||
144 filename.endsWith('.m4s') ||
145 filename.endsWith('.tmp')
146 ) {
147 const p = join(hlsDirectory, filename)
148
149 remove(p)
150 .catch(err => logger.error('Cannot remove %s.', p, { err }))
151 }
152 }
153 }