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