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