]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video.ts
Prevent HLS transcoding after webtorrent transcoding
[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 { TagModel } from '@server/models/video/tag'
5 import { VideoModel } from '@server/models/video/video'
6 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
7 import { FilteredModelAttributes } from '@server/types'
8 import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID } from '@server/types/models'
9 import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models'
10 import { CreateJobOptions, JobQueue } from './job-queue/job-queue'
11 import { updateVideoMiniatureFromExisting } from './thumbnail'
12 import { CONFIG } from '@server/initializers/config'
13
14 function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes<VideoModel> {
15 return {
16 name: videoInfo.name,
17 remote: false,
18 category: videoInfo.category,
19 licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
20 language: videoInfo.language,
21 commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
22 downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
23 waitTranscoding: videoInfo.waitTranscoding || false,
24 nsfw: videoInfo.nsfw || false,
25 description: videoInfo.description,
26 support: videoInfo.support,
27 privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
28 channelId: channelId,
29 originallyPublishedAt: videoInfo.originallyPublishedAt
30 ? new Date(videoInfo.originallyPublishedAt)
31 : null
32 }
33 }
34
35 async function buildVideoThumbnailsFromReq (options: {
36 video: MVideoThumbnail
37 files: UploadFiles
38 fallback: (type: ThumbnailType) => Promise<MThumbnail>
39 automaticallyGenerated?: boolean
40 }) {
41 const { video, files, fallback, automaticallyGenerated } = options
42
43 const promises = [
44 {
45 type: ThumbnailType.MINIATURE,
46 fieldName: 'thumbnailfile'
47 },
48 {
49 type: ThumbnailType.PREVIEW,
50 fieldName: 'previewfile'
51 }
52 ].map(p => {
53 const fields = files?.[p.fieldName]
54
55 if (fields) {
56 return updateVideoMiniatureFromExisting({
57 inputPath: fields[0].path,
58 video,
59 type: p.type,
60 automaticallyGenerated: automaticallyGenerated || false
61 })
62 }
63
64 return fallback(p.type)
65 })
66
67 return Promise.all(promises)
68 }
69
70 async function setVideoTags (options: {
71 video: MVideoTag
72 tags: string[]
73 transaction?: Transaction
74 }) {
75 const { video, tags, transaction } = options
76
77 const internalTags = tags || []
78 const tagInstances = await TagModel.findOrCreateTags(internalTags, transaction)
79
80 await video.$set('Tags', tagInstances, { transaction })
81 video.Tags = tagInstances
82 }
83
84 async function addOptimizeOrMergeAudioJob (video: MVideoUUID, videoFile: MVideoFile, user: MUserId) {
85 let dataInput: VideoTranscodingPayload
86
87 if (videoFile.isAudio()) {
88 dataInput = {
89 type: 'merge-audio-to-webtorrent',
90 resolution: DEFAULT_AUDIO_RESOLUTION,
91 videoUUID: video.uuid,
92 createHLSIfNeeded: true,
93 isNewVideo: true
94 }
95 } else {
96 dataInput = {
97 type: 'optimize-to-webtorrent',
98 videoUUID: video.uuid,
99 isNewVideo: true
100 }
101 }
102
103 const jobOptions = {
104 priority: await getTranscodingJobPriority(user)
105 }
106
107 return addTranscodingJob(dataInput, jobOptions)
108 }
109
110 async function addTranscodingJob (payload: VideoTranscodingPayload, options: CreateJobOptions = {}) {
111 await VideoJobInfoModel.increaseOrCreate(payload.videoUUID, 'pendingTranscode')
112
113 return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: payload }, options)
114 }
115
116 async function addMoveToObjectStorageJob (video: MVideoUUID, isNewVideo = true) {
117 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingMove')
118
119 const dataInput = { videoUUID: video.uuid, isNewVideo }
120 return JobQueue.Instance.createJobWithPromise({ type: 'move-to-object-storage', payload: dataInput })
121 }
122
123 async function getTranscodingJobPriority (user: MUserId) {
124 const now = new Date()
125 const lastWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)
126
127 const videoUploadedByUser = await VideoModel.countVideosUploadedByUserSince(user.id, lastWeek)
128
129 return JOB_PRIORITY.TRANSCODING + videoUploadedByUser
130 }
131
132 // ---------------------------------------------------------------------------
133
134 export {
135 buildLocalVideoFromReq,
136 buildVideoThumbnailsFromReq,
137 setVideoTags,
138 addOptimizeOrMergeAudioJob,
139 addTranscodingJob,
140 addMoveToObjectStorageJob,
141 getTranscodingJobPriority
142 }