]>
Commit | Line | Data |
---|---|---|
a5cf76af C |
1 | import * as Bull from 'bull' |
2 | import { readdir, remove } from 'fs-extra' | |
3 | import { join } from 'path' | |
d846d99c C |
4 | import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' |
5 | import { publishAndFederateIfNeeded } from '@server/lib/video' | |
a5cf76af | 6 | import { getHLSDirectory } from '@server/lib/video-paths' |
b5b68755 | 7 | import { generateHlsPlaylist } from '@server/lib/video-transcoding' |
a5cf76af | 8 | import { VideoModel } from '@server/models/video/video' |
68e70a74 | 9 | import { VideoFileModel } from '@server/models/video/video-file' |
b5b68755 | 10 | import { VideoLiveModel } from '@server/models/video/video-live' |
a5cf76af | 11 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
68e70a74 | 12 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' |
053aed43 | 13 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' |
a5cf76af | 14 | import { logger } from '../../../helpers/logger' |
053aed43 | 15 | import { generateVideoMiniature } from '@server/lib/thumbnail' |
a5cf76af C |
16 | |
17 | async function processVideoLiveEnding (job: Bull.Job) { | |
18 | const payload = job.data as VideoLiveEndingPayload | |
19 | ||
68e70a74 C |
20 | function logError () { |
21 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | |
22 | } | |
23 | ||
b5b68755 C |
24 | const video = await VideoModel.load(payload.videoId) |
25 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) | |
26 | ||
68e70a74 C |
27 | if (!video || !live) { |
28 | logError() | |
29 | return | |
30 | } | |
31 | ||
b5b68755 | 32 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) |
68e70a74 C |
33 | if (!streamingPlaylist) { |
34 | logError() | |
a5cf76af C |
35 | return |
36 | } | |
37 | ||
b5b68755 C |
38 | if (live.saveReplay !== true) { |
39 | return cleanupLive(video, streamingPlaylist) | |
40 | } | |
41 | ||
31c82cd9 | 42 | return saveLive(video, live) |
b5b68755 C |
43 | } |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | processVideoLiveEnding | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
31c82cd9 | 53 | async function saveLive (video: MVideo, live: MVideoLive) { |
b5b68755 | 54 | const hlsDirectory = getHLSDirectory(video, false) |
31c82cd9 C |
55 | const files = await readdir(hlsDirectory) |
56 | ||
57 | const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8') | |
58 | const resolutions: number[] = [] | |
d846d99c | 59 | let duration: number |
31c82cd9 C |
60 | |
61 | for (const playlistFile of playlistFiles) { | |
62 | const playlistPath = join(hlsDirectory, playlistFile) | |
63 | const { videoFileResolution } = await getVideoFileResolution(playlistPath) | |
b5b68755 | 64 | |
68e70a74 | 65 | const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution) |
b5b68755 | 66 | |
31c82cd9 C |
67 | // Playlist name is for example 3.m3u8 |
68 | // Segments names are 3-0.ts 3-1.ts etc | |
69 | const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-' | |
70 | ||
71 | const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) | |
68e70a74 | 72 | await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath) |
31c82cd9 | 73 | |
d846d99c | 74 | if (!duration) { |
68e70a74 | 75 | duration = await getDurationFromVideoFile(mp4TmpPath) |
d846d99c C |
76 | } |
77 | ||
31c82cd9 | 78 | resolutions.push(videoFileResolution) |
b5b68755 C |
79 | } |
80 | ||
81 | await cleanupLiveFiles(hlsDirectory) | |
82 | ||
31c82cd9 C |
83 | await live.destroy() |
84 | ||
b5b68755 C |
85 | video.isLive = false |
86 | video.state = VideoState.TO_TRANSCODE | |
d846d99c C |
87 | video.duration = duration |
88 | ||
b5b68755 C |
89 | await video.save() |
90 | ||
97969c4e | 91 | // Remove old HLS playlist video files |
b5b68755 C |
92 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) |
93 | ||
97969c4e C |
94 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() |
95 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) | |
96 | hlsPlaylist.VideoFiles = [] | |
97 | ||
31c82cd9 | 98 | for (const resolution of resolutions) { |
68e70a74 | 99 | const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution) |
b5b68755 C |
100 | const { isPortraitMode } = await getVideoFileResolution(videoInputPath) |
101 | ||
102 | await generateHlsPlaylist({ | |
103 | video: videoWithFiles, | |
104 | videoInputPath, | |
31c82cd9 | 105 | resolution: resolution, |
b5b68755 C |
106 | copyCodecs: true, |
107 | isPortraitMode | |
108 | }) | |
b5b68755 | 109 | |
68e70a74 | 110 | await remove(videoInputPath) |
97969c4e | 111 | } |
d846d99c | 112 | |
053aed43 C |
113 | // Regenerate the thumbnail & preview? |
114 | if (videoWithFiles.getMiniature().automaticallyGenerated === true) { | |
115 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE) | |
116 | } | |
117 | ||
118 | if (videoWithFiles.getPreview().automaticallyGenerated === true) { | |
119 | await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW) | |
120 | } | |
121 | ||
97969c4e | 122 | await publishAndFederateIfNeeded(video, true) |
b5b68755 C |
123 | } |
124 | ||
125 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | |
a5cf76af C |
126 | const hlsDirectory = getHLSDirectory(video, false) |
127 | ||
68e70a74 | 128 | await remove(hlsDirectory) |
b5b68755 C |
129 | |
130 | streamingPlaylist.destroy() | |
131 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | |
132 | } | |
133 | ||
134 | async function cleanupLiveFiles (hlsDirectory: string) { | |
a5cf76af C |
135 | const files = await readdir(hlsDirectory) |
136 | ||
137 | for (const filename of files) { | |
138 | if ( | |
139 | filename.endsWith('.ts') || | |
140 | filename.endsWith('.m3u8') || | |
141 | filename.endsWith('.mpd') || | |
142 | filename.endsWith('.m4s') || | |
143 | filename.endsWith('.tmp') | |
144 | ) { | |
145 | const p = join(hlsDirectory, filename) | |
146 | ||
147 | remove(p) | |
148 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
149 | } | |
150 | } | |
a5cf76af C |
151 | } |
152 | ||
68e70a74 C |
153 | function buildMP4TmpPath (basePath: string, resolution: number) { |
154 | return join(basePath, resolution + '-tmp.mp4') | |
a5cf76af | 155 | } |