]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { readdir, remove } from 'fs-extra' | |
3 | import { join } from 'path' | |
4 | import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils' | |
5 | import { publishAndFederateIfNeeded } from '@server/lib/video' | |
6 | import { getHLSDirectory } from '@server/lib/video-paths' | |
7 | import { generateHlsPlaylist } from '@server/lib/video-transcoding' | |
8 | import { VideoModel } from '@server/models/video/video' | |
9 | import { VideoFileModel } from '@server/models/video/video-file' | |
10 | import { VideoLiveModel } from '@server/models/video/video-live' | |
11 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' | |
12 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' | |
13 | import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models' | |
14 | import { logger } from '../../../helpers/logger' | |
15 | import { generateVideoMiniature } from '@server/lib/thumbnail' | |
16 | ||
17 | async function processVideoLiveEnding (job: Bull.Job) { | |
18 | const payload = job.data as VideoLiveEndingPayload | |
19 | ||
20 | function logError () { | |
21 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | |
22 | } | |
23 | ||
24 | const video = await VideoModel.load(payload.videoId) | |
25 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) | |
26 | ||
27 | if (!video || !live) { | |
28 | logError() | |
29 | return | |
30 | } | |
31 | ||
32 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | |
33 | if (!streamingPlaylist) { | |
34 | logError() | |
35 | return | |
36 | } | |
37 | ||
38 | if (live.saveReplay !== true) { | |
39 | return cleanupLive(video, streamingPlaylist) | |
40 | } | |
41 | ||
42 | return saveLive(video, live) | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | processVideoLiveEnding | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | async function saveLive (video: MVideo, live: MVideoLive) { | |
54 | const hlsDirectory = getHLSDirectory(video, false) | |
55 | const files = await readdir(hlsDirectory) | |
56 | ||
57 | const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8') | |
58 | const resolutions: number[] = [] | |
59 | let duration: number | |
60 | ||
61 | for (const playlistFile of playlistFiles) { | |
62 | const playlistPath = join(hlsDirectory, playlistFile) | |
63 | const { videoFileResolution } = await getVideoFileResolution(playlistPath) | |
64 | ||
65 | const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution) | |
66 | ||
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')) | |
72 | await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath) | |
73 | ||
74 | if (!duration) { | |
75 | duration = await getDurationFromVideoFile(mp4TmpPath) | |
76 | } | |
77 | ||
78 | resolutions.push(videoFileResolution) | |
79 | } | |
80 | ||
81 | await cleanupLiveFiles(hlsDirectory) | |
82 | ||
83 | await live.destroy() | |
84 | ||
85 | video.isLive = false | |
86 | video.state = VideoState.TO_TRANSCODE | |
87 | video.duration = duration | |
88 | ||
89 | await video.save() | |
90 | ||
91 | // Remove old HLS playlist video files | |
92 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) | |
93 | ||
94 | const hlsPlaylist = videoWithFiles.getHLSPlaylist() | |
95 | await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id) | |
96 | hlsPlaylist.VideoFiles = [] | |
97 | ||
98 | for (const resolution of resolutions) { | |
99 | const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution) | |
100 | const { isPortraitMode } = await getVideoFileResolution(videoInputPath) | |
101 | ||
102 | await generateHlsPlaylist({ | |
103 | video: videoWithFiles, | |
104 | videoInputPath, | |
105 | resolution: resolution, | |
106 | copyCodecs: true, | |
107 | isPortraitMode | |
108 | }) | |
109 | ||
110 | await remove(videoInputPath) | |
111 | } | |
112 | ||
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 | ||
122 | await publishAndFederateIfNeeded(video, true) | |
123 | } | |
124 | ||
125 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | |
126 | const hlsDirectory = getHLSDirectory(video) | |
127 | ||
128 | await remove(hlsDirectory) | |
129 | ||
130 | streamingPlaylist.destroy() | |
131 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | |
132 | } | |
133 | ||
134 | async function cleanupLiveFiles (hlsDirectory: string) { | |
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 | } | |
151 | } | |
152 | ||
153 | function buildMP4TmpPath (basePath: string, resolution: number) { | |
154 | return join(basePath, resolution + '-tmp.mp4') | |
155 | } |