]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-studio-edition.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-studio-edition.ts
index 991d11ef153498c7b58d38d132d5e895cec6636d..caf051bfa7d6e532f6313c5ea374342dd5b6e2e0 100644 (file)
@@ -1,25 +1,18 @@
 import { Job } from 'bullmq'
-import { move, remove } from 'fs-extra'
+import { remove } from 'fs-extra'
 import { join } from 'path'
 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 { buildFileMetadata, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file'
 import { VideoPathManager } from '@server/lib/video-path-manager'
-import { approximateIntroOutroAdditionalSize } from '@server/lib/video-studio'
+import { approximateIntroOutroAdditionalSize, onVideoStudioEnded, safeCleanupStudioTMPFiles } from '@server/lib/video-studio'
 import { UserModel } from '@server/models/user/user'
 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 { buildUUID, getFileSize } from '@shared/extra-utils'
-import { FFmpegEdition, ffprobePromise, getVideoStreamDimensionsInfo, getVideoStreamDuration, getVideoStreamFPS } from '@shared/ffmpeg'
+import { MVideo, MVideoFullLight } from '@server/types/models'
+import { pick } from '@shared/core-utils'
+import { buildUUID } from '@shared/extra-utils'
+import { FFmpegEdition } from '@shared/ffmpeg'
 import {
   VideoStudioEditionPayload,
   VideoStudioTask,
@@ -31,7 +24,7 @@ import {
 } from '@shared/models'
 import { logger, loggerTagsFactory } from '../../../helpers/logger'
 
-const lTagsBase = loggerTagsFactory('video-edition')
+const lTagsBase = loggerTagsFactory('video-studio')
 
 async function processVideoStudioEdition (job: Job) {
   const payload = job.data as VideoStudioEditionPayload
@@ -39,63 +32,54 @@ async function processVideoStudioEdition (job: Job) {
 
   logger.info('Process video studio edition of %s in job %s.', payload.videoUUID, job.id, lTags)
 
-  const video = await VideoModel.loadFull(payload.videoUUID)
+  try {
+    const video = await VideoModel.loadFull(payload.videoUUID)
 
-  // No video, maybe deleted?
-  if (!video) {
-    logger.info('Can\'t process job %d, video does not exist.', job.id, lTags)
-    return undefined
-  }
-
-  await checkUserQuotaOrThrow(video, payload)
-
-  const inputFile = video.getMaxQualityFile()
-
-  const editionResultPath = await VideoPathManager.Instance.makeAvailableVideoFile(inputFile, async originalFilePath => {
-    let tmpInputFilePath: string
-    let outputPath: string
-
-    for (const task of payload.tasks) {
-      const outputFilename = buildUUID() + inputFile.extname
-      outputPath = join(CONFIG.STORAGE.TMP_DIR, outputFilename)
-
-      await processTask({
-        inputPath: tmpInputFilePath ?? originalFilePath,
-        video,
-        outputPath,
-        task,
-        lTags
-      })
+    // No video, maybe deleted?
+    if (!video) {
+      logger.info('Can\'t process job %d, video does not exist.', job.id, lTags)
 
-      if (tmpInputFilePath) await remove(tmpInputFilePath)
-
-      // For the next iteration
-      tmpInputFilePath = outputPath
+      await safeCleanupStudioTMPFiles(payload.tasks)
+      return undefined
     }
 
-    return outputPath
-  })
+    await checkUserQuotaOrThrow(video, payload)
 
-  logger.info('Video edition ended for video %s.', video.uuid, lTags)
+    const inputFile = video.getMaxQualityFile()
 
-  const newFile = await buildNewFile(video, editionResultPath)
+    const editionResultPath = await VideoPathManager.Instance.makeAvailableVideoFile(inputFile, async originalFilePath => {
+      let tmpInputFilePath: string
+      let outputPath: string
 
-  const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newFile)
-  await move(editionResultPath, outputPath)
+      for (const task of payload.tasks) {
+        const outputFilename = buildUUID() + inputFile.extname
+        outputPath = join(CONFIG.STORAGE.TMP_DIR, outputFilename)
 
-  await createTorrentAndSetInfoHashFromPath(video, newFile, outputPath)
-  await removeAllFiles(video, newFile)
+        await processTask({
+          inputPath: tmpInputFilePath ?? originalFilePath,
+          video,
+          outputPath,
+          task,
+          lTags
+        })
 
-  await newFile.save()
+        if (tmpInputFilePath) await remove(tmpInputFilePath)
 
-  video.duration = await getVideoStreamDuration(outputPath)
-  await video.save()
+        // For the next iteration
+        tmpInputFilePath = outputPath
+      }
 
-  await federateVideoIfNeeded(video, false, undefined)
+      return outputPath
+    })
 
-  const user = await UserModel.loadByVideoId(video.id)
+    logger.info('Video edition ended for video %s.', video.uuid, lTags)
 
-  await createOptimizeOrMergeAudioJobs({ video, videoFile: newFile, isNewVideo: false, user })
+    await onVideoStudioEnded({ video, editionResultPath, tasks: payload.tasks })
+  } catch (err) {
+    await safeCleanupStudioTMPFiles(payload.tasks)
+
+    throw err
+  }
 }
 
 // ---------------------------------------------------------------------------
@@ -171,44 +155,15 @@ function processAddWatermark (options: TaskProcessorOptions<VideoStudioTaskWater
     watermarkPath: task.options.file,
 
     videoFilters: {
-      watermarkSizeRatio: VIDEO_FILTERS.WATERMARK.SIZE_RATIO,
-      horitonzalMarginRatio: VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO,
-      verticalMarginRatio: VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO
+      watermarkSizeRatio: task.options.watermarkSizeRatio,
+      horitonzalMarginRatio: task.options.horitonzalMarginRatio,
+      verticalMarginRatio: task.options.verticalMarginRatio
     }
   })
 }
 
 // ---------------------------------------------------------------------------
 
-async function buildNewFile (video: MVideoId, path: string) {
-  const videoFile = new VideoFileModel({
-    extname: getLowercaseExtension(path),
-    size: await getFileSize(path),
-    metadata: await buildFileMetadata(path),
-    videoStreamingPlaylistId: null,
-    videoId: video.id
-  })
-
-  const probe = await ffprobePromise(path)
-
-  videoFile.fps = await getVideoStreamFPS(path, probe)
-  videoFile.resolution = (await getVideoStreamDimensionsInfo(path, probe)).resolution
-
-  videoFile.filename = generateWebTorrentVideoFilename(videoFile.resolution, videoFile.extname)
-
-  return videoFile
-}
-
-async function removeAllFiles (video: MVideoWithAllFiles, webTorrentFileException: MVideoFile) {
-  await removeHLSPlaylist(video)
-
-  for (const file of video.VideoFiles) {
-    if (file.id === webTorrentFileException.id) continue
-
-    await removeWebTorrentFile(video, file.id)
-  }
-}
-
 async function checkUserQuotaOrThrow (video: MVideoFullLight, payload: VideoStudioEditionPayload) {
   const user = await UserModel.loadByVideoId(video.id)