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