]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-studio-edition.ts
Enable external plugins to test the PR
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-studio-edition.ts
index cf3064a7ace5e5c8e34e2611dccbf07edb554214..fbb55a388631f3cb8041049f442f08154ebfb791 100644 (file)
@@ -1,14 +1,16 @@
-import { Job } from 'bull'
+import { Job } from 'bullmq'
 import { move, remove } from 'fs-extra'
 import { join } from 'path'
-import { addIntroOutro, addWatermark, cutVideo } from '@server/helpers/ffmpeg'
+import { getFFmpegCommandWrapperOptions } from '@server/helpers/ffmpeg'
 import { createTorrentAndSetInfoHashFromPath } from '@server/helpers/webtorrent'
 import { CONFIG } from '@server/initializers/config'
+import { VIDEO_FILTERS } from '@server/initializers/constants'
 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
 import { generateWebTorrentVideoFilename } from '@server/lib/paths'
+import { createOptimizeOrMergeAudioJobs } from '@server/lib/transcoding/create-transcoding-job'
 import { VideoTranscodingProfilesManager } from '@server/lib/transcoding/default-transcoding-profiles'
 import { isAbleToUploadVideo } from '@server/lib/user'
-import { addOptimizeOrMergeAudioJob } from '@server/lib/video'
+import { buildFileMetadata, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file'
 import { VideoPathManager } from '@server/lib/video-path-manager'
 import { approximateIntroOutroAdditionalSize } from '@server/lib/video-studio'
 import { UserModel } from '@server/models/user/user'
@@ -16,23 +18,16 @@ import { VideoModel } from '@server/models/video/video'
 import { VideoFileModel } from '@server/models/video/video-file'
 import { MVideo, MVideoFile, MVideoFullLight, MVideoId, MVideoWithAllFiles } from '@server/types/models'
 import { getLowercaseExtension, pick } from '@shared/core-utils'
-import {
-  buildFileMetadata,
-  buildUUID,
-  ffprobePromise,
-  getFileSize,
-  getVideoStreamDimensionsInfo,
-  getVideoStreamDuration,
-  getVideoStreamFPS
-} from '@shared/extra-utils'
+import { buildUUID, getFileSize } from '@shared/extra-utils'
+import { FFmpegEdition, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamDuration, getVideoStreamFPS } from '@shared/ffmpeg'
 import {
   VideoStudioEditionPayload,
-  VideoStudioTaskPayload,
+  VideoStudioTask,
   VideoStudioTaskCutPayload,
   VideoStudioTaskIntroPayload,
   VideoStudioTaskOutroPayload,
-  VideoStudioTaskWatermarkPayload,
-  VideoStudioTask
+  VideoStudioTaskPayload,
+  VideoStudioTaskWatermarkPayload
 } from '@shared/models'
 import { logger, loggerTagsFactory } from '../../../helpers/logger'
 
@@ -42,9 +37,9 @@ async function processVideoStudioEdition (job: Job) {
   const payload = job.data as VideoStudioEditionPayload
   const lTags = lTagsBase(payload.videoUUID)
 
-  logger.info('Process video studio edition of %s in job %d.', payload.videoUUID, job.id, lTags)
+  logger.info('Process video studio edition of %s in job %s.', payload.videoUUID, job.id, lTags)
 
-  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
+  const video = await VideoModel.loadFull(payload.videoUUID)
 
   // No video, maybe deleted?
   if (!video) {
@@ -89,7 +84,6 @@ async function processVideoStudioEdition (job: Job) {
   await move(editionResultPath, outputPath)
 
   await createTorrentAndSetInfoHashFromPath(video, newFile, outputPath)
-
   await removeAllFiles(video, newFile)
 
   await newFile.save()
@@ -100,7 +94,8 @@ async function processVideoStudioEdition (job: Job) {
   await federateVideoIfNeeded(video, false, undefined)
 
   const user = await UserModel.loadByVideoId(video.id)
-  await addOptimizeOrMergeAudioJob({ video, videoFile: newFile, user, isNewVideo: false })
+
+  await createOptimizeOrMergeAudioJobs({ video, videoFile: newFile, isNewVideo: false, user, videoFileAlreadyLocked: false })
 }
 
 // ---------------------------------------------------------------------------
@@ -127,9 +122,9 @@ const taskProcessors: { [id in VideoStudioTask['name']]: (options: TaskProcessor
 }
 
 async function processTask (options: TaskProcessorOptions) {
-  const { video, task } = options
+  const { video, task, lTags } = options
 
-  logger.info('Processing %s task for video %s.', task.name, video.uuid, { task, ...options.lTags })
+  logger.info('Processing %s task for video %s.', task.name, video.uuid, { task, ...lTags })
 
   const processor = taskProcessors[options.task.name]
   if (!process) throw new Error('Unknown task ' + task.name)
@@ -138,25 +133,26 @@ async function processTask (options: TaskProcessorOptions) {
 }
 
 function processAddIntroOutro (options: TaskProcessorOptions<VideoStudioTaskIntroPayload | VideoStudioTaskOutroPayload>) {
-  const { task } = options
+  const { task, lTags } = options
+
+  logger.debug('Will add intro/outro to the video.', { options, ...lTags })
 
-  return addIntroOutro({
+  return buildFFmpegEdition().addIntroOutro({
     ...pick(options, [ 'inputPath', 'outputPath' ]),
 
     introOutroPath: task.options.file,
     type: task.name === 'add-intro'
       ? 'intro'
-      : 'outro',
-
-    availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
-    profile: CONFIG.TRANSCODING.PROFILE
+      : 'outro'
   })
 }
 
 function processCut (options: TaskProcessorOptions<VideoStudioTaskCutPayload>) {
-  const { task } = options
+  const { task, lTags } = options
+
+  logger.debug('Will cut the video.', { options, ...lTags })
 
-  return cutVideo({
+  return buildFFmpegEdition().cutVideo({
     ...pick(options, [ 'inputPath', 'outputPath' ]),
 
     start: task.options.start,
@@ -165,18 +161,25 @@ function processCut (options: TaskProcessorOptions<VideoStudioTaskCutPayload>) {
 }
 
 function processAddWatermark (options: TaskProcessorOptions<VideoStudioTaskWatermarkPayload>) {
-  const { task } = options
+  const { task, lTags } = options
 
-  return addWatermark({
+  logger.debug('Will add watermark to the video.', { options, ...lTags })
+
+  return buildFFmpegEdition().addWatermark({
     ...pick(options, [ 'inputPath', 'outputPath' ]),
 
     watermarkPath: task.options.file,
 
-    availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
-    profile: CONFIG.TRANSCODING.PROFILE
+    videoFilters: {
+      watermarkSizeRatio: VIDEO_FILTERS.WATERMARK.SIZE_RATIO,
+      horitonzalMarginRatio: VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO,
+      verticalMarginRatio: VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO
+    }
   })
 }
 
+// ---------------------------------------------------------------------------
+
 async function buildNewFile (video: MVideoId, path: string) {
   const videoFile = new VideoFileModel({
     extname: getLowercaseExtension(path),
@@ -197,18 +200,12 @@ async function buildNewFile (video: MVideoId, path: string) {
 }
 
 async function removeAllFiles (video: MVideoWithAllFiles, webTorrentFileException: MVideoFile) {
-  const hls = video.getHLSPlaylist()
-
-  if (hls) {
-    await video.removeStreamingPlaylistFiles(hls)
-    await hls.destroy()
-  }
+  await removeHLSPlaylist(video)
 
   for (const file of video.VideoFiles) {
     if (file.id === webTorrentFileException.id) continue
 
-    await video.removeWebTorrentFileAndTorrent(file)
-    await file.destroy()
+    await removeWebTorrentFile(video, file.id)
   }
 }
 
@@ -222,3 +219,7 @@ async function checkUserQuotaOrThrow (video: MVideoFullLight, payload: VideoStud
     throw new Error('Quota exceeded for this user to edit the video')
   }
 }
+
+function buildFFmpegEdition () {
+  return new FFmpegEdition(getFFmpegCommandWrapperOptions('vod', VideoTranscodingProfilesManager.Instance.getAvailableEncoders()))
+}