]>
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' |
8ebf2a5d | 6 | import { buildConcatenatedName, cleanupLive, LiveSegmentShaStore } from '@server/lib/live' |
daf6e480 | 7 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
c07902b9 | 8 | import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding' |
d846d99c | 9 | import { publishAndFederateIfNeeded } from '@server/lib/video' |
764b1a14 | 10 | import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHLSDirectory } from '@server/lib/video-paths' |
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' |
764b1a14 | 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 | ||
8ebf2a5d | 40 | LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid) |
bb4ba6d9 | 41 | |
b5b68755 C |
42 | if (live.saveReplay !== true) { |
43 | return cleanupLive(video, streamingPlaylist) | |
44 | } | |
45 | ||
764b1a14 | 46 | return saveLive(video, live, streamingPlaylist) |
b5b68755 C |
47 | } |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
8ebf2a5d | 52 | processVideoLiveEnding |
b5b68755 C |
53 | } |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
764b1a14 | 57 | async function saveLive (video: MVideo, live: MVideoLive, streamingPlaylist: MStreamingPlaylist) { |
b5b68755 | 58 | const hlsDirectory = getHLSDirectory(video, false) |
937581b8 C |
59 | const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY) |
60 | ||
61 | const rootFiles = await readdir(hlsDirectory) | |
62 | ||
5b9b403a | 63 | const playlistFiles = rootFiles.filter(file => { |
764b1a14 | 64 | return file.endsWith('.m3u8') && file !== streamingPlaylist.playlistFilename |
5b9b403a | 65 | }) |
937581b8 | 66 | |
b5b68755 C |
67 | await cleanupLiveFiles(hlsDirectory) |
68 | ||
31c82cd9 C |
69 | await live.destroy() |
70 | ||
b5b68755 | 71 | video.isLive = false |
e4bf7856 C |
72 | // Reinit views |
73 | video.views = 0 | |
b5b68755 | 74 | video.state = VideoState.TO_TRANSCODE |
d846d99c | 75 | |
b5b68755 C |
76 | await video.save() |
77 | ||
97969c4e | 78 | // Remove old HLS playlist video files |
90a8bd30 | 79 | const videoWithFiles = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id) |
b5b68755 | 80 | |
97969c4e C |
81 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() |
82 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) | |
764b1a14 C |
83 | |
84 | // Reset playlist | |
97969c4e | 85 | hlsPlaylist.VideoFiles = [] |
764b1a14 C |
86 | hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename() |
87 | hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename() | |
88 | await hlsPlaylist.save() | |
97969c4e | 89 | |
a800dbf3 | 90 | let durationDone = false |
b5b68755 | 91 | |
2650d6d4 | 92 | for (const playlistFile of playlistFiles) { |
8ebf2a5d | 93 | const concatenatedTsFile = buildConcatenatedName(playlistFile) |
3851e732 | 94 | const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile) |
2650d6d4 | 95 | |
e772bdf1 C |
96 | const probe = await ffprobePromise(concatenatedTsFilePath) |
97 | const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe) | |
98 | ||
99 | const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe) | |
2650d6d4 | 100 | |
24516aa2 | 101 | const outputPath = await generateHlsPlaylistResolutionFromTS({ |
b5b68755 | 102 | video: videoWithFiles, |
3851e732 | 103 | concatenatedTsFilePath, |
2650d6d4 | 104 | resolution: videoFileResolution, |
e772bdf1 C |
105 | isPortraitMode, |
106 | isAAC: audioStream?.codec_name === 'aac' | |
b5b68755 | 107 | }) |
b5b68755 | 108 | |
4a54a939 | 109 | if (!durationDone) { |
2650d6d4 C |
110 | videoWithFiles.duration = await getDurationFromVideoFile(outputPath) |
111 | await videoWithFiles.save() | |
4a54a939 C |
112 | |
113 | durationDone = true | |
2650d6d4 | 114 | } |
97969c4e | 115 | } |
d846d99c | 116 | |
2650d6d4 C |
117 | await remove(replayDirectory) |
118 | ||
053aed43 C |
119 | // Regenerate the thumbnail & preview? |
120 | if (videoWithFiles.getMiniature().automaticallyGenerated === true) { | |
a35a2279 C |
121 | await generateVideoMiniature({ |
122 | video: videoWithFiles, | |
123 | videoFile: videoWithFiles.getMaxQualityFile(), | |
124 | type: ThumbnailType.MINIATURE | |
125 | }) | |
053aed43 C |
126 | } |
127 | ||
128 | if (videoWithFiles.getPreview().automaticallyGenerated === true) { | |
a35a2279 C |
129 | await generateVideoMiniature({ |
130 | video: videoWithFiles, | |
131 | videoFile: videoWithFiles.getMaxQualityFile(), | |
132 | type: ThumbnailType.PREVIEW | |
133 | }) | |
053aed43 C |
134 | } |
135 | ||
4a54a939 | 136 | await publishAndFederateIfNeeded(videoWithFiles, true) |
b5b68755 C |
137 | } |
138 | ||
b5b68755 | 139 | async function cleanupLiveFiles (hlsDirectory: string) { |
bb4ba6d9 C |
140 | if (!await pathExists(hlsDirectory)) return |
141 | ||
a5cf76af C |
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') || | |
2650d6d4 | 150 | filename.endsWith('.tmp') |
a5cf76af C |
151 | ) { |
152 | const p = join(hlsDirectory, filename) | |
153 | ||
154 | remove(p) | |
155 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
156 | } | |
157 | } | |
a5cf76af | 158 | } |