]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
Fix audio sync after saving replay
[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 if (!duration) {
74 duration = await getDurationFromVideoFile(mp4TmpPath)
75 }
76
77 resolutions.push(videoFileResolution)
78 }
79
80 await cleanupLiveFiles(hlsDirectory)
81
82 await live.destroy()
83
84 video.isLive = false
85 video.state = VideoState.TO_TRANSCODE
86 video.duration = duration
87
88 await video.save()
89
90 // Remove old HLS playlist video files
91 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
92
93 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
94 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
95 hlsPlaylist.VideoFiles = []
96
97 for (const resolution of resolutions) {
98 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
99 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
100
101 await generateHlsPlaylist({
102 video: videoWithFiles,
103 videoInputPath,
104 resolution: resolution,
105 copyCodecs: true,
106 isPortraitMode
107 })
108
109 await remove(videoInputPath)
110 }
111
112 await publishAndFederateIfNeeded(video, true)
113 }
114
115 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
116 const hlsDirectory = getHLSDirectory(video, false)
117
118 await remove(hlsDirectory)
119
120 streamingPlaylist.destroy()
121 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
122 }
123
124 async function cleanupLiveFiles (hlsDirectory: string) {
125 const files = await readdir(hlsDirectory)
126
127 for (const filename of files) {
128 if (
129 filename.endsWith('.ts') ||
130 filename.endsWith('.m3u8') ||
131 filename.endsWith('.mpd') ||
132 filename.endsWith('.m4s') ||
133 filename.endsWith('.tmp')
134 ) {
135 const p = join(hlsDirectory, filename)
136
137 remove(p)
138 .catch(err => logger.error('Cannot remove %s.', p, { err }))
139 }
140 }
141 }
142
143 function buildMP4TmpPath (basePath: string, resolution: number) {
144 return join(basePath, resolution + '-tmp.mp4')
145 }