]>
Commit | Line | Data |
---|---|---|
a5cf76af | 1 | import * as Bull from 'bull' |
e772bdf1 | 2 | import { pathExists, readdir, remove } from 'fs-extra' |
a5cf76af | 3 | import { join } from 'path' |
aa5ee501 | 4 | import { ffprobePromise, getAudioStream, getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils' |
8c666c44 | 5 | import { VIDEO_LIVE } from '@server/initializers/constants' |
e772bdf1 | 6 | import { LiveManager } from '@server/lib/live-manager' |
daf6e480 | 7 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
d846d99c | 8 | import { publishAndFederateIfNeeded } from '@server/lib/video' |
a5cf76af | 9 | import { getHLSDirectory } from '@server/lib/video-paths' |
2650d6d4 | 10 | import { generateHlsPlaylistFromTS } from '@server/lib/video-transcoding' |
a5cf76af | 11 | import { VideoModel } from '@server/models/video/video' |
68e70a74 | 12 | import { VideoFileModel } from '@server/models/video/video-file' |
b5b68755 | 13 | import { VideoLiveModel } from '@server/models/video/video-live' |
a5cf76af | 14 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
68e70a74 | 15 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' |
053aed43 | 16 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' |
a5cf76af C |
17 | import { logger } from '../../../helpers/logger' |
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 | 75 | // Move remaining files in the replay directory |
3851e732 C |
76 | if (file.endsWith('.ts')) { |
77 | await LiveManager.Instance.addSegmentToReplay(hlsDirectory, join(hlsDirectory, file)) | |
8c666c44 | 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 | ||
4a54a939 | 103 | let durationDone: boolean |
b5b68755 | 104 | |
2650d6d4 | 105 | for (const playlistFile of playlistFiles) { |
3851e732 C |
106 | const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile) |
107 | const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile) | |
2650d6d4 | 108 | |
e772bdf1 C |
109 | const probe = await ffprobePromise(concatenatedTsFilePath) |
110 | const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) | |
111 | ||
112 | const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe) | |
2650d6d4 C |
113 | |
114 | const outputPath = await generateHlsPlaylistFromTS({ | |
b5b68755 | 115 | video: videoWithFiles, |
3851e732 | 116 | concatenatedTsFilePath, |
2650d6d4 | 117 | resolution: videoFileResolution, |
e772bdf1 C |
118 | isPortraitMode, |
119 | isAAC: audioStream?.codec_name === 'aac' | |
b5b68755 | 120 | }) |
b5b68755 | 121 | |
4a54a939 | 122 | if (!durationDone) { |
2650d6d4 C |
123 | videoWithFiles.duration = await getDurationFromVideoFile(outputPath) |
124 | await videoWithFiles.save() | |
4a54a939 C |
125 | |
126 | durationDone = true | |
2650d6d4 | 127 | } |
97969c4e | 128 | } |
d846d99c | 129 | |
2650d6d4 C |
130 | await remove(replayDirectory) |
131 | ||
053aed43 C |
132 | // Regenerate the thumbnail & preview? |
133 | if (videoWithFiles.getMiniature().automaticallyGenerated === true) { | |
134 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE) | |
135 | } | |
136 | ||
137 | if (videoWithFiles.getPreview().automaticallyGenerated === true) { | |
138 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW) | |
139 | } | |
140 | ||
4a54a939 | 141 | await publishAndFederateIfNeeded(videoWithFiles, true) |
b5b68755 C |
142 | } |
143 | ||
b5b68755 | 144 | async function cleanupLiveFiles (hlsDirectory: string) { |
bb4ba6d9 C |
145 | if (!await pathExists(hlsDirectory)) return |
146 | ||
a5cf76af C |
147 | const files = await readdir(hlsDirectory) |
148 | ||
149 | for (const filename of files) { | |
150 | if ( | |
151 | filename.endsWith('.ts') || | |
152 | filename.endsWith('.m3u8') || | |
153 | filename.endsWith('.mpd') || | |
154 | filename.endsWith('.m4s') || | |
2650d6d4 | 155 | filename.endsWith('.tmp') |
a5cf76af C |
156 | ) { |
157 | const p = join(hlsDirectory, filename) | |
158 | ||
159 | remove(p) | |
160 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
161 | } | |
162 | } | |
a5cf76af | 163 | } |