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