]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
2b900998a9c7da05a3927b88a999b9e7d83915a5
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
1 import * as Bull from 'bull'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5 import { publishAndFederateIfNeeded } from '@server/lib/video'
6 import { getHLSDirectory } from '@server/lib/video-paths'
7 import { generateHlsPlaylist } from '@server/lib/video-transcoding'
8 import { VideoModel } from '@server/models/video/video'
9 import { VideoFileModel } from '@server/models/video/video-file'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
12 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
13 import { VideoLiveEndingPayload, VideoState } from '@shared/models'
14 import { logger } from '../../../helpers/logger'
15
16 async function processVideoLiveEnding (job: Bull.Job) {
17 const payload = job.data as VideoLiveEndingPayload
18
19 function logError () {
20 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
21 }
22
23 const video = await VideoModel.load(payload.videoId)
24 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
25
26 if (!video || !live) {
27 logError()
28 return
29 }
30
31 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
32 if (!streamingPlaylist) {
33 logError()
34 return
35 }
36
37 if (live.saveReplay !== true) {
38 return cleanupLive(video, streamingPlaylist)
39 }
40
41 return saveLive(video, live)
42 }
43
44 // ---------------------------------------------------------------------------
45
46 export {
47 processVideoLiveEnding
48 }
49
50 // ---------------------------------------------------------------------------
51
52 async function saveLive (video: MVideo, live: MVideoLive) {
53 const hlsDirectory = getHLSDirectory(video, false)
54 const files = await readdir(hlsDirectory)
55
56 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
57 const resolutions: number[] = []
58 let duration: number
59
60 for (const playlistFile of playlistFiles) {
61 const playlistPath = join(hlsDirectory, playlistFile)
62 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
63
64 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
65
66 // Playlist name is for example 3.m3u8
67 // Segments names are 3-0.ts 3-1.ts etc
68 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
69
70 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
71 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
72
73 for (const file of segmentFiles) {
74 await remove(join(hlsDirectory, file))
75 }
76
77 if (!duration) {
78 duration = await getDurationFromVideoFile(mp4TmpPath)
79 }
80
81 resolutions.push(videoFileResolution)
82 }
83
84 await cleanupLiveFiles(hlsDirectory)
85
86 await live.destroy()
87
88 video.isLive = false
89 video.state = VideoState.TO_TRANSCODE
90 video.duration = duration
91
92 await video.save()
93
94 // Remove old HLS playlist video files
95 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
96
97 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
98 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
99 hlsPlaylist.VideoFiles = []
100
101 for (const resolution of resolutions) {
102 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
103 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
104
105 await generateHlsPlaylist({
106 video: videoWithFiles,
107 videoInputPath,
108 resolution: resolution,
109 copyCodecs: true,
110 isPortraitMode
111 })
112
113 await remove(videoInputPath)
114 }
115
116 await publishAndFederateIfNeeded(video, true)
117 }
118
119 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
120 const hlsDirectory = getHLSDirectory(video, false)
121
122 await remove(hlsDirectory)
123
124 streamingPlaylist.destroy()
125 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
126 }
127
128 async function cleanupLiveFiles (hlsDirectory: string) {
129 const files = await readdir(hlsDirectory)
130
131 for (const filename of files) {
132 if (
133 filename.endsWith('.ts') ||
134 filename.endsWith('.m3u8') ||
135 filename.endsWith('.mpd') ||
136 filename.endsWith('.m4s') ||
137 filename.endsWith('.tmp')
138 ) {
139 const p = join(hlsDirectory, filename)
140
141 remove(p)
142 .catch(err => logger.error('Cannot remove %s.', p, { err }))
143 }
144 }
145 }
146
147 function buildMP4TmpPath (basePath: string, resolution: number) {
148 return join(basePath, resolution + '-tmp.mp4')
149 }