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