]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
f6d6e7f8 1import { UploadFiles } from 'express'
1ef65f4c 2import { Transaction } from 'sequelize/types'
77d7e851 3import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
d846d99c 4import { sequelizeTypescript } from '@server/initializers/database'
1ef65f4c 5import { TagModel } from '@server/models/video/tag'
c6c0fa6c
C
6import { VideoModel } from '@server/models/video/video'
7import { FilteredModelAttributes } from '@server/types'
6c9c3b7b 8import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
77d7e851 9import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
d846d99c 10import { federateVideoIfNeeded } from './activitypub/videos'
77d7e851 11import { JobQueue } from './job-queue/job-queue'
d846d99c 12import { Notifier } from './notifier'
1ef65f4c 13import { createVideoMiniatureFromExisting } from './thumbnail'
c6c0fa6c 14
1ef65f4c 15function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
c6c0fa6c
C
16 return {
17 name: videoInfo.name,
18 remote: false,
19 category: videoInfo.category,
20 licence: videoInfo.licence,
21 language: videoInfo.language,
22 commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true"
23 downloadEnabled: videoInfo.downloadEnabled !== false,
24 waitTranscoding: videoInfo.waitTranscoding || false,
c6c0fa6c
C
25 nsfw: videoInfo.nsfw || false,
26 description: videoInfo.description,
27 support: videoInfo.support,
28 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
c6c0fa6c
C
29 channelId: channelId,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
31 }
32}
33
1ef65f4c
C
34async function buildVideoThumbnailsFromReq (options: {
35 video: MVideoThumbnail
f6d6e7f8 36 files: UploadFiles
1ef65f4c
C
37 fallback: (type: ThumbnailType) => Promise<MThumbnail>
38 automaticallyGenerated?: boolean
39}) {
40 const { video, files, fallback, automaticallyGenerated } = options
41
42 const promises = [
43 {
44 type: ThumbnailType.MINIATURE,
45 fieldName: 'thumbnailfile'
46 },
47 {
48 type: ThumbnailType.PREVIEW,
49 fieldName: 'previewfile'
50 }
51 ].map(p => {
52 const fields = files?.[p.fieldName]
53
54 if (fields) {
55 return createVideoMiniatureFromExisting({
56 inputPath: fields[0].path,
57 video,
58 type: p.type,
59 automaticallyGenerated: automaticallyGenerated || false
60 })
61 }
62
63 return fallback(p.type)
64 })
65
66 return Promise.all(promises)
67}
68
69async function setVideoTags (options: {
70 video: MVideoTag
71 tags: string[]
72 transaction?: Transaction
1ef65f4c 73}) {
6c9c3b7b 74 const { video, tags, transaction } = options
1ef65f4c 75
6c9c3b7b
C
76 const internalTags = tags || []
77 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
78
79 await video.$set('Tags', tagInstances, { transaction })
80 video.Tags = tagInstances
1ef65f4c
C
81}
82
97969c4e
C
83async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
84 const result = await sequelizeTypescript.transaction(async t => {
d846d99c
C
85 // Maybe the video changed in database, refresh it
86 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
87 // Video does not exist anymore
88 if (!videoDatabase) return undefined
89
90 // We transcoded the video file in another format, now we can publish it
91 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
92
93 // If the video was not published, we consider it is a new one for other instances
97969c4e
C
94 // Live videos are always federated, so it's not a new video
95 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
d846d99c
C
96
97 return { videoDatabase, videoPublished }
98 })
99
97969c4e
C
100 if (result?.videoPublished) {
101 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
102 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
d846d99c
C
103 }
104}
105
77d7e851
C
106async function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile, user: MUserId) {
107 let dataInput: VideoTranscodingPayload
108
109 if (videoFile.isAudio()) {
110 dataInput = {
111 type: 'merge-audio-to-webtorrent',
112 resolution: DEFAULT_AUDIO_RESOLUTION,
113 videoUUID: video.uuid,
114 isNewVideo: true
115 }
116 } else {
117 dataInput = {
118 type: 'optimize-to-webtorrent',
119 videoUUID: video.uuid,
120 isNewVideo: true
121 }
122 }
123
124 const jobOptions = {
a6e37eeb 125 priority: await getTranscodingJobPriority(user)
77d7e851
C
126 }
127
128 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
129}
130
a6e37eeb 131async function getTranscodingJobPriority (user: MUserId) {
77d7e851
C
132 const now = new Date()
133 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
134
135 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
136
a6e37eeb 137 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
77d7e851
C
138}
139
c6c0fa6c
C
140// ---------------------------------------------------------------------------
141
142export {
1ef65f4c 143 buildLocalVideoFromReq,
d846d99c 144 publishAndFederateIfNeeded,
1ef65f4c 145 buildVideoThumbnailsFromReq,
77d7e851
C
146 setVideoTags,
147 addOptimizeOrMergeAudioJob,
a6e37eeb 148 getTranscodingJobPriority
c6c0fa6c 149}