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