]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/transcoding/transcoding.ts
Prevent duplicated HLS playlist on transcoding
[github/Chocobozzz/PeerTube.git] / server / lib / transcoding / transcoding.ts
1 import { Job } from 'bull'
2 import { copyFile, ensureDir, move, remove, stat } from 'fs-extra'
3 import { basename, extname as extnameUtil, join } from 'path'
4 import { toEven } from '@server/helpers/core-utils'
5 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6 import { MStreamingPlaylistFilesVideo, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
7 import { VideoResolution, VideoStorage } from '../../../shared/models/videos'
8 import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type'
9 import {
10 buildFileMetadata,
11 canDoQuickTranscode,
12 getVideoStreamDuration,
13 getVideoStreamFPS,
14 transcodeVOD,
15 TranscodeVODOptions,
16 TranscodeVODOptionsType
17 } from '../../helpers/ffmpeg'
18 import { CONFIG } from '../../initializers/config'
19 import { P2P_MEDIA_LOADER_PEER_VERSION } from '../../initializers/constants'
20 import { VideoFileModel } from '../../models/video/video-file'
21 import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist'
22 import { updateMasterHLSPlaylist, updateSha256VODSegments } from '../hls'
23 import {
24 generateHLSMasterPlaylistFilename,
25 generateHlsSha256SegmentsFilename,
26 generateHLSVideoFilename,
27 generateWebTorrentVideoFilename,
28 getHlsResolutionPlaylistFilename
29 } from '../paths'
30 import { VideoPathManager } from '../video-path-manager'
31 import { VideoTranscodingProfilesManager } from './default-transcoding-profiles'
32 import { retryTransactionWrapper } from '@server/helpers/database-utils'
33 import { sequelizeTypescript } from '@server/initializers/database'
34
35 /**
36 *
37 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
38 * Mainly called by the job queue
39 *
40 */
41
42 // Optimize the original video file and replace it. The resolution is not changed.
43 function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVideoFile, job?: Job) {
44 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
45 const newExtname = '.mp4'
46
47 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async videoInputPath => {
48 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
49
50 const transcodeType: TranscodeVODOptionsType = await canDoQuickTranscode(videoInputPath)
51 ? 'quick-transcode'
52 : 'video'
53
54 const resolution = toEven(inputVideoFile.resolution)
55
56 const transcodeOptions: TranscodeVODOptions = {
57 type: transcodeType,
58
59 inputPath: videoInputPath,
60 outputPath: videoTranscodedPath,
61
62 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
63 profile: CONFIG.TRANSCODING.PROFILE,
64
65 resolution,
66
67 job
68 }
69
70 // Could be very long!
71 await transcodeVOD(transcodeOptions)
72
73 // Important to do this before getVideoFilename() to take in account the new filename
74 inputVideoFile.extname = newExtname
75 inputVideoFile.filename = generateWebTorrentVideoFilename(resolution, newExtname)
76 inputVideoFile.storage = VideoStorage.FILE_SYSTEM
77
78 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
79
80 const { videoFile } = await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
81 await remove(videoInputPath)
82
83 return { transcodeType, videoFile }
84 })
85 }
86
87 // Transcode the original video file to a lower resolution
88 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
89 function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: VideoResolution, isPortrait: boolean, job: Job) {
90 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
91 const extname = '.mp4'
92
93 return VideoPathManager.Instance.makeAvailableVideoFile(video.getMaxQualityFile().withVideoOrPlaylist(video), async videoInputPath => {
94 const newVideoFile = new VideoFileModel({
95 resolution,
96 extname,
97 filename: generateWebTorrentVideoFilename(resolution, extname),
98 size: 0,
99 videoId: video.id
100 })
101
102 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newVideoFile)
103 const videoTranscodedPath = join(transcodeDirectory, newVideoFile.filename)
104
105 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
106 ? {
107 type: 'only-audio' as 'only-audio',
108
109 inputPath: videoInputPath,
110 outputPath: videoTranscodedPath,
111
112 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
113 profile: CONFIG.TRANSCODING.PROFILE,
114
115 resolution,
116
117 job
118 }
119 : {
120 type: 'video' as 'video',
121 inputPath: videoInputPath,
122 outputPath: videoTranscodedPath,
123
124 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
125 profile: CONFIG.TRANSCODING.PROFILE,
126
127 resolution,
128 isPortraitMode: isPortrait,
129
130 job
131 }
132
133 await transcodeVOD(transcodeOptions)
134
135 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
136 })
137 }
138
139 // Merge an image with an audio file to create a video
140 function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolution, job: Job) {
141 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
142 const newExtname = '.mp4'
143
144 const inputVideoFile = video.getMinQualityFile()
145
146 return VideoPathManager.Instance.makeAvailableVideoFile(inputVideoFile.withVideoOrPlaylist(video), async audioInputPath => {
147 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
148
149 // If the user updates the video preview during transcoding
150 const previewPath = video.getPreview().getPath()
151 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
152 await copyFile(previewPath, tmpPreviewPath)
153
154 const transcodeOptions = {
155 type: 'merge-audio' as 'merge-audio',
156
157 inputPath: tmpPreviewPath,
158 outputPath: videoTranscodedPath,
159
160 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
161 profile: CONFIG.TRANSCODING.PROFILE,
162
163 audioPath: audioInputPath,
164 resolution,
165
166 job
167 }
168
169 try {
170 await transcodeVOD(transcodeOptions)
171
172 await remove(audioInputPath)
173 await remove(tmpPreviewPath)
174 } catch (err) {
175 await remove(tmpPreviewPath)
176 throw err
177 }
178
179 // Important to do this before getVideoFilename() to take in account the new file extension
180 inputVideoFile.extname = newExtname
181 inputVideoFile.resolution = resolution
182 inputVideoFile.filename = generateWebTorrentVideoFilename(inputVideoFile.resolution, newExtname)
183
184 const videoOutputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, inputVideoFile)
185 // ffmpeg generated a new video file, so update the video duration
186 // See https://trac.ffmpeg.org/ticket/5456
187 video.duration = await getVideoStreamDuration(videoTranscodedPath)
188 await video.save()
189
190 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
191 })
192 }
193
194 // Concat TS segments from a live video to a fragmented mp4 HLS playlist
195 async function generateHlsPlaylistResolutionFromTS (options: {
196 video: MVideo
197 concatenatedTsFilePath: string
198 resolution: VideoResolution
199 isPortraitMode: boolean
200 isAAC: boolean
201 }) {
202 return generateHlsPlaylistCommon({
203 video: options.video,
204 resolution: options.resolution,
205 isPortraitMode: options.isPortraitMode,
206 inputPath: options.concatenatedTsFilePath,
207 type: 'hls-from-ts' as 'hls-from-ts',
208 isAAC: options.isAAC
209 })
210 }
211
212 // Generate an HLS playlist from an input file, and update the master playlist
213 function generateHlsPlaylistResolution (options: {
214 video: MVideo
215 videoInputPath: string
216 resolution: VideoResolution
217 copyCodecs: boolean
218 isPortraitMode: boolean
219 job?: Job
220 }) {
221 return generateHlsPlaylistCommon({
222 video: options.video,
223 resolution: options.resolution,
224 copyCodecs: options.copyCodecs,
225 isPortraitMode: options.isPortraitMode,
226 inputPath: options.videoInputPath,
227 type: 'hls' as 'hls',
228 job: options.job
229 })
230 }
231
232 // ---------------------------------------------------------------------------
233
234 export {
235 generateHlsPlaylistResolution,
236 generateHlsPlaylistResolutionFromTS,
237 optimizeOriginalVideofile,
238 transcodeNewWebTorrentResolution,
239 mergeAudioVideofile
240 }
241
242 // ---------------------------------------------------------------------------
243
244 async function onWebTorrentVideoFileTranscoding (
245 video: MVideoFullLight,
246 videoFile: MVideoFile,
247 transcodingPath: string,
248 outputPath: string
249 ) {
250 const stats = await stat(transcodingPath)
251 const fps = await getVideoStreamFPS(transcodingPath)
252 const metadata = await buildFileMetadata(transcodingPath)
253
254 await move(transcodingPath, outputPath, { overwrite: true })
255
256 videoFile.size = stats.size
257 videoFile.fps = fps
258 videoFile.metadata = metadata
259
260 await createTorrentAndSetInfoHash(video, videoFile)
261
262 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
263 video.VideoFiles = await video.$get('VideoFiles')
264
265 return { video, videoFile }
266 }
267
268 async function generateHlsPlaylistCommon (options: {
269 type: 'hls' | 'hls-from-ts'
270 video: MVideo
271 inputPath: string
272 resolution: VideoResolution
273 copyCodecs?: boolean
274 isAAC?: boolean
275 isPortraitMode: boolean
276
277 job?: Job
278 }) {
279 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
280 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
281
282 const videoTranscodedBasePath = join(transcodeDirectory, type)
283 await ensureDir(videoTranscodedBasePath)
284
285 const videoFilename = generateHLSVideoFilename(resolution)
286 const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFilename)
287 const resolutionPlaylistFileTranscodePath = join(videoTranscodedBasePath, resolutionPlaylistFilename)
288
289 const transcodeOptions = {
290 type,
291
292 inputPath,
293 outputPath: resolutionPlaylistFileTranscodePath,
294
295 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
296 profile: CONFIG.TRANSCODING.PROFILE,
297
298 resolution,
299 copyCodecs,
300 isPortraitMode,
301
302 isAAC,
303
304 hlsPlaylist: {
305 videoFilename
306 },
307
308 job
309 }
310
311 await transcodeVOD(transcodeOptions)
312
313 // Create or update the playlist
314 const playlist = await retryTransactionWrapper(() => {
315 return sequelizeTypescript.transaction(async transaction => {
316 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video, transaction)
317
318 if (!playlist.playlistFilename) {
319 playlist.playlistFilename = generateHLSMasterPlaylistFilename(video.isLive)
320 }
321
322 if (!playlist.segmentsSha256Filename) {
323 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(video.isLive)
324 }
325
326 playlist.p2pMediaLoaderInfohashes = []
327 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
328
329 playlist.type = VideoStreamingPlaylistType.HLS
330
331 await playlist.save({ transaction })
332
333 return playlist
334 })
335 })
336
337 // Build the new playlist file
338 const extname = extnameUtil(videoFilename)
339 const newVideoFile = new VideoFileModel({
340 resolution,
341 extname,
342 size: 0,
343 filename: videoFilename,
344 fps: -1,
345 videoStreamingPlaylistId: playlist.id
346 })
347
348 const videoFilePath = VideoPathManager.Instance.getFSVideoFileOutputPath(playlist, newVideoFile)
349
350 // Move files from tmp transcoded directory to the appropriate place
351 await ensureDir(VideoPathManager.Instance.getFSHLSOutputPath(video))
352
353 // Move playlist file
354 const resolutionPlaylistPath = VideoPathManager.Instance.getFSHLSOutputPath(video, resolutionPlaylistFilename)
355 await move(resolutionPlaylistFileTranscodePath, resolutionPlaylistPath, { overwrite: true })
356 // Move video file
357 await move(join(videoTranscodedBasePath, videoFilename), videoFilePath, { overwrite: true })
358
359 const stats = await stat(videoFilePath)
360
361 newVideoFile.size = stats.size
362 newVideoFile.fps = await getVideoStreamFPS(videoFilePath)
363 newVideoFile.metadata = await buildFileMetadata(videoFilePath)
364
365 await createTorrentAndSetInfoHash(playlist, newVideoFile)
366
367 const savedVideoFile = await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
368
369 const playlistWithFiles = playlist as MStreamingPlaylistFilesVideo
370 playlistWithFiles.VideoFiles = await playlist.$get('VideoFiles')
371 playlist.assignP2PMediaLoaderInfoHashes(video, playlistWithFiles.VideoFiles)
372
373 await playlist.save()
374
375 video.setHLSPlaylist(playlist)
376
377 await updateMasterHLSPlaylist(video, playlistWithFiles)
378 await updateSha256VODSegments(video, playlistWithFiles)
379
380 return { resolutionPlaylistPath, videoFile: savedVideoFile }
381 }