]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import { Job } 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' |
0305db28 | 7 | import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveDirectory } from '@server/lib/paths' |
daf6e480 | 8 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
c07902b9 | 9 | import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding' |
0305db28 JB |
10 | import { VideoPathManager } from '@server/lib/video-path-manager' |
11 | import { moveToNextState } from '@server/lib/video-state' | |
a5cf76af | 12 | import { VideoModel } from '@server/models/video/video' |
68e70a74 | 13 | import { VideoFileModel } from '@server/models/video/video-file' |
b5b68755 | 14 | import { VideoLiveModel } from '@server/models/video/video-live' |
a5cf76af | 15 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
764b1a14 | 16 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' |
053aed43 | 17 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' |
a5cf76af C |
18 | import { logger } from '../../../helpers/logger' |
19 | ||
41fb13c3 | 20 | async function processVideoLiveEnding (job: Job) { |
a5cf76af C |
21 | const payload = job.data as VideoLiveEndingPayload |
22 | ||
68e70a74 C |
23 | function logError () { |
24 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | |
25 | } | |
26 | ||
b5b68755 C |
27 | const video = await VideoModel.load(payload.videoId) |
28 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) | |
29 | ||
68e70a74 C |
30 | if (!video || !live) { |
31 | logError() | |
32 | return | |
33 | } | |
34 | ||
b5b68755 | 35 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) |
68e70a74 C |
36 | if (!streamingPlaylist) { |
37 | logError() | |
a5cf76af C |
38 | return |
39 | } | |
40 | ||
8ebf2a5d | 41 | LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid) |
bb4ba6d9 | 42 | |
b5b68755 C |
43 | if (live.saveReplay !== true) { |
44 | return cleanupLive(video, streamingPlaylist) | |
45 | } | |
46 | ||
764b1a14 | 47 | return saveLive(video, live, streamingPlaylist) |
b5b68755 C |
48 | } |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | export { | |
8ebf2a5d | 53 | processVideoLiveEnding |
b5b68755 C |
54 | } |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
764b1a14 | 58 | async function saveLive (video: MVideo, live: MVideoLive, streamingPlaylist: MStreamingPlaylist) { |
0305db28 | 59 | const replayDirectory = VideoPathManager.Instance.getFSHLSOutputPath(video, VIDEO_LIVE.REPLAY_DIRECTORY) |
937581b8 | 60 | |
0305db28 | 61 | const rootFiles = await readdir(getLiveDirectory(video)) |
937581b8 | 62 | |
5b9b403a | 63 | const playlistFiles = rootFiles.filter(file => { |
764b1a14 | 64 | return file.endsWith('.m3u8') && file !== streamingPlaylist.playlistFilename |
5b9b403a | 65 | }) |
937581b8 | 66 | |
0305db28 | 67 | await cleanupTMPLiveFiles(getLiveDirectory(video)) |
b5b68755 | 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 | ||
679c12e6 | 99 | const { resolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe) |
2650d6d4 | 100 | |
0305db28 | 101 | const { resolutionPlaylistPath: outputPath } = await generateHlsPlaylistResolutionFromTS({ |
b5b68755 | 102 | video: videoWithFiles, |
3851e732 | 103 | concatenatedTsFilePath, |
679c12e6 | 104 | resolution, |
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 | ||
0305db28 | 136 | await moveToNextState(videoWithFiles, false) |
b5b68755 C |
137 | } |
138 | ||
0305db28 | 139 | async function cleanupTMPLiveFiles (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 | } |