]>
Commit | Line | Data |
---|---|---|
a5cf76af | 1 | import * as Bull from 'bull' |
937581b8 | 2 | import { move, readdir, remove } from 'fs-extra' |
a5cf76af | 3 | import { join } from 'path' |
daf6e480 C |
4 | import { hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' |
5 | import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils' | |
6 | import { generateVideoMiniature } from '@server/lib/thumbnail' | |
d846d99c | 7 | import { publishAndFederateIfNeeded } from '@server/lib/video' |
a5cf76af | 8 | import { getHLSDirectory } from '@server/lib/video-paths' |
b5b68755 | 9 | import { generateHlsPlaylist } from '@server/lib/video-transcoding' |
a5cf76af | 10 | import { VideoModel } from '@server/models/video/video' |
68e70a74 | 11 | import { VideoFileModel } from '@server/models/video/video-file' |
b5b68755 | 12 | import { VideoLiveModel } from '@server/models/video/video-live' |
a5cf76af | 13 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
68e70a74 | 14 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' |
053aed43 | 15 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' |
a5cf76af | 16 | import { logger } from '../../../helpers/logger' |
937581b8 | 17 | import { VIDEO_LIVE } from '@server/initializers/constants' |
a5cf76af C |
18 | |
19 | async function processVideoLiveEnding (job: Bull.Job) { | |
20 | const payload = job.data as VideoLiveEndingPayload | |
21 | ||
68e70a74 C |
22 | function logError () { |
23 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | |
24 | } | |
25 | ||
b5b68755 C |
26 | const video = await VideoModel.load(payload.videoId) |
27 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) | |
28 | ||
68e70a74 C |
29 | if (!video || !live) { |
30 | logError() | |
31 | return | |
32 | } | |
33 | ||
b5b68755 | 34 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) |
68e70a74 C |
35 | if (!streamingPlaylist) { |
36 | logError() | |
a5cf76af C |
37 | return |
38 | } | |
39 | ||
b5b68755 C |
40 | if (live.saveReplay !== true) { |
41 | return cleanupLive(video, streamingPlaylist) | |
42 | } | |
43 | ||
31c82cd9 | 44 | return saveLive(video, live) |
b5b68755 C |
45 | } |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | processVideoLiveEnding | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
31c82cd9 | 55 | async function saveLive (video: MVideo, live: MVideoLive) { |
b5b68755 | 56 | const hlsDirectory = getHLSDirectory(video, false) |
937581b8 C |
57 | const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY) |
58 | ||
59 | const rootFiles = await readdir(hlsDirectory) | |
60 | ||
61 | const playlistFiles: string[] = [] | |
62 | ||
63 | for (const file of rootFiles) { | |
64 | if (file.endsWith('.m3u8') !== true) continue | |
65 | ||
66 | await move(join(hlsDirectory, file), join(replayDirectory, file)) | |
67 | ||
68 | if (file !== 'master.m3u8') { | |
69 | playlistFiles.push(file) | |
70 | } | |
71 | } | |
72 | ||
73 | const replayFiles = await readdir(replayDirectory) | |
31c82cd9 | 74 | |
31c82cd9 | 75 | const resolutions: number[] = [] |
d846d99c | 76 | let duration: number |
31c82cd9 C |
77 | |
78 | for (const playlistFile of playlistFiles) { | |
937581b8 | 79 | const playlistPath = join(replayDirectory, playlistFile) |
31c82cd9 | 80 | const { videoFileResolution } = await getVideoFileResolution(playlistPath) |
b5b68755 | 81 | |
937581b8 | 82 | // Put the final mp4 in the hls directory, and not in the replay directory |
68e70a74 | 83 | const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution) |
b5b68755 | 84 | |
31c82cd9 C |
85 | // Playlist name is for example 3.m3u8 |
86 | // Segments names are 3-0.ts 3-1.ts etc | |
87 | const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-' | |
88 | ||
937581b8 C |
89 | const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) |
90 | await hlsPlaylistToFragmentedMP4(replayDirectory, segmentFiles, mp4TmpPath) | |
31c82cd9 | 91 | |
d846d99c | 92 | if (!duration) { |
68e70a74 | 93 | duration = await getDurationFromVideoFile(mp4TmpPath) |
d846d99c C |
94 | } |
95 | ||
31c82cd9 | 96 | resolutions.push(videoFileResolution) |
b5b68755 C |
97 | } |
98 | ||
99 | await cleanupLiveFiles(hlsDirectory) | |
100 | ||
31c82cd9 C |
101 | await live.destroy() |
102 | ||
b5b68755 | 103 | video.isLive = false |
e4bf7856 C |
104 | // Reinit views |
105 | video.views = 0 | |
b5b68755 | 106 | video.state = VideoState.TO_TRANSCODE |
d846d99c C |
107 | video.duration = duration |
108 | ||
b5b68755 C |
109 | await video.save() |
110 | ||
97969c4e | 111 | // Remove old HLS playlist video files |
b5b68755 C |
112 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) |
113 | ||
97969c4e C |
114 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() |
115 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) | |
116 | hlsPlaylist.VideoFiles = [] | |
117 | ||
31c82cd9 | 118 | for (const resolution of resolutions) { |
68e70a74 | 119 | const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution) |
b5b68755 C |
120 | const { isPortraitMode } = await getVideoFileResolution(videoInputPath) |
121 | ||
122 | await generateHlsPlaylist({ | |
123 | video: videoWithFiles, | |
124 | videoInputPath, | |
31c82cd9 | 125 | resolution: resolution, |
b5b68755 C |
126 | copyCodecs: true, |
127 | isPortraitMode | |
128 | }) | |
b5b68755 | 129 | |
68e70a74 | 130 | await remove(videoInputPath) |
97969c4e | 131 | } |
d846d99c | 132 | |
053aed43 C |
133 | // Regenerate the thumbnail & preview? |
134 | if (videoWithFiles.getMiniature().automaticallyGenerated === true) { | |
135 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE) | |
136 | } | |
137 | ||
138 | if (videoWithFiles.getPreview().automaticallyGenerated === true) { | |
139 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW) | |
140 | } | |
141 | ||
97969c4e | 142 | await publishAndFederateIfNeeded(video, true) |
b5b68755 C |
143 | } |
144 | ||
145 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | |
9e2b2e76 | 146 | const hlsDirectory = getHLSDirectory(video) |
a5cf76af | 147 | |
68e70a74 | 148 | await remove(hlsDirectory) |
b5b68755 C |
149 | |
150 | streamingPlaylist.destroy() | |
151 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | |
152 | } | |
153 | ||
154 | async function cleanupLiveFiles (hlsDirectory: string) { | |
a5cf76af C |
155 | const files = await readdir(hlsDirectory) |
156 | ||
157 | for (const filename of files) { | |
158 | if ( | |
159 | filename.endsWith('.ts') || | |
160 | filename.endsWith('.m3u8') || | |
161 | filename.endsWith('.mpd') || | |
162 | filename.endsWith('.m4s') || | |
937581b8 C |
163 | filename.endsWith('.tmp') || |
164 | filename === VIDEO_LIVE.REPLAY_DIRECTORY | |
a5cf76af C |
165 | ) { |
166 | const p = join(hlsDirectory, filename) | |
167 | ||
168 | remove(p) | |
169 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
170 | } | |
171 | } | |
a5cf76af C |
172 | } |
173 | ||
68e70a74 C |
174 | function buildMP4TmpPath (basePath: string, resolution: number) { |
175 | return join(basePath, resolution + '-tmp.mp4') | |
a5cf76af | 176 | } |