1 import { Transaction } from 'sequelize/types'
2 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
3 import { sequelizeTypescript } from '@server/initializers/database'
4 import { TagModel } from '@server/models/video/tag'
5 import { VideoModel } from '@server/models/video/video'
6 import { FilteredModelAttributes } from '@server/types'
7 import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
8 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
9 import { federateVideoIfNeeded } from './activitypub/videos'
10 import { JobQueue } from './job-queue/job-queue'
11 import { Notifier } from './notifier'
12 import { createVideoMiniatureFromExisting } from './thumbnail'
14 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
18 category: videoInfo.category,
19 licence: videoInfo.licence,
20 language: videoInfo.language,
21 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
22 downloadEnabled: videoInfo.downloadEnabled !== false,
23 waitTranscoding: videoInfo.waitTranscoding || false,
24 nsfw: videoInfo.nsfw || false,
25 description: videoInfo.description,
26 support: videoInfo.support,
27 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
29 originallyPublishedAt: videoInfo.originallyPublishedAt
33 async function buildVideoThumbnailsFromReq (options: {
34 video: MVideoThumbnail
35 files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[]
36 fallback: (type: ThumbnailType) => Promise<MThumbnail>
37 automaticallyGenerated?: boolean
39 const { video, files, fallback, automaticallyGenerated } = options
43 type: ThumbnailType.MINIATURE,
44 fieldName: 'thumbnailfile'
47 type: ThumbnailType.PREVIEW,
48 fieldName: 'previewfile'
51 const fields = files?.[p.fieldName]
54 return createVideoMiniatureFromExisting({
55 inputPath: fields[0].path,
58 automaticallyGenerated: automaticallyGenerated || false
62 return fallback(p.type)
65 return Promise.all(promises)
68 async function setVideoTags (options: {
71 transaction?: Transaction
73 const { video, tags, transaction } = options
75 const internalTags = tags || []
76 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
78 await video.$set('Tags', tagInstances, { transaction })
79 video.Tags = tagInstances
82 async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
83 const result = await sequelizeTypescript.transaction(async t => {
84 // Maybe the video changed in database, refresh it
85 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
86 // Video does not exist anymore
87 if (!videoDatabase) return undefined
89 // We transcoded the video file in another format, now we can publish it
90 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
92 // If the video was not published, we consider it is a new one for other instances
93 // Live videos are always federated, so it's not a new video
94 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
96 return { videoDatabase, videoPublished }
99 if (result?.videoPublished) {
100 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
101 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
105 async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) {
106 let dataInput: VideoTranscodingPayload
108 if (videoFile.isAudio()) {
110 type: 'merge-audio-to-webtorrent',
111 resolution: DEFAULT_AUDIO_RESOLUTION,
112 videoUUID: video.uuid,
117 type: 'optimize-to-webtorrent',
118 videoUUID: video.uuid,
124 priority: JOB_PRIORITY.TRANSCODING.OPTIMIZER + await getJobTranscodingPriorityMalus(user)
127 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
130 async function getJobTranscodingPriorityMalus (user: MUserId) {
131 const now = new Date()
132 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
134 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
136 return videoUploadedByUser
139 // ---------------------------------------------------------------------------
142 buildLocalVideoFromReq,
143 publishAndFederateIfNeeded,
144 buildVideoThumbnailsFromReq,
146 addOptimizeOrMergeAudioJob,
147 getJobTranscodingPriorityMalus