]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video.ts
Merge branch 'feature/improve-live' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
CommitLineData
f6d6e7f8 1import { UploadFiles } from 'express'
bd911b54 2import memoizee from 'memoizee'
1ef65f4c 3import { Transaction } from 'sequelize/types'
bd911b54 4import { CONFIG } from '@server/initializers/config'
aa2ce188 5import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants'
1ef65f4c 6import { TagModel } from '@server/models/video/tag'
c6c0fa6c 7import { VideoModel } from '@server/models/video/video'
0305db28 8import { VideoJobInfoModel } from '@server/models/video/video-job-info'
c6c0fa6c 9import { FilteredModelAttributes } from '@server/types'
3545e72c
C
10import { MThumbnail, MUserId, MVideoFile, MVideoFullLight, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
11import { ManageVideoTorrentPayload, ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
12import { CreateJobArgument, CreateJobOptions, JobQueue } from './job-queue/job-queue'
91f8f8db 13import { updateVideoMiniatureFromExisting } from './thumbnail'
3545e72c 14import { moveFilesIfPrivacyChanged } from './video-privacy'
c6c0fa6c 15
1ef65f4c 16function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
c6c0fa6c
C
17 return {
18 name: videoInfo.name,
19 remote: false,
20 category: videoInfo.category,
3cf68b86 21 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
c6c0fa6c 22 language: videoInfo.language,
3cf68b86
C
23 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
24 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
c6c0fa6c 25 waitTranscoding: videoInfo.waitTranscoding || false,
c6c0fa6c
C
26 nsfw: videoInfo.nsfw || false,
27 description: videoInfo.description,
28 support: videoInfo.support,
29 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
ba2684ce 30 channelId,
c6c0fa6c 31 originallyPublishedAt: videoInfo.originallyPublishedAt
16c016e8
C
32 ? new Date(videoInfo.originallyPublishedAt)
33 : null
c6c0fa6c
C
34 }
35}
36
1ef65f4c
C
37async function buildVideoThumbnailsFromReq (options: {
38 video: MVideoThumbnail
f6d6e7f8 39 files: UploadFiles
1ef65f4c
C
40 fallback: (type: ThumbnailType) => Promise<MThumbnail>
41 automaticallyGenerated?: boolean
42}) {
43 const { video, files, fallback, automaticallyGenerated } = options
44
45 const promises = [
46 {
47 type: ThumbnailType.MINIATURE,
48 fieldName: 'thumbnailfile'
49 },
50 {
51 type: ThumbnailType.PREVIEW,
52 fieldName: 'previewfile'
53 }
54 ].map(p => {
55 const fields = files?.[p.fieldName]
56
57 if (fields) {
91f8f8db 58 return updateVideoMiniatureFromExisting({
1ef65f4c
C
59 inputPath: fields[0].path,
60 video,
61 type: p.type,
62 automaticallyGenerated: automaticallyGenerated || false
63 })
64 }
65
66 return fallback(p.type)
67 })
68
69 return Promise.all(promises)
70}
71
1808a1f8
C
72// ---------------------------------------------------------------------------
73
1ef65f4c
C
74async function setVideoTags (options: {
75 video: MVideoTag
76 tags: string[]
77 transaction?: Transaction
1ef65f4c 78}) {
6c9c3b7b 79 const { video, tags, transaction } = options
1ef65f4c 80
6c9c3b7b
C
81 const internalTags = tags || []
82 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
83
84 await video.$set('Tags', tagInstances, { transaction })
85 video.Tags = tagInstances
1ef65f4c
C
86}
87
1808a1f8
C
88// ---------------------------------------------------------------------------
89
bd911b54 90async function buildOptimizeOrMergeAudioJob (options: {
1808a1f8
C
91 video: MVideoUUID
92 videoFile: MVideoFile
93 user: MUserId
94 isNewVideo?: boolean // Default true
95}) {
96 const { video, videoFile, user, isNewVideo } = options
97
bd911b54 98 let payload: VideoTranscodingPayload
77d7e851
C
99
100 if (videoFile.isAudio()) {
bd911b54 101 payload = {
77d7e851
C
102 type: 'merge-audio-to-webtorrent',
103 resolution: DEFAULT_AUDIO_RESOLUTION,
104 videoUUID: video.uuid,
0f11ec8d 105 createHLSIfNeeded: true,
c729caf6 106 isNewVideo
77d7e851
C
107 }
108 } else {
bd911b54 109 payload = {
77d7e851
C
110 type: 'optimize-to-webtorrent',
111 videoUUID: video.uuid,
c729caf6 112 isNewVideo
77d7e851
C
113 }
114 }
115
bd911b54 116 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
77d7e851 117
bd911b54
C
118 return {
119 type: 'video-transcoding' as 'video-transcoding',
120 priority: await getTranscodingJobPriority(user),
121 payload
122 }
0305db28
JB
123}
124
b42c2c7e 125async function buildTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
0305db28
JB
126 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
127
b42c2c7e 128 return { type: 'video-transcoding' as 'video-transcoding', payload, ...options }
0305db28
JB
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
bd911b54 142async function buildMoveToObjectStorageJob (options: {
1808a1f8
C
143 video: MVideoUUID
144 previousVideoState: VideoState
145 isNewVideo?: boolean // Default true
146}) {
147 const { video, previousVideoState, isNewVideo = true } = options
148
149 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
150
bd911b54
C
151 return {
152 type: 'move-to-object-storage' as 'move-to-object-storage',
153 payload: {
154 videoUUID: video.uuid,
155 isNewVideo,
156 previousVideoState
157 }
158 }
1808a1f8
C
159}
160
161// ---------------------------------------------------------------------------
162
aa2ce188
C
163async function getVideoDuration (videoId: number | string) {
164 const video = await VideoModel.load(videoId)
165
166 const duration = video.isLive
167 ? undefined
168 : video.duration
169
170 return { duration, isLive: video.isLive }
171}
172
173const getCachedVideoDuration = memoizee(getVideoDuration, {
174 promise: true,
175 max: MEMOIZE_LENGTH.VIDEO_DURATION,
176 maxAge: MEMOIZE_TTL.VIDEO_DURATION
177})
178
179// ---------------------------------------------------------------------------
180
3545e72c
C
181async function addVideoJobsAfterUpdate (options: {
182 video: MVideoFullLight
183 isNewVideo: boolean
184
185 nameChanged: boolean
186 oldPrivacy: VideoPrivacy
187}) {
188 const { video, nameChanged, oldPrivacy, isNewVideo } = options
189 const jobs: CreateJobArgument[] = []
190
191 const filePathChanged = await moveFilesIfPrivacyChanged(video, oldPrivacy)
192
193 if (!video.isLive && (nameChanged || filePathChanged)) {
194 for (const file of (video.VideoFiles || [])) {
195 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
196
197 jobs.push({ type: 'manage-video-torrent', payload })
198 }
199
200 const hls = video.getHLSPlaylist()
201
202 for (const file of (hls?.VideoFiles || [])) {
203 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
204
205 jobs.push({ type: 'manage-video-torrent', payload })
206 }
207 }
208
209 jobs.push({
210 type: 'federate-video',
211 payload: {
212 videoUUID: video.uuid,
213 isNewVideo
214 }
215 })
216
217 const wasConfidentialVideo = new Set([ VideoPrivacy.PRIVATE, VideoPrivacy.UNLISTED, VideoPrivacy.INTERNAL ]).has(oldPrivacy)
218
219 if (wasConfidentialVideo) {
220 jobs.push({
221 type: 'notify',
222 payload: {
223 action: 'new-video',
224 videoUUID: video.uuid
225 }
226 })
227 }
228
229 return JobQueue.Instance.createSequentialJobFlow(...jobs)
230}
231
232// ---------------------------------------------------------------------------
233
c6c0fa6c 234export {
1ef65f4c
C
235 buildLocalVideoFromReq,
236 buildVideoThumbnailsFromReq,
77d7e851 237 setVideoTags,
bd911b54 238 buildOptimizeOrMergeAudioJob,
b42c2c7e 239 buildTranscodingJob,
bd911b54 240 buildMoveToObjectStorageJob,
aa2ce188 241 getTranscodingJobPriority,
3545e72c 242 addVideoJobsAfterUpdate,
aa2ce188 243 getCachedVideoDuration
c6c0fa6c 244}