2021-05-10 05:13:41 -04:00
|
|
|
import { UploadFiles } from 'express'
|
2022-08-08 09:48:17 -04:00
|
|
|
import memoizee from 'memoizee'
|
2020-09-17 04:00:46 -04:00
|
|
|
import { Transaction } from 'sequelize/types'
|
2022-08-08 09:48:17 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2022-06-17 08:34:37 -04:00
|
|
|
import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants'
|
2020-09-17 04:00:46 -04:00
|
|
|
import { TagModel } from '@server/models/video/tag'
|
2020-09-17 03:20:52 -04:00
|
|
|
import { VideoModel } from '@server/models/video/video'
|
2021-08-17 02:26:20 -04:00
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
2020-09-17 03:20:52 -04:00
|
|
|
import { FilteredModelAttributes } from '@server/types'
|
2021-07-23 05:20:00 -04:00
|
|
|
import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
|
2022-03-22 09:35:04 -04:00
|
|
|
import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
|
2022-08-09 03:09:31 -04:00
|
|
|
import { CreateJobOptions } from './job-queue/job-queue'
|
2021-06-04 03:19:01 -04:00
|
|
|
import { updateVideoMiniatureFromExisting } from './thumbnail'
|
2020-09-17 03:20:52 -04:00
|
|
|
|
2020-09-17 04:00:46 -04:00
|
|
|
function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
|
2020-09-17 03:20:52 -04:00
|
|
|
return {
|
|
|
|
name: videoInfo.name,
|
|
|
|
remote: false,
|
|
|
|
category: videoInfo.category,
|
2021-12-14 11:17:01 -05:00
|
|
|
licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
|
2020-09-17 03:20:52 -04:00
|
|
|
language: videoInfo.language,
|
2021-12-14 11:17:01 -05:00
|
|
|
commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
|
|
|
|
downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
|
2020-09-17 03:20:52 -04:00
|
|
|
waitTranscoding: videoInfo.waitTranscoding || false,
|
|
|
|
nsfw: videoInfo.nsfw || false,
|
|
|
|
description: videoInfo.description,
|
|
|
|
support: videoInfo.support,
|
|
|
|
privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
|
2022-07-13 05:58:01 -04:00
|
|
|
channelId,
|
2020-09-17 03:20:52 -04:00
|
|
|
originallyPublishedAt: videoInfo.originallyPublishedAt
|
2021-05-12 08:09:04 -04:00
|
|
|
? new Date(videoInfo.originallyPublishedAt)
|
|
|
|
: null
|
2020-09-17 03:20:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 04:00:46 -04:00
|
|
|
async function buildVideoThumbnailsFromReq (options: {
|
|
|
|
video: MVideoThumbnail
|
2021-05-10 05:13:41 -04:00
|
|
|
files: UploadFiles
|
2020-09-17 04:00:46 -04:00
|
|
|
fallback: (type: ThumbnailType) => Promise<MThumbnail>
|
|
|
|
automaticallyGenerated?: boolean
|
|
|
|
}) {
|
|
|
|
const { video, files, fallback, automaticallyGenerated } = options
|
|
|
|
|
|
|
|
const promises = [
|
|
|
|
{
|
|
|
|
type: ThumbnailType.MINIATURE,
|
|
|
|
fieldName: 'thumbnailfile'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: ThumbnailType.PREVIEW,
|
|
|
|
fieldName: 'previewfile'
|
|
|
|
}
|
|
|
|
].map(p => {
|
|
|
|
const fields = files?.[p.fieldName]
|
|
|
|
|
|
|
|
if (fields) {
|
2021-06-04 03:19:01 -04:00
|
|
|
return updateVideoMiniatureFromExisting({
|
2020-09-17 04:00:46 -04:00
|
|
|
inputPath: fields[0].path,
|
|
|
|
video,
|
|
|
|
type: p.type,
|
|
|
|
automaticallyGenerated: automaticallyGenerated || false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback(p.type)
|
|
|
|
})
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-09-17 04:00:46 -04:00
|
|
|
async function setVideoTags (options: {
|
|
|
|
video: MVideoTag
|
|
|
|
tags: string[]
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
2021-03-03 05:03:30 -05:00
|
|
|
const { video, tags, transaction } = options
|
2020-09-17 04:00:46 -04:00
|
|
|
|
2021-03-03 05:03:30 -05:00
|
|
|
const internalTags = tags || []
|
|
|
|
const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
|
|
|
|
|
|
|
|
await video.$set('Tags', tagInstances, { transaction })
|
|
|
|
video.Tags = tagInstances
|
2020-09-17 04:00:46 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:35:04 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
async function buildOptimizeOrMergeAudioJob (options: {
|
2022-03-22 09:35:04 -04:00
|
|
|
video: MVideoUUID
|
|
|
|
videoFile: MVideoFile
|
|
|
|
user: MUserId
|
|
|
|
isNewVideo?: boolean // Default true
|
|
|
|
}) {
|
|
|
|
const { video, videoFile, user, isNewVideo } = options
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
let payload: VideoTranscodingPayload
|
2021-01-21 10:57:21 -05:00
|
|
|
|
|
|
|
if (videoFile.isAudio()) {
|
2022-08-08 09:48:17 -04:00
|
|
|
payload = {
|
2021-01-21 10:57:21 -05:00
|
|
|
type: 'merge-audio-to-webtorrent',
|
|
|
|
resolution: DEFAULT_AUDIO_RESOLUTION,
|
|
|
|
videoUUID: video.uuid,
|
2022-02-01 05:16:45 -05:00
|
|
|
createHLSIfNeeded: true,
|
2022-02-11 04:51:33 -05:00
|
|
|
isNewVideo
|
2021-01-21 10:57:21 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-08 09:48:17 -04:00
|
|
|
payload = {
|
2021-01-21 10:57:21 -05:00
|
|
|
type: 'optimize-to-webtorrent',
|
|
|
|
videoUUID: video.uuid,
|
2022-02-11 04:51:33 -05:00
|
|
|
isNewVideo
|
2021-01-21 10:57:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
|
2021-01-21 10:57:21 -05:00
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
return {
|
|
|
|
type: 'video-transcoding' as 'video-transcoding',
|
|
|
|
priority: await getTranscodingJobPriority(user),
|
|
|
|
payload
|
|
|
|
}
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 03:09:31 -04:00
|
|
|
async function buildTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
|
2021-08-17 02:26:20 -04:00
|
|
|
await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
|
|
|
|
|
2022-08-09 03:09:31 -04:00
|
|
|
return { type: 'video-transcoding' as 'video-transcoding', payload, ...options }
|
2021-08-17 02:26:20 -04:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:25:11 -04:00
|
|
|
async function getTranscodingJobPriority (user: MUserId) {
|
2021-01-21 10:57:21 -05:00
|
|
|
const now = new Date()
|
|
|
|
const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
|
|
|
|
|
|
|
|
const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
|
|
|
|
|
2021-05-05 03:25:11 -04:00
|
|
|
return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
|
2021-01-21 10:57:21 -05:00
|
|
|
}
|
|
|
|
|
2020-09-17 03:20:52 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
async function buildMoveToObjectStorageJob (options: {
|
2022-03-22 09:35:04 -04:00
|
|
|
video: MVideoUUID
|
|
|
|
previousVideoState: VideoState
|
|
|
|
isNewVideo?: boolean // Default true
|
|
|
|
}) {
|
|
|
|
const { video, previousVideoState, isNewVideo = true } = options
|
|
|
|
|
|
|
|
await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
|
|
|
|
|
2022-08-08 09:48:17 -04:00
|
|
|
return {
|
|
|
|
type: 'move-to-object-storage' as 'move-to-object-storage',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
isNewVideo,
|
|
|
|
previousVideoState
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 09:35:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-06-17 08:34:37 -04:00
|
|
|
async function getVideoDuration (videoId: number | string) {
|
|
|
|
const video = await VideoModel.load(videoId)
|
|
|
|
|
|
|
|
const duration = video.isLive
|
|
|
|
? undefined
|
|
|
|
: video.duration
|
|
|
|
|
|
|
|
return { duration, isLive: video.isLive }
|
|
|
|
}
|
|
|
|
|
|
|
|
const getCachedVideoDuration = memoizee(getVideoDuration, {
|
|
|
|
promise: true,
|
|
|
|
max: MEMOIZE_LENGTH.VIDEO_DURATION,
|
|
|
|
maxAge: MEMOIZE_TTL.VIDEO_DURATION
|
|
|
|
})
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-09-17 03:20:52 -04:00
|
|
|
export {
|
2020-09-17 04:00:46 -04:00
|
|
|
buildLocalVideoFromReq,
|
|
|
|
buildVideoThumbnailsFromReq,
|
2021-01-21 10:57:21 -05:00
|
|
|
setVideoTags,
|
2022-08-08 09:48:17 -04:00
|
|
|
buildOptimizeOrMergeAudioJob,
|
2022-08-09 03:09:31 -04:00
|
|
|
buildTranscodingJob,
|
2022-08-08 09:48:17 -04:00
|
|
|
buildMoveToObjectStorageJob,
|
2022-06-17 08:34:37 -04:00
|
|
|
getTranscodingJobPriority,
|
|
|
|
getCachedVideoDuration
|
2020-09-17 03:20:52 -04:00
|
|
|
}
|