]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
21e4b7ff21402e9a3e6a87e2e52001b3a49ed525
[github/Chocobozzz/PeerTube.git] / server / lib / video.ts
1 import { UploadFiles } from 'express'
2 import { Transaction } from 'sequelize/types'
3 import { DEFAULT_AUDIO_RESOLUTION, JOB_PRIORITY } from '@server/initializers/constants'
4 import { sequelizeTypescript } from '@server/initializers/database'
5 import { TagModel } from '@server/models/video/tag'
6 import { VideoModel } from '@server/models/video/video'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MThumbnail, MUserId, MVideo, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
9 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
10 import { federateVideoIfNeeded } from './activitypub/videos'
11 import { JobQueue } from './job-queue/job-queue'
12 import { Notifier } from './notifier'
13 import { createVideoMiniatureFromExisting } 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,
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,
25 nsfw: videoInfo.nsfw || false,
26 description: videoInfo.description,
27 support: videoInfo.support,
28 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
29 channelId: channelId,
30 originallyPublishedAt: videoInfo.originallyPublishedAt
31 }
32 }
33
34 async function buildVideoThumbnailsFromReq (options: {
35 video: MVideoThumbnail
36 files: UploadFiles
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
69 async function setVideoTags (options: {
70 video: MVideoTag
71 tags: string[]
72 transaction?: Transaction
73 }) {
74 const { video, tags, transaction } = options
75
76 const internalTags = tags || []
77 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
78
79 await video.$set('Tags', tagInstances, { transaction })
80 video.Tags = tagInstances
81 }
82
83 async function publishAndFederateIfNeeded (video: MVideoUUID, wasLive = false) {
84 const result = await sequelizeTypescript.transaction(async t => {
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
94 // Live videos are always federated, so it's not a new video
95 await federateVideoIfNeeded(videoDatabase, !wasLive && videoPublished, t)
96
97 return { videoDatabase, videoPublished }
98 })
99
100 if (result?.videoPublished) {
101 Notifier.Instance.notifyOnNewVideoIfNeeded(result.videoDatabase)
102 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(result.videoDatabase)
103 }
104 }
105
106 async 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 = {
125 priority: await getTranscodingJobPriority(user)
126 }
127
128 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }, jobOptions)
129 }
130
131 async function getTranscodingJobPriority (user: MUserId) {
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
137 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
138 }
139
140 // ---------------------------------------------------------------------------
141
142 export {
143 buildLocalVideoFromReq,
144 publishAndFederateIfNeeded,
145 buildVideoThumbnailsFromReq,
146 setVideoTags,
147 addOptimizeOrMergeAudioJob,
148 getTranscodingJobPriority
149 }