video-studio.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { move, remove } from 'fs-extra/esm'
  2. import { join } from 'path'
  3. import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
  4. import { createTorrentAndSetInfoHashFromPath } from '@server/helpers/webtorrent.js'
  5. import { CONFIG } from '@server/initializers/config.js'
  6. import { MUser, MVideo, MVideoFile, MVideoFullLight, MVideoWithAllFiles } from '@server/types/models/index.js'
  7. import { getVideoStreamDuration } from '@peertube/peertube-ffmpeg'
  8. import { VideoStudioEditionPayload, VideoStudioTask, VideoStudioTaskPayload } from '@peertube/peertube-models'
  9. import { JobQueue } from './job-queue/index.js'
  10. import { VideoStudioTranscodingJobHandler } from './runners/index.js'
  11. import { getTranscodingJobPriority } from './transcoding/transcoding-priority.js'
  12. import { buildNewFile, removeHLSPlaylist, removeWebVideoFile } from './video-file.js'
  13. import { VideoPathManager } from './video-path-manager.js'
  14. import { buildStoryboardJobIfNeeded } from './video-jobs.js'
  15. import { buildAspectRatio } from '@peertube/peertube-core-utils'
  16. const lTags = loggerTagsFactory('video-studio')
  17. export function buildTaskFileFieldname (indice: number, fieldName = 'file') {
  18. return `tasks[${indice}][options][${fieldName}]`
  19. }
  20. export function getTaskFileFromReq (files: Express.Multer.File[], indice: number, fieldName = 'file') {
  21. return files.find(f => f.fieldname === buildTaskFileFieldname(indice, fieldName))
  22. }
  23. export function getStudioTaskFilePath (filename: string) {
  24. return join(CONFIG.STORAGE.TMP_PERSISTENT_DIR, filename)
  25. }
  26. export async function safeCleanupStudioTMPFiles (tasks: VideoStudioTaskPayload[]) {
  27. logger.info('Removing studio task files', { tasks, ...lTags() })
  28. for (const task of tasks) {
  29. try {
  30. if (task.name === 'add-intro' || task.name === 'add-outro') {
  31. await remove(task.options.file)
  32. } else if (task.name === 'add-watermark') {
  33. await remove(task.options.file)
  34. }
  35. } catch (err) {
  36. logger.error('Cannot remove studio file', { err })
  37. }
  38. }
  39. }
  40. // ---------------------------------------------------------------------------
  41. export async function approximateIntroOutroAdditionalSize (
  42. video: MVideoFullLight,
  43. tasks: VideoStudioTask[],
  44. fileFinder: (i: number) => string
  45. ) {
  46. let additionalDuration = 0
  47. for (let i = 0; i < tasks.length; i++) {
  48. const task = tasks[i]
  49. if (task.name !== 'add-intro' && task.name !== 'add-outro') continue
  50. const filePath = fileFinder(i)
  51. additionalDuration += await getVideoStreamDuration(filePath)
  52. }
  53. return (video.getMaxQualityFile().size / video.duration) * additionalDuration
  54. }
  55. // ---------------------------------------------------------------------------
  56. export async function createVideoStudioJob (options: {
  57. video: MVideo
  58. user: MUser
  59. payload: VideoStudioEditionPayload
  60. }) {
  61. const { video, user, payload } = options
  62. const priority = await getTranscodingJobPriority({ user, type: 'studio', fallback: 0 })
  63. if (CONFIG.VIDEO_STUDIO.REMOTE_RUNNERS.ENABLED) {
  64. await new VideoStudioTranscodingJobHandler().create({ video, tasks: payload.tasks, priority })
  65. return
  66. }
  67. await JobQueue.Instance.createJob({ type: 'video-studio-edition', payload, priority })
  68. }
  69. export async function onVideoStudioEnded (options: {
  70. editionResultPath: string
  71. tasks: VideoStudioTaskPayload[]
  72. video: MVideoFullLight
  73. }) {
  74. const { video, tasks, editionResultPath } = options
  75. const newFile = await buildNewFile({ path: editionResultPath, mode: 'web-video' })
  76. newFile.videoId = video.id
  77. const outputPath = VideoPathManager.Instance.getFSVideoFileOutputPath(video, newFile)
  78. await move(editionResultPath, outputPath)
  79. await safeCleanupStudioTMPFiles(tasks)
  80. await createTorrentAndSetInfoHashFromPath(video, newFile, outputPath)
  81. await removeAllFiles(video, newFile)
  82. await newFile.save()
  83. video.duration = await getVideoStreamDuration(outputPath)
  84. video.aspectRatio = buildAspectRatio({ width: newFile.width, height: newFile.height })
  85. await video.save()
  86. return JobQueue.Instance.createSequentialJobFlow(
  87. buildStoryboardJobIfNeeded({ video, federate: false }),
  88. {
  89. type: 'federate-video' as 'federate-video',
  90. payload: {
  91. videoUUID: video.uuid,
  92. isNewVideoForFederation: false
  93. }
  94. },
  95. {
  96. type: 'transcoding-job-builder' as 'transcoding-job-builder',
  97. payload: {
  98. videoUUID: video.uuid,
  99. optimizeJob: {
  100. isNewVideo: false
  101. }
  102. }
  103. }
  104. )
  105. }
  106. // ---------------------------------------------------------------------------
  107. // Private
  108. // ---------------------------------------------------------------------------
  109. async function removeAllFiles (video: MVideoWithAllFiles, webVideoFileException: MVideoFile) {
  110. await removeHLSPlaylist(video)
  111. for (const file of video.VideoFiles) {
  112. if (file.id === webVideoFileException.id) continue
  113. await removeWebVideoFile(video, file.id)
  114. }
  115. }