2022-08-08 03:42:08 -05:00
|
|
|
import { Job } from 'bullmq'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { remove } from 'fs-extra'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { join } from 'path'
|
2023-04-21 07:55:10 -05:00
|
|
|
import { getFFmpegCommandWrapperOptions } from '@server/helpers/ffmpeg'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { VideoTranscodingProfilesManager } from '@server/lib/transcoding/default-transcoding-profiles'
|
|
|
|
import { isAbleToUploadVideo } from '@server/lib/user'
|
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { approximateIntroOutroAdditionalSize, onVideoEditionEnded, safeCleanupStudioTMPFiles } from '@server/lib/video-studio'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { UserModel } from '@server/models/user/user'
|
|
|
|
import { VideoModel } from '@server/models/video/video'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { MVideo, MVideoFullLight } from '@server/types/models'
|
|
|
|
import { pick } from '@shared/core-utils'
|
|
|
|
import { buildUUID } from '@shared/extra-utils'
|
|
|
|
import { FFmpegEdition } from '@shared/ffmpeg'
|
2022-02-11 03:51:33 -06:00
|
|
|
import {
|
2022-03-22 10:58:49 -05:00
|
|
|
VideoStudioEditionPayload,
|
2022-07-29 07:50:41 -05:00
|
|
|
VideoStudioTask,
|
2022-03-22 10:58:49 -05:00
|
|
|
VideoStudioTaskCutPayload,
|
|
|
|
VideoStudioTaskIntroPayload,
|
|
|
|
VideoStudioTaskOutroPayload,
|
2022-07-29 07:50:41 -05:00
|
|
|
VideoStudioTaskPayload,
|
|
|
|
VideoStudioTaskWatermarkPayload
|
2022-02-11 03:51:33 -06:00
|
|
|
} from '@shared/models'
|
|
|
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
|
|
|
|
|
|
|
const lTagsBase = loggerTagsFactory('video-edition')
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
async function processVideoStudioEdition (job: Job) {
|
|
|
|
const payload = job.data as VideoStudioEditionPayload
|
2022-03-22 08:35:04 -05:00
|
|
|
const lTags = lTagsBase(payload.videoUUID)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2022-08-08 08:48:17 -05:00
|
|
|
logger.info('Process video studio edition of %s in job %s.', payload.videoUUID, job.id, lTags)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
try {
|
|
|
|
const video = await VideoModel.loadFull(payload.videoUUID)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
// No video, maybe deleted?
|
|
|
|
if (!video) {
|
|
|
|
logger.info('Can\'t process job %d, video does not exist.', job.id, lTags)
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
await safeCleanupStudioTMPFiles(payload.tasks)
|
2023-05-03 08:17:11 -05:00
|
|
|
return undefined
|
|
|
|
}
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
await checkUserQuotaOrThrow(video, payload)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
const inputFile = video.getMaxQualityFile()
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
const editionResultPath = await VideoPathManager.Instance.makeAvailableVideoFile(inputFile, async originalFilePath => {
|
|
|
|
let tmpInputFilePath: string
|
|
|
|
let outputPath: string
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
for (const task of payload.tasks) {
|
|
|
|
const outputFilename = buildUUID() + inputFile.extname
|
|
|
|
outputPath = join(CONFIG.STORAGE.TMP_DIR, outputFilename)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
await processTask({
|
|
|
|
inputPath: tmpInputFilePath ?? originalFilePath,
|
|
|
|
video,
|
|
|
|
outputPath,
|
|
|
|
task,
|
|
|
|
lTags
|
|
|
|
})
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
if (tmpInputFilePath) await remove(tmpInputFilePath)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
// For the next iteration
|
|
|
|
tmpInputFilePath = outputPath
|
|
|
|
}
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
return outputPath
|
|
|
|
})
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
logger.info('Video edition ended for video %s.', video.uuid, lTags)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
await onVideoEditionEnded({ video, editionResultPath, tasks: payload.tasks })
|
2023-05-03 08:17:11 -05:00
|
|
|
} catch (err) {
|
2023-05-04 08:29:34 -05:00
|
|
|
await safeCleanupStudioTMPFiles(payload.tasks)
|
2023-05-03 08:17:11 -05:00
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2022-03-22 10:58:49 -05:00
|
|
|
processVideoStudioEdition
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
type TaskProcessorOptions <T extends VideoStudioTaskPayload = VideoStudioTaskPayload> = {
|
2022-02-11 03:51:33 -06:00
|
|
|
inputPath: string
|
|
|
|
outputPath: string
|
|
|
|
video: MVideo
|
|
|
|
task: T
|
2022-03-22 08:35:04 -05:00
|
|
|
lTags: { tags: string[] }
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
const taskProcessors: { [id in VideoStudioTask['name']]: (options: TaskProcessorOptions) => Promise<any> } = {
|
2022-02-11 03:51:33 -06:00
|
|
|
'add-intro': processAddIntroOutro,
|
|
|
|
'add-outro': processAddIntroOutro,
|
|
|
|
'cut': processCut,
|
|
|
|
'add-watermark': processAddWatermark
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processTask (options: TaskProcessorOptions) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const { video, task, lTags } = options
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
logger.info('Processing %s task for video %s.', task.name, video.uuid, { task, ...lTags })
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
const processor = taskProcessors[options.task.name]
|
|
|
|
if (!process) throw new Error('Unknown task ' + task.name)
|
|
|
|
|
|
|
|
return processor(options)
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
function processAddIntroOutro (options: TaskProcessorOptions<VideoStudioTaskIntroPayload | VideoStudioTaskOutroPayload>) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const { task, lTags } = options
|
|
|
|
|
|
|
|
logger.debug('Will add intro/outro to the video.', { options, ...lTags })
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
return buildFFmpegEdition().addIntroOutro({
|
2022-02-11 03:51:33 -06:00
|
|
|
...pick(options, [ 'inputPath', 'outputPath' ]),
|
|
|
|
|
|
|
|
introOutroPath: task.options.file,
|
|
|
|
type: task.name === 'add-intro'
|
|
|
|
? 'intro'
|
2023-04-21 07:55:10 -05:00
|
|
|
: 'outro'
|
2022-02-11 03:51:33 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
function processCut (options: TaskProcessorOptions<VideoStudioTaskCutPayload>) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const { task, lTags } = options
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
logger.debug('Will cut the video.', { options, ...lTags })
|
|
|
|
|
|
|
|
return buildFFmpegEdition().cutVideo({
|
2022-02-11 03:51:33 -06:00
|
|
|
...pick(options, [ 'inputPath', 'outputPath' ]),
|
|
|
|
|
|
|
|
start: task.options.start,
|
2023-04-21 07:55:10 -05:00
|
|
|
end: task.options.end
|
2022-02-11 03:51:33 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
function processAddWatermark (options: TaskProcessorOptions<VideoStudioTaskWatermarkPayload>) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const { task, lTags } = options
|
|
|
|
|
|
|
|
logger.debug('Will add watermark to the video.', { options, ...lTags })
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
return buildFFmpegEdition().addWatermark({
|
2022-02-11 03:51:33 -06:00
|
|
|
...pick(options, [ 'inputPath', 'outputPath' ]),
|
|
|
|
|
|
|
|
watermarkPath: task.options.file,
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
videoFilters: {
|
2023-05-04 08:29:34 -05:00
|
|
|
watermarkSizeRatio: task.options.watermarkSizeRatio,
|
|
|
|
horitonzalMarginRatio: task.options.horitonzalMarginRatio,
|
|
|
|
verticalMarginRatio: task.options.verticalMarginRatio
|
2023-04-21 07:55:10 -05:00
|
|
|
}
|
2022-02-11 03:51:33 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
async function checkUserQuotaOrThrow (video: MVideoFullLight, payload: VideoStudioEditionPayload) {
|
2022-02-11 03:51:33 -06:00
|
|
|
const user = await UserModel.loadByVideoId(video.id)
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
const filePathFinder = (i: number) => (payload.tasks[i] as VideoStudioTaskIntroPayload | VideoStudioTaskOutroPayload).options.file
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
const additionalBytes = await approximateIntroOutroAdditionalSize(video, payload.tasks, filePathFinder)
|
|
|
|
if (await isAbleToUploadVideo(user.id, additionalBytes) === false) {
|
|
|
|
throw new Error('Quota exceeded for this user to edit the video')
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
function buildFFmpegEdition () {
|
|
|
|
return new FFmpegEdition(getFFmpegCommandWrapperOptions('vod', VideoTranscodingProfilesManager.Instance.getAvailableEncoders()))
|
|
|
|
}
|