aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-import.ts
diff options
context:
space:
mode:
authorFlorent <florent.git@zeteo.me>2022-08-10 09:53:39 +0200
committerGitHub <noreply@github.com>2022-08-10 09:53:39 +0200
commit2a491182e483b97afb1b65c908b23cb48d591807 (patch)
treeec13503216ad72a3ea8f1ce3b659899f8167fb47 /server/lib/video-import.ts
parent06ac128958c489efe1008eeca1df683819bd2f18 (diff)
downloadPeerTube-2a491182e483b97afb1b65c908b23cb48d591807.tar.gz
PeerTube-2a491182e483b97afb1b65c908b23cb48d591807.tar.zst
PeerTube-2a491182e483b97afb1b65c908b23cb48d591807.zip
Channel sync (#5135)
* Add external channel URL for channel update / creation (#754) * Disallow synchronisation if user has no video quota (#754) * More constraints serverside (#754) * Disable sync if server configuration does not allow HTTP import (#754) * Working version synchronizing videos with a job (#754) TODO: refactoring, too much code duplication * More logs and try/catch (#754) * Fix eslint error (#754) * WIP: support synchronization time change (#754) * New frontend #754 * WIP: Create sync front (#754) * Enhance UI, sync creation form (#754) * Warning message when HTTP upload is disallowed * More consistent names (#754) * Binding Front with API (#754) * Add a /me API (#754) * Improve list UI (#754) * Implement creation and deletion routes (#754) * Lint (#754) * Lint again (#754) * WIP: UI for triggering import existing videos (#754) * Implement jobs for syncing and importing channels * Don't sync videos before sync creation + avoid concurrency issue (#754) * Cleanup (#754) * Cleanup: OpenAPI + API rework (#754) * Remove dead code (#754) * Eslint (#754) * Revert the mess with whitespaces in constants.ts (#754) * Some fixes after rebase (#754) * Several fixes after PR remarks (#754) * Front + API: Rename video-channels-sync to video-channel-syncs (#754) * Allow enabling channel sync through UI (#754) * getChannelInfo (#754) * Minor fixes: openapi + model + sql (#754) * Simplified API validators (#754) * Rename MChannelSync to MChannelSyncChannel (#754) * Add command for VideoChannelSync (#754) * Use synchronization.enabled config (#754) * Check parameters test + some fixes (#754) * Fix conflict mistake (#754) * Restrict access to video channel sync list API (#754) * Start adding unit test for synchronization (#754) * Continue testing (#754) * Tests finished + convertion of job to scheduler (#754) * Add lastSyncAt field (#754) * Fix externalRemoteUrl sort + creation date not well formatted (#754) * Small fix (#754) * Factorize addYoutubeDLImport and buildVideo (#754) * Check duplicates on channel not on users (#754) * factorize thumbnail generation (#754) * Fetch error should return status 400 (#754) * Separate video-channel-import and video-channel-sync-latest (#754) * Bump DB migration version after rebase (#754) * Prettier states in UI table (#754) * Add DefaultScope in VideoChannelSyncModel (#754) * Fix audit logs (#754) * Ensure user can upload when importing channel + minor fixes (#754) * Mark synchronization as failed on exception + typos (#754) * Change REST API for importing videos into channel (#754) * Add option for fully synchronize a chnanel (#754) * Return a whole sync object on creation to avoid tricks in Front (#754) * Various remarks (#754) * Single quotes by default (#754) * Rename synchronization to video_channel_synchronization * Add check.latest_videos_count and max_per_user options (#754) * Better channel rendering in list #754 * Allow sorting with channel name and state (#754) * Add missing tests for channel imports (#754) * Prefer using a parent job for channel sync * Styling * Client styling Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'server/lib/video-import.ts')
-rw-r--r--server/lib/video-import.ts308
1 files changed, 308 insertions, 0 deletions
diff --git a/server/lib/video-import.ts b/server/lib/video-import.ts
new file mode 100644
index 000000000..fb9306967
--- /dev/null
+++ b/server/lib/video-import.ts
@@ -0,0 +1,308 @@
1import { remove } from 'fs-extra'
2import { moveAndProcessCaptionFile } from '@server/helpers/captions-utils'
3import { isVTTFileValid } from '@server/helpers/custom-validators/video-captions'
4import { isVideoFileExtnameValid } from '@server/helpers/custom-validators/videos'
5import { isResolvingToUnicastOnly } from '@server/helpers/dns'
6import { logger } from '@server/helpers/logger'
7import { YoutubeDLInfo, YoutubeDLWrapper } from '@server/helpers/youtube-dl'
8import { CONFIG } from '@server/initializers/config'
9import { sequelizeTypescript } from '@server/initializers/database'
10import { Hooks } from '@server/lib/plugins/hooks'
11import { ServerConfigManager } from '@server/lib/server-config-manager'
12import { setVideoTags } from '@server/lib/video'
13import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
14import { VideoModel } from '@server/models/video/video'
15import { VideoCaptionModel } from '@server/models/video/video-caption'
16import { VideoImportModel } from '@server/models/video/video-import'
17import { FilteredModelAttributes } from '@server/types'
18import {
19 MChannelAccountDefault,
20 MChannelSync,
21 MThumbnail,
22 MUser,
23 MVideoAccountDefault,
24 MVideoCaption,
25 MVideoImportFormattable,
26 MVideoTag,
27 MVideoThumbnail,
28 MVideoWithBlacklistLight
29} from '@server/types/models'
30import { ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState, VideoPrivacy, VideoState } from '@shared/models'
31import { getLocalVideoActivityPubUrl } from './activitypub/url'
32import { updateVideoMiniatureFromExisting, updateVideoMiniatureFromUrl } from './thumbnail'
33
34class YoutubeDlImportError extends Error {
35 code: YoutubeDlImportError.CODE
36 cause?: Error // Property to remove once ES2022 is used
37 constructor ({ message, code }) {
38 super(message)
39 this.code = code
40 }
41
42 static fromError (err: Error, code: YoutubeDlImportError.CODE, message?: string) {
43 const ytDlErr = new this({ message: message ?? err.message, code })
44 ytDlErr.cause = err
45 ytDlErr.stack = err.stack // Useless once ES2022 is used
46 return ytDlErr
47 }
48}
49
50namespace YoutubeDlImportError {
51 export enum CODE {
52 FETCH_ERROR,
53 NOT_ONLY_UNICAST_URL
54 }
55}
56
57// ---------------------------------------------------------------------------
58
59async function insertFromImportIntoDB (parameters: {
60 video: MVideoThumbnail
61 thumbnailModel: MThumbnail
62 previewModel: MThumbnail
63 videoChannel: MChannelAccountDefault
64 tags: string[]
65 videoImportAttributes: FilteredModelAttributes<VideoImportModel>
66 user: MUser
67}): Promise<MVideoImportFormattable> {
68 const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters
69
70 const videoImport = await sequelizeTypescript.transaction(async t => {
71 const sequelizeOptions = { transaction: t }
72
73 // Save video object in database
74 const videoCreated = await video.save(sequelizeOptions) as (MVideoAccountDefault & MVideoWithBlacklistLight & MVideoTag)
75 videoCreated.VideoChannel = videoChannel
76
77 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
78 if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)
79
80 await autoBlacklistVideoIfNeeded({
81 video: videoCreated,
82 user,
83 notify: false,
84 isRemote: false,
85 isNew: true,
86 transaction: t
87 })
88
89 await setVideoTags({ video: videoCreated, tags, transaction: t })
90
91 // Create video import object in database
92 const videoImport = await VideoImportModel.create(
93 Object.assign({ videoId: videoCreated.id }, videoImportAttributes),
94 sequelizeOptions
95 ) as MVideoImportFormattable
96 videoImport.Video = videoCreated
97
98 return videoImport
99 })
100
101 return videoImport
102}
103
104async function buildVideoFromImport ({ channelId, importData, importDataOverride, importType }: {
105 channelId: number
106 importData: YoutubeDLInfo
107 importDataOverride?: Partial<VideoImportCreate>
108 importType: 'url' | 'torrent'
109}): Promise<MVideoThumbnail> {
110 let videoData = {
111 name: importDataOverride?.name || importData.name || 'Unknown name',
112 remote: false,
113 category: importDataOverride?.category || importData.category,
114 licence: importDataOverride?.licence ?? importData.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
115 language: importDataOverride?.language || importData.language,
116 commentsEnabled: importDataOverride?.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
117 downloadEnabled: importDataOverride?.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
118 waitTranscoding: importDataOverride?.waitTranscoding || false,
119 state: VideoState.TO_IMPORT,
120 nsfw: importDataOverride?.nsfw || importData.nsfw || false,
121 description: importDataOverride?.description || importData.description,
122 support: importDataOverride?.support || null,
123 privacy: importDataOverride?.privacy || VideoPrivacy.PRIVATE,
124 duration: 0, // duration will be set by the import job
125 channelId,
126 originallyPublishedAt: importDataOverride?.originallyPublishedAt
127 ? new Date(importDataOverride?.originallyPublishedAt)
128 : importData.originallyPublishedAt
129 }
130
131 videoData = await Hooks.wrapObject(
132 videoData,
133 importType === 'url'
134 ? 'filter:api.video.import-url.video-attribute.result'
135 : 'filter:api.video.import-torrent.video-attribute.result'
136 )
137
138 const video = new VideoModel(videoData)
139 video.url = getLocalVideoActivityPubUrl(video)
140
141 return video
142}
143
144async function buildYoutubeDLImport (options: {
145 targetUrl: string
146 channel: MChannelAccountDefault
147 user: MUser
148 channelSync?: MChannelSync
149 importDataOverride?: Partial<VideoImportCreate>
150 thumbnailFilePath?: string
151 previewFilePath?: string
152}) {
153 const { targetUrl, channel, channelSync, importDataOverride, thumbnailFilePath, previewFilePath, user } = options
154
155 const youtubeDL = new YoutubeDLWrapper(
156 targetUrl,
157 ServerConfigManager.Instance.getEnabledResolutions('vod'),
158 CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
159 )
160
161 // Get video infos
162 let youtubeDLInfo: YoutubeDLInfo
163 try {
164 youtubeDLInfo = await youtubeDL.getInfoForDownload()
165 } catch (err) {
166 throw YoutubeDlImportError.fromError(
167 err, YoutubeDlImportError.CODE.FETCH_ERROR, `Cannot fetch information from import for URL ${targetUrl}`
168 )
169 }
170
171 if (!await hasUnicastURLsOnly(youtubeDLInfo)) {
172 throw new YoutubeDlImportError({
173 message: 'Cannot use non unicast IP as targetUrl.',
174 code: YoutubeDlImportError.CODE.NOT_ONLY_UNICAST_URL
175 })
176 }
177
178 const video = await buildVideoFromImport({
179 channelId: channel.id,
180 importData: youtubeDLInfo,
181 importDataOverride,
182 importType: 'url'
183 })
184
185 const thumbnailModel = await forgeThumbnail({
186 inputPath: thumbnailFilePath,
187 downloadUrl: youtubeDLInfo.thumbnailUrl,
188 video,
189 type: ThumbnailType.MINIATURE
190 })
191
192 const previewModel = await forgeThumbnail({
193 inputPath: previewFilePath,
194 downloadUrl: youtubeDLInfo.thumbnailUrl,
195 video,
196 type: ThumbnailType.PREVIEW
197 })
198
199 const videoImport = await insertFromImportIntoDB({
200 video,
201 thumbnailModel,
202 previewModel,
203 videoChannel: channel,
204 tags: importDataOverride?.tags || youtubeDLInfo.tags,
205 user,
206 videoImportAttributes: {
207 targetUrl,
208 state: VideoImportState.PENDING,
209 userId: user.id
210 }
211 })
212
213 // Get video subtitles
214 await processYoutubeSubtitles(youtubeDL, targetUrl, video.id)
215
216 let fileExt = `.${youtubeDLInfo.ext}`
217 if (!isVideoFileExtnameValid(fileExt)) fileExt = '.mp4'
218
219 const payload: VideoImportPayload = {
220 type: 'youtube-dl' as 'youtube-dl',
221 videoImportId: videoImport.id,
222 fileExt,
223 // If part of a sync process, there is a parent job that will aggregate children results
224 preventException: !!channelSync
225 }
226
227 return {
228 videoImport,
229 job: { type: 'video-import' as 'video-import', payload }
230 }
231}
232
233// ---------------------------------------------------------------------------
234
235export {
236 buildYoutubeDLImport,
237 YoutubeDlImportError,
238 insertFromImportIntoDB,
239 buildVideoFromImport
240}
241
242// ---------------------------------------------------------------------------
243
244async function forgeThumbnail ({ inputPath, video, downloadUrl, type }: {
245 inputPath?: string
246 downloadUrl?: string
247 video: MVideoThumbnail
248 type: ThumbnailType
249}): Promise<MThumbnail> {
250 if (inputPath) {
251 return updateVideoMiniatureFromExisting({
252 inputPath,
253 video,
254 type,
255 automaticallyGenerated: false
256 })
257 } else if (downloadUrl) {
258 try {
259 return await updateVideoMiniatureFromUrl({ downloadUrl, video, type })
260 } catch (err) {
261 logger.warn('Cannot process thumbnail %s from youtubedl.', downloadUrl, { err })
262 }
263 }
264 return null
265}
266
267async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl: string, videoId: number) {
268 try {
269 const subtitles = await youtubeDL.getSubtitles()
270
271 logger.info('Will create %s subtitles from youtube import %s.', subtitles.length, targetUrl)
272
273 for (const subtitle of subtitles) {
274 if (!await isVTTFileValid(subtitle.path)) {
275 await remove(subtitle.path)
276 continue
277 }
278
279 const videoCaption = new VideoCaptionModel({
280 videoId,
281 language: subtitle.language,
282 filename: VideoCaptionModel.generateCaptionName(subtitle.language)
283 }) as MVideoCaption
284
285 // Move physical file
286 await moveAndProcessCaptionFile(subtitle, videoCaption)
287
288 await sequelizeTypescript.transaction(async t => {
289 await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
290 })
291 }
292 } catch (err) {
293 logger.warn('Cannot get video subtitles.', { err })
294 }
295}
296
297async function hasUnicastURLsOnly (youtubeDLInfo: YoutubeDLInfo) {
298 const hosts = youtubeDLInfo.urls.map(u => new URL(u).hostname)
299 const uniqHosts = new Set(hosts)
300
301 for (const h of uniqHosts) {
302 if (await isResolvingToUnicastOnly(h) !== true) {
303 return false
304 }
305 }
306
307 return true
308}