+async function buildVideoThumbnailsFromReq (options: {
+ video: MVideoThumbnail
+ files: UploadFiles
+ 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) {
+ return updateVideoMiniatureFromExisting({
+ inputPath: fields[0].path,
+ video,
+ type: p.type,
+ automaticallyGenerated: automaticallyGenerated || false
+ })
+ }
+
+ return fallback(p.type)
+ })
+
+ return Promise.all(promises)
+}
+
+// ---------------------------------------------------------------------------
+
+async function setVideoTags (options: {
+ video: MVideoTag
+ tags: string[]
+ transaction?: Transaction
+}) {
+ const { video, tags, transaction } = options
+
+ const internalTags = tags || []
+ const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
+
+ await video.$set('Tags', tagInstances, { transaction })
+ video.Tags = tagInstances
+}
+
+// ---------------------------------------------------------------------------
+
+async function addOptimizeOrMergeAudioJob (options: {
+ video: MVideoUUID
+ videoFile: MVideoFile
+ user: MUserId
+ isNewVideo?: boolean // Default true
+}) {
+ const { video, videoFile, user, isNewVideo } = options
+
+ let dataInput: VideoTranscodingPayload
+
+ if (videoFile.isAudio()) {
+ dataInput = {
+ type: 'merge-audio-to-webtorrent',
+ resolution: DEFAULT_AUDIO_RESOLUTION,
+ videoUUID: video.uuid,
+ createHLSIfNeeded: true,
+ isNewVideo
+ }
+ } else {
+ dataInput = {
+ type: 'optimize-to-webtorrent',
+ videoUUID: video.uuid,
+ isNewVideo
+ }
+ }
+
+ const jobOptions = {
+ priority: await getTranscodingJobPriority(user)
+ }
+
+ return addTranscodingJob(dataInput, jobOptions)
+}
+
+async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
+ await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
+
+ return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload }, options)
+}
+
+async function getTranscodingJobPriority (user: MUserId) {
+ const now = new Date()
+ const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
+
+ const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
+
+ return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
+}
+
+// ---------------------------------------------------------------------------
+
+async function addMoveToObjectStorageJob (options: {
+ video: MVideoUUID
+ previousVideoState: VideoState
+ isNewVideo?: boolean // Default true
+}) {
+ const { video, previousVideoState, isNewVideo = true } = options
+
+ await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
+
+ const dataInput = { videoUUID: video.uuid, isNewVideo, previousVideoState }
+ return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
+}
+
+// ---------------------------------------------------------------------------
+
+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
+})
+