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