]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
a5cf76af
C
1import * as Bull from 'bull'
2import { readdir, remove } from 'fs-extra'
3import { join } from 'path'
daf6e480
C
4import { hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
6import { generateVideoMiniature } from '@server/lib/thumbnail'
d846d99c 7import { publishAndFederateIfNeeded } from '@server/lib/video'
a5cf76af 8import { getHLSDirectory } from '@server/lib/video-paths'
b5b68755 9import { generateHlsPlaylist } 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)
31c82cd9
C
56 const files = await readdir(hlsDirectory)
57
58 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
59 const resolutions: number[] = []
d846d99c 60 let duration: number
31c82cd9
C
61
62 for (const playlistFile of playlistFiles) {
63 const playlistPath = join(hlsDirectory, playlistFile)
64 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
b5b68755 65
68e70a74 66 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
b5b68755 67
31c82cd9
C
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'))
68e70a74 73 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
31c82cd9 74
d846d99c 75 if (!duration) {
68e70a74 76 duration = await getDurationFromVideoFile(mp4TmpPath)
d846d99c
C
77 }
78
31c82cd9 79 resolutions.push(videoFileResolution)
b5b68755
C
80 }
81
82 await cleanupLiveFiles(hlsDirectory)
83
31c82cd9
C
84 await live.destroy()
85
b5b68755 86 video.isLive = false
e4bf7856
C
87 // Reinit views
88 video.views = 0
b5b68755 89 video.state = VideoState.TO_TRANSCODE
d846d99c
C
90 video.duration = duration
91
b5b68755
C
92 await video.save()
93
97969c4e 94 // Remove old HLS playlist video files
b5b68755
C
95 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
96
97969c4e
C
97 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
98 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
99 hlsPlaylist.VideoFiles = []
100
31c82cd9 101 for (const resolution of resolutions) {
68e70a74 102 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
b5b68755
C
103 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
104
105 await generateHlsPlaylist({
106 video: videoWithFiles,
107 videoInputPath,
31c82cd9 108 resolution: resolution,
b5b68755
C
109 copyCodecs: true,
110 isPortraitMode
111 })
b5b68755 112
68e70a74 113 await remove(videoInputPath)
97969c4e 114 }
d846d99c 115
053aed43
C
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
97969c4e 125 await publishAndFederateIfNeeded(video, true)
b5b68755
C
126}
127
128async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
9e2b2e76 129 const hlsDirectory = getHLSDirectory(video)
a5cf76af 130
68e70a74 131 await remove(hlsDirectory)
b5b68755
C
132
133 streamingPlaylist.destroy()
134 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
135}
136
137async function cleanupLiveFiles (hlsDirectory: string) {
a5cf76af
C
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 }
a5cf76af
C
154}
155
68e70a74
C
156function buildMP4TmpPath (basePath: string, resolution: number) {
157 return join(basePath, resolution + '-tmp.mp4')
a5cf76af 158}