]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-live-ending.ts
Fix video upload with a capitalized ext
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
CommitLineData
a5cf76af 1import * as Bull from 'bull'
e772bdf1 2import { pathExists, readdir, remove } from 'fs-extra'
a5cf76af 3import { join } from 'path'
aa5ee501 4import { ffprobePromise, getAudioStream, getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
8c666c44 5import { VIDEO_LIVE } from '@server/initializers/constants'
e772bdf1 6import { LiveManager } from '@server/lib/live-manager'
daf6e480 7import { generateVideoMiniature } from '@server/lib/thumbnail'
c07902b9 8import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/video-transcoding'
d846d99c 9import { publishAndFederateIfNeeded } from '@server/lib/video'
a5cf76af
C
10import { getHLSDirectory } from '@server/lib/video-paths'
11import { VideoModel } from '@server/models/video/video'
68e70a74 12import { VideoFileModel } from '@server/models/video/video-file'
b5b68755 13import { VideoLiveModel } from '@server/models/video/video-live'
a5cf76af 14import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
68e70a74 15import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
053aed43 16import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
a5cf76af
C
17import { logger } from '../../../helpers/logger'
18
19async 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
bb4ba6d9
C
40 LiveManager.Instance.cleanupShaSegments(video.uuid)
41
b5b68755
C
42 if (live.saveReplay !== true) {
43 return cleanupLive(video, streamingPlaylist)
44 }
45
31c82cd9 46 return saveLive(video, live)
b5b68755
C
47}
48
bb4ba6d9
C
49async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
50 const hlsDirectory = getHLSDirectory(video)
51
52 await remove(hlsDirectory)
53
54 await streamingPlaylist.destroy()
55}
56
b5b68755
C
57// ---------------------------------------------------------------------------
58
59export {
bb4ba6d9
C
60 processVideoLiveEnding,
61 cleanupLive
b5b68755
C
62}
63
64// ---------------------------------------------------------------------------
65
31c82cd9 66async function saveLive (video: MVideo, live: MVideoLive) {
b5b68755 67 const hlsDirectory = getHLSDirectory(video, false)
937581b8
C
68 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
69
70 const rootFiles = await readdir(hlsDirectory)
71
5b9b403a
C
72 const playlistFiles = rootFiles.filter(file => {
73 return file.endsWith('.m3u8') && file !== 'master.m3u8'
74 })
937581b8 75
b5b68755
C
76 await cleanupLiveFiles(hlsDirectory)
77
31c82cd9
C
78 await live.destroy()
79
b5b68755 80 video.isLive = false
e4bf7856
C
81 // Reinit views
82 video.views = 0
b5b68755 83 video.state = VideoState.TO_TRANSCODE
d846d99c 84
b5b68755
C
85 await video.save()
86
97969c4e 87 // Remove old HLS playlist video files
90a8bd30 88 const videoWithFiles = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
b5b68755 89
97969c4e
C
90 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
91 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
92 hlsPlaylist.VideoFiles = []
93
a800dbf3 94 let durationDone = false
b5b68755 95
2650d6d4 96 for (const playlistFile of playlistFiles) {
3851e732
C
97 const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile)
98 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
2650d6d4 99
e772bdf1
C
100 const probe = await ffprobePromise(concatenatedTsFilePath)
101 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
102
103 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath, probe)
2650d6d4 104
24516aa2 105 const outputPath = await generateHlsPlaylistResolutionFromTS({
b5b68755 106 video: videoWithFiles,
3851e732 107 concatenatedTsFilePath,
2650d6d4 108 resolution: videoFileResolution,
e772bdf1
C
109 isPortraitMode,
110 isAAC: audioStream?.codec_name === 'aac'
b5b68755 111 })
b5b68755 112
4a54a939 113 if (!durationDone) {
2650d6d4
C
114 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
115 await videoWithFiles.save()
4a54a939
C
116
117 durationDone = true
2650d6d4 118 }
97969c4e 119 }
d846d99c 120
2650d6d4
C
121 await remove(replayDirectory)
122
053aed43
C
123 // Regenerate the thumbnail & preview?
124 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
a35a2279
C
125 await generateVideoMiniature({
126 video: videoWithFiles,
127 videoFile: videoWithFiles.getMaxQualityFile(),
128 type: ThumbnailType.MINIATURE
129 })
053aed43
C
130 }
131
132 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
a35a2279
C
133 await generateVideoMiniature({
134 video: videoWithFiles,
135 videoFile: videoWithFiles.getMaxQualityFile(),
136 type: ThumbnailType.PREVIEW
137 })
053aed43
C
138 }
139
4a54a939 140 await publishAndFederateIfNeeded(videoWithFiles, true)
b5b68755
C
141}
142
b5b68755 143async function cleanupLiveFiles (hlsDirectory: string) {
bb4ba6d9
C
144 if (!await pathExists(hlsDirectory)) return
145
a5cf76af
C
146 const files = await readdir(hlsDirectory)
147
148 for (const filename of files) {
149 if (
150 filename.endsWith('.ts') ||
151 filename.endsWith('.m3u8') ||
152 filename.endsWith('.mpd') ||
153 filename.endsWith('.m4s') ||
2650d6d4 154 filename.endsWith('.tmp')
a5cf76af
C
155 ) {
156 const p = join(hlsDirectory, filename)
157
158 remove(p)
159 .catch(err => logger.error('Cannot remove %s.', p, { err }))
160 }
161 }
a5cf76af 162}