]>
Commit | Line | Data |
---|---|---|
a5cf76af | 1 | import * as Bull from 'bull' |
bb4ba6d9 | 2 | import { copy, pathExists, readdir, remove } from 'fs-extra' |
a5cf76af | 3 | import { join } from 'path' |
daf6e480 | 4 | import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils' |
8c666c44 | 5 | import { VIDEO_LIVE } from '@server/initializers/constants' |
daf6e480 | 6 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
d846d99c | 7 | import { publishAndFederateIfNeeded } from '@server/lib/video' |
a5cf76af | 8 | import { getHLSDirectory } from '@server/lib/video-paths' |
2650d6d4 | 9 | import { generateHlsPlaylistFromTS } 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' |
bb4ba6d9 | 17 | import { LiveManager } from '@server/lib/live-manager' |
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 | ||
bb4ba6d9 C |
40 | LiveManager.Instance.cleanupShaSegments(video.uuid) |
41 | ||
b5b68755 C |
42 | if (live.saveReplay !== true) { |
43 | return cleanupLive(video, streamingPlaylist) | |
44 | } | |
45 | ||
31c82cd9 | 46 | return saveLive(video, live) |
b5b68755 C |
47 | } |
48 | ||
bb4ba6d9 C |
49 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
50 | const hlsDirectory = getHLSDirectory(video) | |
51 | ||
52 | await remove(hlsDirectory) | |
53 | ||
54 | await streamingPlaylist.destroy() | |
55 | } | |
56 | ||
b5b68755 C |
57 | // --------------------------------------------------------------------------- |
58 | ||
59 | export { | |
bb4ba6d9 C |
60 | processVideoLiveEnding, |
61 | cleanupLive | |
b5b68755 C |
62 | } |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
31c82cd9 | 66 | async function saveLive (video: MVideo, live: MVideoLive) { |
b5b68755 | 67 | const hlsDirectory = getHLSDirectory(video, false) |
937581b8 C |
68 | const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY) |
69 | ||
70 | const rootFiles = await readdir(hlsDirectory) | |
71 | ||
72 | const playlistFiles: string[] = [] | |
73 | ||
74 | for (const file of rootFiles) { | |
8c666c44 C |
75 | // Move remaining files in the replay directory |
76 | if (file.endsWith('.ts') || file.endsWith('.m3u8')) { | |
77 | await copy(join(hlsDirectory, file), join(replayDirectory, file)) | |
78 | } | |
937581b8 | 79 | |
8c666c44 | 80 | if (file.endsWith('.m3u8') && file !== 'master.m3u8') { |
937581b8 C |
81 | playlistFiles.push(file) |
82 | } | |
83 | } | |
84 | ||
b5b68755 C |
85 | await cleanupLiveFiles(hlsDirectory) |
86 | ||
31c82cd9 C |
87 | await live.destroy() |
88 | ||
b5b68755 | 89 | video.isLive = false |
e4bf7856 C |
90 | // Reinit views |
91 | video.views = 0 | |
b5b68755 | 92 | video.state = VideoState.TO_TRANSCODE |
d846d99c | 93 | |
b5b68755 C |
94 | await video.save() |
95 | ||
97969c4e | 96 | // Remove old HLS playlist video files |
b5b68755 C |
97 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) |
98 | ||
97969c4e C |
99 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() |
100 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) | |
101 | hlsPlaylist.VideoFiles = [] | |
102 | ||
2650d6d4 | 103 | const replayFiles = await readdir(replayDirectory) |
4a54a939 | 104 | let durationDone: boolean |
b5b68755 | 105 | |
2650d6d4 C |
106 | for (const playlistFile of playlistFiles) { |
107 | const playlistPath = join(replayDirectory, playlistFile) | |
108 | const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(playlistPath) | |
109 | ||
110 | // Playlist name is for example 3.m3u8 | |
111 | // Segments names are 3-0.ts 3-1.ts etc | |
112 | const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-' | |
113 | ||
114 | const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) | |
115 | ||
116 | const outputPath = await generateHlsPlaylistFromTS({ | |
b5b68755 | 117 | video: videoWithFiles, |
2650d6d4 C |
118 | replayDirectory, |
119 | segmentFiles, | |
120 | resolution: videoFileResolution, | |
b5b68755 C |
121 | isPortraitMode |
122 | }) | |
b5b68755 | 123 | |
4a54a939 | 124 | if (!durationDone) { |
2650d6d4 C |
125 | videoWithFiles.duration = await getDurationFromVideoFile(outputPath) |
126 | await videoWithFiles.save() | |
4a54a939 C |
127 | |
128 | durationDone = true | |
2650d6d4 | 129 | } |
97969c4e | 130 | } |
d846d99c | 131 | |
2650d6d4 C |
132 | await remove(replayDirectory) |
133 | ||
053aed43 C |
134 | // Regenerate the thumbnail & preview? |
135 | if (videoWithFiles.getMiniature().automaticallyGenerated === true) { | |
136 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE) | |
137 | } | |
138 | ||
139 | if (videoWithFiles.getPreview().automaticallyGenerated === true) { | |
140 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW) | |
141 | } | |
142 | ||
4a54a939 | 143 | await publishAndFederateIfNeeded(videoWithFiles, true) |
b5b68755 C |
144 | } |
145 | ||
b5b68755 | 146 | async function cleanupLiveFiles (hlsDirectory: string) { |
bb4ba6d9 C |
147 | if (!await pathExists(hlsDirectory)) return |
148 | ||
a5cf76af C |
149 | const files = await readdir(hlsDirectory) |
150 | ||
151 | for (const filename of files) { | |
152 | if ( | |
153 | filename.endsWith('.ts') || | |
154 | filename.endsWith('.m3u8') || | |
155 | filename.endsWith('.mpd') || | |
156 | filename.endsWith('.m4s') || | |
2650d6d4 | 157 | filename.endsWith('.tmp') |
a5cf76af C |
158 | ) { |
159 | const p = join(hlsDirectory, filename) | |
160 | ||
161 | remove(p) | |
162 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
163 | } | |
164 | } | |
a5cf76af | 165 | } |