]>
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' |
b5b68755 | 9 | import { VideoLiveModel } from '@server/models/video/video-live' |
a5cf76af | 10 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
31c82cd9 | 11 | import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models' |
b5b68755 | 12 | import { VideoLiveEndingPayload, VideoState } from '@shared/models' |
a5cf76af C |
13 | import { logger } from '../../../helpers/logger' |
14 | ||
15 | async function processVideoLiveEnding (job: Bull.Job) { | |
16 | const payload = job.data as VideoLiveEndingPayload | |
17 | ||
b5b68755 C |
18 | const video = await VideoModel.load(payload.videoId) |
19 | const live = await VideoLiveModel.loadByVideoId(payload.videoId) | |
20 | ||
21 | const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | |
22 | if (!video || !streamingPlaylist || !live) { | |
23 | logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) | |
a5cf76af C |
24 | return |
25 | } | |
26 | ||
b5b68755 C |
27 | if (live.saveReplay !== true) { |
28 | return cleanupLive(video, streamingPlaylist) | |
29 | } | |
30 | ||
31c82cd9 | 31 | return saveLive(video, live) |
b5b68755 C |
32 | } |
33 | ||
34 | // --------------------------------------------------------------------------- | |
35 | ||
36 | export { | |
37 | processVideoLiveEnding | |
38 | } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
31c82cd9 | 42 | async function saveLive (video: MVideo, live: MVideoLive) { |
b5b68755 | 43 | const hlsDirectory = getHLSDirectory(video, false) |
31c82cd9 C |
44 | const files = await readdir(hlsDirectory) |
45 | ||
46 | const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8') | |
47 | const resolutions: number[] = [] | |
d846d99c | 48 | let duration: number |
31c82cd9 C |
49 | |
50 | for (const playlistFile of playlistFiles) { | |
51 | const playlistPath = join(hlsDirectory, playlistFile) | |
52 | const { videoFileResolution } = await getVideoFileResolution(playlistPath) | |
b5b68755 | 53 | |
31c82cd9 | 54 | const mp4TmpName = buildMP4TmpName(videoFileResolution) |
b5b68755 | 55 | |
31c82cd9 C |
56 | // Playlist name is for example 3.m3u8 |
57 | // Segments names are 3-0.ts 3-1.ts etc | |
58 | const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-' | |
59 | ||
60 | const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) | |
61 | await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpName) | |
62 | ||
d846d99c C |
63 | if (!duration) { |
64 | duration = await getDurationFromVideoFile(mp4TmpName) | |
65 | } | |
66 | ||
31c82cd9 | 67 | resolutions.push(videoFileResolution) |
b5b68755 C |
68 | } |
69 | ||
70 | await cleanupLiveFiles(hlsDirectory) | |
71 | ||
31c82cd9 C |
72 | await live.destroy() |
73 | ||
b5b68755 C |
74 | video.isLive = false |
75 | video.state = VideoState.TO_TRANSCODE | |
d846d99c C |
76 | video.duration = duration |
77 | ||
b5b68755 C |
78 | await video.save() |
79 | ||
80 | const videoWithFiles = await VideoModel.loadWithFiles(video.id) | |
81 | ||
31c82cd9 C |
82 | for (const resolution of resolutions) { |
83 | const videoInputPath = buildMP4TmpName(resolution) | |
b5b68755 C |
84 | const { isPortraitMode } = await getVideoFileResolution(videoInputPath) |
85 | ||
86 | await generateHlsPlaylist({ | |
87 | video: videoWithFiles, | |
88 | videoInputPath, | |
31c82cd9 | 89 | resolution: resolution, |
b5b68755 C |
90 | copyCodecs: true, |
91 | isPortraitMode | |
92 | }) | |
93 | } | |
94 | ||
95 | video.state = VideoState.PUBLISHED | |
96 | await video.save() | |
d846d99c C |
97 | |
98 | await publishAndFederateIfNeeded(video) | |
b5b68755 C |
99 | } |
100 | ||
101 | async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | |
a5cf76af C |
102 | const hlsDirectory = getHLSDirectory(video, false) |
103 | ||
b5b68755 C |
104 | await cleanupLiveFiles(hlsDirectory) |
105 | ||
106 | streamingPlaylist.destroy() | |
107 | .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) | |
108 | } | |
109 | ||
110 | async function cleanupLiveFiles (hlsDirectory: string) { | |
a5cf76af C |
111 | const files = await readdir(hlsDirectory) |
112 | ||
113 | for (const filename of files) { | |
114 | if ( | |
115 | filename.endsWith('.ts') || | |
116 | filename.endsWith('.m3u8') || | |
117 | filename.endsWith('.mpd') || | |
118 | filename.endsWith('.m4s') || | |
119 | filename.endsWith('.tmp') | |
120 | ) { | |
121 | const p = join(hlsDirectory, filename) | |
122 | ||
123 | remove(p) | |
124 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
125 | } | |
126 | } | |
a5cf76af C |
127 | } |
128 | ||
b5b68755 | 129 | function buildMP4TmpName (resolution: number) { |
31c82cd9 | 130 | return resolution + '-tmp.mp4' |
a5cf76af | 131 | } |