]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
Improve VideoChannelSyncLatestScheduler logs
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
1 import { UploadFiles } from 'express'
2 import memoizee from 'memoizee'
3 import { Transaction } from 'sequelize/types'
4 import { CONFIG } from '@server/initializers/config'
5 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY, MEMOIZE_LENGTH, MEMOIZE_TTL } from '@server/initializers/constants'
6 import { TagModel } from '@server/models/video/tag'
7 import { VideoModel } from '@server/models/video/video'
8 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
9 import { FilteredModelAttributes } from '@server/types'
10 import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
11 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoState, VideoTranscodingPayload } from '@shared/models'
12 import { CreateJobOptions } from './job-queue/job-queue'
13 import { updateVideoMiniatureFromExisting } from './thumbnail'
14
15 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
16 return {
17 name: videoInfo.name,
18 remote: false,
19 category: videoInfo.category,
20 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
21 language: videoInfo.language,
22 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
23 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
24 waitTranscoding: videoInfo.waitTranscoding || false,
25 nsfw: videoInfo.nsfw || false,
26 description: videoInfo.description,
27 support: videoInfo.support,
28 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
29 channelId,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
31 ? new Date(videoInfo.originallyPublishedAt)
32 : null
33 }
34 }
35
36 async function buildVideoThumbnailsFromReq (options: {
37 video: MVideoThumbnail
38 files: UploadFiles
39 fallback: (type: ThumbnailType) => Promise<MThumbnail>
40 automaticallyGenerated?: boolean
41 }) {
42 const { video, files, fallback, automaticallyGenerated } = options
43
44 const promises = [
45 {
46 type: ThumbnailType.MINIATURE,
47 fieldName: 'thumbnailfile'
48 },
49 {
50 type: ThumbnailType.PREVIEW,
51 fieldName: 'previewfile'
52 }
53 ].map(p => {
54 const fields = files?.[p.fieldName]
55
56 if (fields) {
57 return updateVideoMiniatureFromExisting({
58 inputPath: fields[0].path,
59 video,
60 type: p.type,
61 automaticallyGenerated: automaticallyGenerated || false
62 })
63 }
64
65 return fallback(p.type)
66 })
67
68 return Promise.all(promises)
69 }
70
71 // ---------------------------------------------------------------------------
72
73 async function setVideoTags (options: {
74 video: MVideoTag
75 tags: string[]
76 transaction?: Transaction
77 }) {
78 const { video, tags, transaction } = options
79
80 const internalTags = tags || []
81 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
82
83 await video.$set('Tags', tagInstances, { transaction })
84 video.Tags = tagInstances
85 }
86
87 // ---------------------------------------------------------------------------
88
89 async function buildOptimizeOrMergeAudioJob (options: {
90 video: MVideoUUID
91 videoFile: MVideoFile
92 user: MUserId
93 isNewVideo?: boolean // Default true
94 }) {
95 const { video, videoFile, user, isNewVideo } = options
96
97 let payload: VideoTranscodingPayload
98
99 if (videoFile.isAudio()) {
100 payload = {
101 type: 'merge-audio-to-webtorrent',
102 resolution: DEFAULT_AUDIO_RESOLUTION,
103 videoUUID: video.uuid,
104 createHLSIfNeeded: true,
105 isNewVideo
106 }
107 } else {
108 payload = {
109 type: 'optimize-to-webtorrent',
110 videoUUID: video.uuid,
111 isNewVideo
112 }
113 }
114
115 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
116
117 return {
118 type: 'video-transcoding' as 'video-transcoding',
119 priority: await getTranscodingJobPriority(user),
120 payload
121 }
122 }
123
124 async function buildTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
125 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
126
127 return { type: 'video-transcoding' as 'video-transcoding', payload, ...options }
128 }
129
130 async function getTranscodingJobPriority (user: MUserId) {
131 const now = new Date()
132 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
133
134 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
135
136 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
137 }
138
139 // ---------------------------------------------------------------------------
140
141 async function buildMoveToObjectStorageJob (options: {
142 video: MVideoUUID
143 previousVideoState: VideoState
144 isNewVideo?: boolean // Default true
145 }) {
146 const { video, previousVideoState, isNewVideo = true } = options
147
148 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
149
150 return {
151 type: 'move-to-object-storage' as 'move-to-object-storage',
152 payload: {
153 videoUUID: video.uuid,
154 isNewVideo,
155 previousVideoState
156 }
157 }
158 }
159
160 // ---------------------------------------------------------------------------
161
162 async function getVideoDuration (videoId: number | string) {
163 const video = await VideoModel.load(videoId)
164
165 const duration = video.isLive
166 ? undefined
167 : video.duration
168
169 return { duration, isLive: video.isLive }
170 }
171
172 const getCachedVideoDuration = memoizee(getVideoDuration, {
173 promise: true,
174 max: MEMOIZE_LENGTH.VIDEO_DURATION,
175 maxAge: MEMOIZE_TTL.VIDEO_DURATION
176 })
177
178 // ---------------------------------------------------------------------------
179
180 export {
181 buildLocalVideoFromReq,
182 buildVideoThumbnailsFromReq,
183 setVideoTags,
184 buildOptimizeOrMergeAudioJob,
185 buildTranscodingJob,
186 buildMoveToObjectStorageJob,
187 getTranscodingJobPriority,
188 getCachedVideoDuration
189 }