]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-live-ending.ts
Fix duplicate ids in admin config form
[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
C
4import { hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
8c666c44 6import { VIDEO_LIVE } from '@server/initializers/constants'
daf6e480 7import { generateVideoMiniature } from '@server/lib/thumbnail'
d846d99c 8import { publishAndFederateIfNeeded } from '@server/lib/video'
a5cf76af 9import { getHLSDirectory } from '@server/lib/video-paths'
b5b68755 10import { generateHlsPlaylist } from '@server/lib/video-transcoding'
a5cf76af 11import { VideoModel } from '@server/models/video/video'
68e70a74 12import { VideoFileModel } from '@server/models/video/video-file'
b5b68755 13import { VideoLiveModel } from '@server/models/video/video-live'
a5cf76af 14import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
68e70a74 15import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
053aed43 16import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
a5cf76af
C
17import { logger } from '../../../helpers/logger'
18
19async function processVideoLiveEnding (job: Bull.Job) {
20 const payload = job.data as VideoLiveEndingPayload
21
68e70a74
C
22 function logError () {
23 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
24 }
25
b5b68755
C
26 const video = await VideoModel.load(payload.videoId)
27 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
28
68e70a74
C
29 if (!video || !live) {
30 logError()
31 return
32 }
33
b5b68755 34 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
68e70a74
C
35 if (!streamingPlaylist) {
36 logError()
a5cf76af
C
37 return
38 }
39
b5b68755
C
40 if (live.saveReplay !== true) {
41 return cleanupLive(video, streamingPlaylist)
42 }
43
31c82cd9 44 return saveLive(video, live)
b5b68755
C
45}
46
47// ---------------------------------------------------------------------------
48
49export {
50 processVideoLiveEnding
51}
52
53// ---------------------------------------------------------------------------
54
31c82cd9 55async function saveLive (video: MVideo, live: MVideoLive) {
b5b68755 56 const hlsDirectory = getHLSDirectory(video, false)
937581b8
C
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) {
8c666c44
C
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 }
937581b8 68
8c666c44 69 if (file.endsWith('.m3u8') && file !== 'master.m3u8') {
937581b8
C
70 playlistFiles.push(file)
71 }
72 }
73
74 const replayFiles = await readdir(replayDirectory)
31c82cd9 75
31c82cd9 76 const resolutions: number[] = []
d846d99c 77 let duration: number
31c82cd9
C
78
79 for (const playlistFile of playlistFiles) {
937581b8 80 const playlistPath = join(replayDirectory, playlistFile)
31c82cd9 81 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
b5b68755 82
937581b8 83 // Put the final mp4 in the hls directory, and not in the replay directory
68e70a74 84 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
b5b68755 85
31c82cd9
C
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
937581b8
C
90 const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
91 await hlsPlaylistToFragmentedMP4(replayDirectory, segmentFiles, mp4TmpPath)
31c82cd9 92
d846d99c 93 if (!duration) {
68e70a74 94 duration = await getDurationFromVideoFile(mp4TmpPath)
d846d99c
C
95 }
96
31c82cd9 97 resolutions.push(videoFileResolution)
b5b68755
C
98 }
99
100 await cleanupLiveFiles(hlsDirectory)
101
31c82cd9
C
102 await live.destroy()
103
b5b68755 104 video.isLive = false
e4bf7856
C
105 // Reinit views
106 video.views = 0
b5b68755 107 video.state = VideoState.TO_TRANSCODE
d846d99c
C
108 video.duration = duration
109
b5b68755
C
110 await video.save()
111
97969c4e 112 // Remove old HLS playlist video files
b5b68755
C
113 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
114
97969c4e
C
115 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
116 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
117 hlsPlaylist.VideoFiles = []
118
31c82cd9 119 for (const resolution of resolutions) {
68e70a74 120 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
b5b68755
C
121 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
122
123 await generateHlsPlaylist({
124 video: videoWithFiles,
125 videoInputPath,
31c82cd9 126 resolution: resolution,
b5b68755
C
127 copyCodecs: true,
128 isPortraitMode
129 })
b5b68755 130
68e70a74 131 await remove(videoInputPath)
97969c4e 132 }
d846d99c 133
053aed43
C
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
97969c4e 143 await publishAndFederateIfNeeded(video, true)
b5b68755
C
144}
145
146async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
9e2b2e76 147 const hlsDirectory = getHLSDirectory(video)
a5cf76af 148
68e70a74 149 await remove(hlsDirectory)
b5b68755
C
150
151 streamingPlaylist.destroy()
152 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
153}
154
155async function cleanupLiveFiles (hlsDirectory: string) {
a5cf76af
C
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') ||
937581b8
C
164 filename.endsWith('.tmp') ||
165 filename === VIDEO_LIVE.REPLAY_DIRECTORY
a5cf76af
C
166 ) {
167 const p = join(hlsDirectory, filename)
168
169 remove(p)
170 .catch(err => logger.error('Cannot remove %s.', p, { err }))
171 }
172 }
a5cf76af
C
173}
174
68e70a74
C
175function buildMP4TmpPath (basePath: string, resolution: number) {
176 return join(basePath, resolution + '-tmp.mp4')
a5cf76af 177}