]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-transcoding.ts
add test and openapi for hot sort parameter
[github/Chocobozzz/PeerTube.git] / server / lib / video-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'
053aed43
C
4import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
5import { MStreamingPlaylistFilesVideo, MVideoFile, MVideoWithAllFiles, MVideoWithFile } from '@server/types/models'
5a547f69 6import { VideoResolution } from '../../shared/models/videos'
053aed43 7import { VideoStreamingPlaylistType } from '../../shared/models/videos/video-streaming-playlist.type'
5a547f69
C
8import { transcode, TranscodeOptions, TranscodeOptionsType } from '../helpers/ffmpeg-utils'
9import { canDoQuickTranscode, getDurationFromVideoFile, getMetadataFromFile, getVideoFileFPS } from '../helpers/ffprobe-utils'
098eb377 10import { logger } from '../helpers/logger'
053aed43 11import { CONFIG } from '../initializers/config'
5a547f69 12import { HLS_STREAMING_PLAYLIST_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, WEBSERVER } from '../initializers/constants'
098eb377 13import { VideoFileModel } from '../models/video/video-file'
09209296 14import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
053aed43 15import { updateMasterHLSPlaylist, updateSha256VODSegments } from './hls'
d7a25329 16import { generateVideoStreamingPlaylistName, getVideoFilename, getVideoFilePath } from './video-paths'
5a547f69 17import { availableEncoders } from './video-transcoding-profiles'
098eb377 18
658a47ab 19/**
6b67897e
C
20 *
21 * Functions that run transcoding functions, update the database, cleanup files, create torrent files...
22 * Mainly called by the job queue
23 *
658a47ab 24 */
6b67897e
C
25
26// Optimize the original video file and replace it. The resolution is not changed.
3b01f4c0 27async function optimizeOriginalVideofile (video: MVideoWithFile, inputVideoFile: MVideoFile, job?: Job) {
2fbd5e25 28 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377 29 const newExtname = '.mp4'
9f1ddd24 30
d7a25329 31 const videoInputPath = getVideoFilePath(video, inputVideoFile)
2fbd5e25 32 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 33
536598cf
C
34 const transcodeType: TranscodeOptionsType = await canDoQuickTranscode(videoInputPath)
35 ? 'quick-transcode'
36 : 'video'
5ba49f26 37
536598cf 38 const transcodeOptions: TranscodeOptions = {
d7a25329 39 type: transcodeType,
9252a33d 40
098eb377 41 inputPath: videoInputPath,
09209296 42 outputPath: videoTranscodedPath,
9252a33d
C
43
44 availableEncoders,
45 profile: 'default',
46
3b01f4c0
C
47 resolution: inputVideoFile.resolution,
48
49 job
098eb377
C
50 }
51
52 // Could be very long!
53 await transcode(transcodeOptions)
54
55 try {
56 await remove(videoInputPath)
57
58 // Important to do this before getVideoFilename() to take in account the new file extension
536598cf 59 inputVideoFile.extname = newExtname
2fbd5e25 60
d7a25329 61 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
098eb377 62
24516aa2 63 await onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
236841a1
C
64
65 return transcodeType
098eb377
C
66 } catch (err) {
67 // Auto destruction...
68 video.destroy().catch(err => logger.error('Cannot destruct video after transcoding failure.', { err }))
69
70 throw err
71 }
72}
73
6b67897e 74// Transcode the original video file to a lower resolution.
24516aa2 75async function transcodeNewWebTorrentResolution (video: MVideoWithFile, resolution: VideoResolution, isPortrait: boolean, job: Job) {
2fbd5e25 76 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
098eb377
C
77 const extname = '.mp4'
78
79 // We are sure it's x264 in mp4 because optimizeOriginalVideofile was already executed
d7a25329 80 const videoInputPath = getVideoFilePath(video, video.getMaxQualityFile())
098eb377
C
81
82 const newVideoFile = new VideoFileModel({
83 resolution,
84 extname,
85 size: 0,
86 videoId: video.id
87 })
d7a25329
C
88 const videoOutputPath = getVideoFilePath(video, newVideoFile)
89 const videoTranscodedPath = join(transcodeDirectory, getVideoFilename(video, newVideoFile))
098eb377 90
5c7d6508 91 const transcodeOptions = resolution === VideoResolution.H_NOVIDEO
92 ? {
3a149e9f 93 type: 'only-audio' as 'only-audio',
9252a33d 94
3a149e9f
C
95 inputPath: videoInputPath,
96 outputPath: videoTranscodedPath,
9252a33d
C
97
98 availableEncoders,
99 profile: 'default',
100
3b01f4c0
C
101 resolution,
102
103 job
3a149e9f 104 }
5c7d6508 105 : {
3a149e9f
C
106 type: 'video' as 'video',
107 inputPath: videoInputPath,
108 outputPath: videoTranscodedPath,
9252a33d
C
109
110 availableEncoders,
111 profile: 'default',
112
3a149e9f 113 resolution,
3b01f4c0
C
114 isPortraitMode: isPortrait,
115
116 job
3a149e9f 117 }
098eb377
C
118
119 await transcode(transcodeOptions)
120
24516aa2 121 return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
536598cf
C
122}
123
6b67897e 124// Merge an image with an audio file to create a video
3b01f4c0 125async function mergeAudioVideofile (video: MVideoWithAllFiles, resolution: VideoResolution, job: Job) {
536598cf
C
126 const transcodeDirectory = CONFIG.STORAGE.TMP_DIR
127 const newExtname = '.mp4'
128
92e0f42e 129 const inputVideoFile = video.getMinQualityFile()
2fbd5e25 130
d7a25329 131 const audioInputPath = getVideoFilePath(video, inputVideoFile)
536598cf 132 const videoTranscodedPath = join(transcodeDirectory, video.id + '-transcoded' + newExtname)
098eb377 133
eba06469
C
134 // If the user updates the video preview during transcoding
135 const previewPath = video.getPreview().getPath()
136 const tmpPreviewPath = join(CONFIG.STORAGE.TMP_DIR, basename(previewPath))
137 await copyFile(previewPath, tmpPreviewPath)
138
536598cf
C
139 const transcodeOptions = {
140 type: 'merge-audio' as 'merge-audio',
9252a33d 141
eba06469 142 inputPath: tmpPreviewPath,
536598cf 143 outputPath: videoTranscodedPath,
9252a33d
C
144
145 availableEncoders,
146 profile: 'default',
147
536598cf 148 audioPath: audioInputPath,
3b01f4c0
C
149 resolution,
150
151 job
536598cf 152 }
098eb377 153
eba06469
C
154 try {
155 await transcode(transcodeOptions)
098eb377 156
eba06469
C
157 await remove(audioInputPath)
158 await remove(tmpPreviewPath)
159 } catch (err) {
160 await remove(tmpPreviewPath)
161 throw err
162 }
098eb377 163
536598cf
C
164 // Important to do this before getVideoFilename() to take in account the new file extension
165 inputVideoFile.extname = newExtname
166
d7a25329 167 const videoOutputPath = getVideoFilePath(video, inputVideoFile)
eba06469
C
168 // ffmpeg generated a new video file, so update the video duration
169 // See https://trac.ffmpeg.org/ticket/5456
170 video.duration = await getDurationFromVideoFile(videoTranscodedPath)
171 await video.save()
536598cf 172
24516aa2 173 return onWebTorrentVideoFileTranscoding(video, inputVideoFile, videoTranscodedPath, videoOutputPath)
098eb377
C
174}
175
2650d6d4 176// Concat TS segments from a live video to a fragmented mp4 HLS playlist
24516aa2 177async function generateHlsPlaylistResolutionFromTS (options: {
2650d6d4 178 video: MVideoWithFile
3851e732 179 concatenatedTsFilePath: string
2650d6d4
C
180 resolution: VideoResolution
181 isPortraitMode: boolean
e772bdf1 182 isAAC: boolean
2650d6d4 183}) {
3851e732
C
184 return generateHlsPlaylistCommon({
185 video: options.video,
186 resolution: options.resolution,
187 isPortraitMode: options.isPortraitMode,
188 inputPath: options.concatenatedTsFilePath,
e772bdf1
C
189 type: 'hls-from-ts' as 'hls-from-ts',
190 isAAC: options.isAAC
3851e732 191 })
2650d6d4
C
192}
193
6b67897e 194// Generate an HLS playlist from an input file, and update the master playlist
24516aa2 195function generateHlsPlaylistResolution (options: {
b5b68755
C
196 video: MVideoWithFile
197 videoInputPath: string
198 resolution: VideoResolution
199 copyCodecs: boolean
200 isPortraitMode: boolean
3b01f4c0 201 job?: Job
b5b68755 202}) {
2650d6d4
C
203 return generateHlsPlaylistCommon({
204 video: options.video,
205 resolution: options.resolution,
206 copyCodecs: options.copyCodecs,
207 isPortraitMode: options.isPortraitMode,
208 inputPath: options.videoInputPath,
3b01f4c0
C
209 type: 'hls' as 'hls',
210 job: options.job
2650d6d4
C
211 })
212}
213
454c20fa
RK
214function getEnabledResolutions (type: 'vod' | 'live') {
215 const transcoding = type === 'vod'
216 ? CONFIG.TRANSCODING
217 : CONFIG.LIVE.TRANSCODING
218
219 return Object.keys(transcoding.RESOLUTIONS)
220 .filter(key => transcoding.ENABLED && transcoding.RESOLUTIONS[key] === true)
221 .map(r => parseInt(r, 10))
222}
223
2650d6d4
C
224// ---------------------------------------------------------------------------
225
226export {
24516aa2
C
227 generateHlsPlaylistResolution,
228 generateHlsPlaylistResolutionFromTS,
2650d6d4 229 optimizeOriginalVideofile,
24516aa2 230 transcodeNewWebTorrentResolution,
454c20fa
RK
231 mergeAudioVideofile,
232 getEnabledResolutions
2650d6d4
C
233}
234
235// ---------------------------------------------------------------------------
236
24516aa2
C
237async function onWebTorrentVideoFileTranscoding (
238 video: MVideoWithFile,
239 videoFile: MVideoFile,
240 transcodingPath: string,
241 outputPath: string
242) {
2650d6d4
C
243 const stats = await stat(transcodingPath)
244 const fps = await getVideoFileFPS(transcodingPath)
245 const metadata = await getMetadataFromFile(transcodingPath)
246
247 await move(transcodingPath, outputPath, { overwrite: true })
248
249 videoFile.size = stats.size
250 videoFile.fps = fps
251 videoFile.metadata = metadata
252
253 await createTorrentAndSetInfoHash(video, videoFile)
254
255 await VideoFileModel.customUpsert(videoFile, 'video', undefined)
256 video.VideoFiles = await video.$get('VideoFiles')
257
258 return video
259}
260
261async function generateHlsPlaylistCommon (options: {
262 type: 'hls' | 'hls-from-ts'
263 video: MVideoWithFile
264 inputPath: string
265 resolution: VideoResolution
266 copyCodecs?: boolean
e772bdf1 267 isAAC?: boolean
2650d6d4 268 isPortraitMode: boolean
3b01f4c0
C
269
270 job?: Job
2650d6d4 271}) {
3b01f4c0 272 const { type, video, inputPath, resolution, copyCodecs, isPortraitMode, isAAC, job } = options
b5b68755 273
9c6ca37f
C
274 const baseHlsDirectory = join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid)
275 await ensureDir(join(HLS_STREAMING_PLAYLIST_DIRECTORY, video.uuid))
09209296 276
09209296 277 const outputPath = join(baseHlsDirectory, VideoStreamingPlaylistModel.getHlsPlaylistFilename(resolution))
d7a25329 278 const videoFilename = generateVideoStreamingPlaylistName(video.uuid, resolution)
09209296
C
279
280 const transcodeOptions = {
2650d6d4 281 type,
9252a33d 282
2650d6d4 283 inputPath,
09209296 284 outputPath,
9252a33d
C
285
286 availableEncoders,
287 profile: 'default',
288
09209296 289 resolution,
d7a25329 290 copyCodecs,
09209296 291 isPortraitMode,
4c280004 292
e772bdf1
C
293 isAAC,
294
4c280004 295 hlsPlaylist: {
d7a25329 296 videoFilename
3b01f4c0
C
297 },
298
299 job
09209296
C
300 }
301
d7a25329 302 await transcode(transcodeOptions)
09209296 303
6dd9de95 304 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
09209296 305
d7a25329 306 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
09209296
C
307 videoId: video.id,
308 playlistUrl,
c6c0fa6c 309 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
b5b68755 310 p2pMediaLoaderInfohashes: [],
594d0c6a 311 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
09209296
C
312
313 type: VideoStreamingPlaylistType.HLS
d7a25329
C
314 }, { returning: true }) as [ MStreamingPlaylistFilesVideo, boolean ]
315 videoStreamingPlaylist.Video = video
316
317 const newVideoFile = new VideoFileModel({
318 resolution,
319 extname: extnameUtil(videoFilename),
320 size: 0,
321 fps: -1,
322 videoStreamingPlaylistId: videoStreamingPlaylist.id
09209296 323 })
d7a25329
C
324
325 const videoFilePath = getVideoFilePath(videoStreamingPlaylist, newVideoFile)
326 const stats = await stat(videoFilePath)
327
328 newVideoFile.size = stats.size
329 newVideoFile.fps = await getVideoFileFPS(videoFilePath)
8319d6ae 330 newVideoFile.metadata = await getMetadataFromFile(videoFilePath)
d7a25329
C
331
332 await createTorrentAndSetInfoHash(videoStreamingPlaylist, newVideoFile)
333
c547bbf9 334 await VideoFileModel.customUpsert(newVideoFile, 'streaming-playlist', undefined)
e6122097 335 videoStreamingPlaylist.VideoFiles = await videoStreamingPlaylist.$get('VideoFiles')
d7a25329 336
b5b68755
C
337 videoStreamingPlaylist.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(
338 playlistUrl, videoStreamingPlaylist.VideoFiles
339 )
340 await videoStreamingPlaylist.save()
341
d7a25329
C
342 video.setHLSPlaylist(videoStreamingPlaylist)
343
344 await updateMasterHLSPlaylist(video)
c6c0fa6c 345 await updateSha256VODSegments(video)
d7a25329 346
2650d6d4 347 return outputPath
098eb377 348}