]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
esModuleInterop to true
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
1 import { Job } 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 { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveDirectory } from '@server/lib/paths'
8 import { generateVideoMiniature } from '@server/lib/thumbnail'
9 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding'
10 import { VideoPathManager } from '@server/lib/video-path-manager'
11 import { moveToNextState } from '@server/lib/video-state'
12 import { VideoModel } from '@server/models/video/video'
13 import { VideoFileModel } from '@server/models/video/video-file'
14 import { VideoLiveModel } from '@server/models/video/video-live'
15 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
16 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
17 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
18 import { logger } from '../../../helpers/logger'
19
20 async function processVideoLiveEnding (job: Job) {
21 const payload = job.data as VideoLiveEndingPayload
22
23 function logError () {
24 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
25 }
26
27 const video = await VideoModel.load(payload.videoId)
28 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
29
30 if (!video || !live) {
31 logError()
32 return
33 }
34
35 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
36 if (!streamingPlaylist) {
37 logError()
38 return
39 }
40
41 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
42
43 if (live.saveReplay !== true) {
44 return cleanupLive(video, streamingPlaylist)
45 }
46
47 return saveLive(video, live, streamingPlaylist)
48 }
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 processVideoLiveEnding
54 }
55
56 // ---------------------------------------------------------------------------
57
58 async function saveLive (video: MVideo, live: MVideoLive, streamingPlaylist: MStreamingPlaylist) {
59 const replayDirectory = VideoPathManager.Instance.getFSHLSOutputPath(video, VIDEO_LIVE.REPLAY_DIRECTORY)
60
61 const rootFiles = await readdir(getLiveDirectory(video))
62
63 const playlistFiles = rootFiles.filter(file => {
64 return file.endsWith('.m3u8') && file !== streamingPlaylist.playlistFilename
65 })
66
67 await cleanupTMPLiveFiles(getLiveDirectory(video))
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
84 // Reset playlist
85 hlsPlaylist.VideoFiles = []
86 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
87 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
88 await hlsPlaylist.save()
89
90 let durationDone = false
91
92 for (const playlistFile of playlistFiles) {
93 const concatenatedTsFile = buildConcatenatedName(playlistFile)
94 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
95
96 const probe = await ffprobePromise(concatenatedTsFilePath)
97 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
98
99 const { resolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
100
101 const { resolutionPlaylistPath: outputPath } = await generateHlsPlaylistResolutionFromTS({
102 video: videoWithFiles,
103 concatenatedTsFilePath,
104 resolution,
105 isPortraitMode,
106 isAAC: audioStream?.codec_name === 'aac'
107 })
108
109 if (!durationDone) {
110 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
111 await videoWithFiles.save()
112
113 durationDone = true
114 }
115 }
116
117 await remove(replayDirectory)
118
119 // Regenerate the thumbnail & preview?
120 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
121 await generateVideoMiniature({
122 video: videoWithFiles,
123 videoFile: videoWithFiles.getMaxQualityFile(),
124 type: ThumbnailType.MINIATURE
125 })
126 }
127
128 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
129 await generateVideoMiniature({
130 video: videoWithFiles,
131 videoFile: videoWithFiles.getMaxQualityFile(),
132 type: ThumbnailType.PREVIEW
133 })
134 }
135
136 await moveToNextState(videoWithFiles, false)
137 }
138
139 async function cleanupTMPLiveFiles (hlsDirectory: string) {
140 if (!await pathExists(hlsDirectory)) return
141
142 const files = await readdir(hlsDirectory)
143
144 for (const filename of files) {
145 if (
146 filename.endsWith('.ts') ||
147 filename.endsWith('.m3u8') ||
148 filename.endsWith('.mpd') ||
149 filename.endsWith('.m4s') ||
150 filename.endsWith('.tmp')
151 ) {
152 const p = join(hlsDirectory, filename)
153
154 remove(p)
155 .catch(err => logger.error('Cannot remove %s.', p, { err }))
156 }
157 }
158 }