aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-pre-import.ts
blob: df67dc953f52ba9e46bb338af8f3629fbfd0fe8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { remove } from 'fs-extra'
import { moveAndProcessCaptionFile } from '@server/helpers/captions-utils'
import { isVTTFileValid } from '@server/helpers/custom-validators/video-captions'
import { isVideoFileExtnameValid } from '@server/helpers/custom-validators/videos'
import { isResolvingToUnicastOnly } from '@server/helpers/dns'
import { logger } from '@server/helpers/logger'
import { YoutubeDLInfo, YoutubeDLWrapper } from '@server/helpers/youtube-dl'
import { CONFIG } from '@server/initializers/config'
import { sequelizeTypescript } from '@server/initializers/database'
import { Hooks } from '@server/lib/plugins/hooks'
import { ServerConfigManager } from '@server/lib/server-config-manager'
import { setVideoTags } from '@server/lib/video'
import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
import { VideoModel } from '@server/models/video/video'
import { VideoCaptionModel } from '@server/models/video/video-caption'
import { VideoImportModel } from '@server/models/video/video-import'
import { FilteredModelAttributes } from '@server/types'
import {
  MChannelAccountDefault,
  MChannelSync,
  MThumbnail,
  MUser,
  MVideoAccountDefault,
  MVideoCaption,
  MVideoImportFormattable,
  MVideoTag,
  MVideoThumbnail,
  MVideoWithBlacklistLight
} from '@server/types/models'
import { ThumbnailType, VideoImportCreate, VideoImportPayload, VideoImportState, VideoPrivacy, VideoState } from '@shared/models'
import { getLocalVideoActivityPubUrl } from './activitypub/url'
import { updateVideoMiniatureFromExisting, updateVideoMiniatureFromUrl } from './thumbnail'

class YoutubeDlImportError extends Error {
  code: YoutubeDlImportError.CODE
  cause?: Error // Property to remove once ES2022 is used
  constructor ({ message, code }) {
    super(message)
    this.code = code
  }

  static fromError (err: Error, code: YoutubeDlImportError.CODE, message?: string) {
    const ytDlErr = new this({ message: message ?? err.message, code })
    ytDlErr.cause = err
    ytDlErr.stack = err.stack // Useless once ES2022 is used
    return ytDlErr
  }
}

namespace YoutubeDlImportError {
  export enum CODE {
    FETCH_ERROR,
    NOT_ONLY_UNICAST_URL
  }
}

// ---------------------------------------------------------------------------

async function insertFromImportIntoDB (parameters: {
  video: MVideoThumbnail
  thumbnailModel: MThumbnail
  previewModel: MThumbnail
  videoChannel: MChannelAccountDefault
  tags: string[]
  videoImportAttributes: FilteredModelAttributes<VideoImportModel>
  user: MUser
}): Promise<MVideoImportFormattable> {
  const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters

  const videoImport = await sequelizeTypescript.transaction(async t => {
    const sequelizeOptions = { transaction: t }

    // Save video object in database
    const videoCreated = await video.save(sequelizeOptions) as (MVideoAccountDefault & MVideoWithBlacklistLight & MVideoTag)
    videoCreated.VideoChannel = videoChannel

    if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
    if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t)

    await autoBlacklistVideoIfNeeded({
      video: videoCreated,
      user,
      notify: false,
      isRemote: false,
      isNew: true,
      transaction: t
    })

    await setVideoTags({ video: videoCreated, tags, transaction: t })

    // Create video import object in database
    const videoImport = await VideoImportModel.create(
      Object.assign({ videoId: videoCreated.id }, videoImportAttributes),
      sequelizeOptions
    ) as MVideoImportFormattable
    videoImport.Video = videoCreated

    return videoImport
  })

  return videoImport
}

async function buildVideoFromImport ({ channelId, importData, importDataOverride, importType }: {
  channelId: number
  importData: YoutubeDLInfo
  importDataOverride?: Partial<VideoImportCreate>
  importType: 'url' | 'torrent'
}): Promise<MVideoThumbnail> {
  let videoData = {
    name: importDataOverride?.name || importData.name || 'Unknown name',
    remote: false,
    category: importDataOverride?.category || importData.category,
    licence: importDataOverride?.licence ?? importData.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
    language: importDataOverride?.language || importData.language,
    commentsEnabled: importDataOverride?.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
    downloadEnabled: importDataOverride?.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
    waitTranscoding: importDataOverride?.waitTranscoding ?? true,
    state: VideoState.TO_IMPORT,
    nsfw: importDataOverride?.nsfw || importData.nsfw || false,
    description: importDataOverride?.description || importData.description,
    support: importDataOverride?.support || null,
    privacy: importDataOverride?.privacy || VideoPrivacy.PRIVATE,
    duration: 0, // duration will be set by the import job
    channelId,
    originallyPublishedAt: importDataOverride?.originallyPublishedAt
      ? new Date(importDataOverride?.originallyPublishedAt)
      : importData.originallyPublishedAtWithoutTime
  }

  videoData = await Hooks.wrapObject(
    videoData,
    importType === 'url'
      ? 'filter:api.video.import-url.video-attribute.result'
      : 'filter:api.video.import-torrent.video-attribute.result'
  )

  const video = new VideoModel(videoData)
  video.url = getLocalVideoActivityPubUrl(video)

  return video
}

async function buildYoutubeDLImport (options: {
  targetUrl: string
  channel: MChannelAccountDefault
  user: MUser
  channelSync?: MChannelSync
  importDataOverride?: Partial<VideoImportCreate>
  thumbnailFilePath?: string
  previewFilePath?: string
}) {
  const { targetUrl, channel, channelSync, importDataOverride, thumbnailFilePath, previewFilePath, user } = options

  const youtubeDL = new YoutubeDLWrapper(
    targetUrl,
    ServerConfigManager.Instance.getEnabledResolutions('vod'),
    CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
  )

  // Get video infos
  let youtubeDLInfo: YoutubeDLInfo
  try {
    youtubeDLInfo = await youtubeDL.getInfoForDownload()
  } catch (err) {
    throw YoutubeDlImportError.fromError(
      err, YoutubeDlImportError.CODE.FETCH_ERROR, `Cannot fetch information from import for URL ${targetUrl}`
    )
  }

  if (!await hasUnicastURLsOnly(youtubeDLInfo)) {
    throw new YoutubeDlImportError({
      message: 'Cannot use non unicast IP as targetUrl.',
      code: YoutubeDlImportError.CODE.NOT_ONLY_UNICAST_URL
    })
  }

  const video = await buildVideoFromImport({
    channelId: channel.id,
    importData: youtubeDLInfo,
    importDataOverride,
    importType: 'url'
  })

  const thumbnailModel = await forgeThumbnail({
    inputPath: thumbnailFilePath,
    downloadUrl: youtubeDLInfo.thumbnailUrl,
    video,
    type: ThumbnailType.MINIATURE
  })

  const previewModel = await forgeThumbnail({
    inputPath: previewFilePath,
    downloadUrl: youtubeDLInfo.thumbnailUrl,
    video,
    type: ThumbnailType.PREVIEW
  })

  const videoImport = await insertFromImportIntoDB({
    video,
    thumbnailModel,
    previewModel,
    videoChannel: channel,
    tags: importDataOverride?.tags || youtubeDLInfo.tags,
    user,
    videoImportAttributes: {
      targetUrl,
      state: VideoImportState.PENDING,
      userId: user.id,
      videoChannelSyncId: channelSync?.id
    }
  })

  // Get video subtitles
  await processYoutubeSubtitles(youtubeDL, targetUrl, video.id)

  let fileExt = `.${youtubeDLInfo.ext}`
  if (!isVideoFileExtnameValid(fileExt)) fileExt = '.mp4'

  const payload: VideoImportPayload = {
    type: 'youtube-dl' as 'youtube-dl',
    videoImportId: videoImport.id,
    fileExt,
    // If part of a sync process, there is a parent job that will aggregate children results
    preventException: !!channelSync
  }

  return {
    videoImport,
    job: { type: 'video-import' as 'video-import', payload }
  }
}

// ---------------------------------------------------------------------------

export {
  buildYoutubeDLImport,
  YoutubeDlImportError,
  insertFromImportIntoDB,
  buildVideoFromImport
}

// ---------------------------------------------------------------------------

async function forgeThumbnail ({ inputPath, video, downloadUrl, type }: {
  inputPath?: string
  downloadUrl?: string
  video: MVideoThumbnail
  type: ThumbnailType
}): Promise<MThumbnail> {
  if (inputPath) {
    return updateVideoMiniatureFromExisting({
      inputPath,
      video,
      type,
      automaticallyGenerated: false
    })
  } else if (downloadUrl) {
    try {
      return await updateVideoMiniatureFromUrl({ downloadUrl, video, type })
    } catch (err) {
      logger.warn('Cannot process thumbnail %s from youtube-dl.', downloadUrl, { err })
    }
  }
  return null
}

async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl: string, videoId: number) {
  try {
    const subtitles = await youtubeDL.getSubtitles()

    logger.info('Found %s subtitles candidates from youtube-dl import %s.', subtitles.length, targetUrl)

    for (const subtitle of subtitles) {
      if (!await isVTTFileValid(subtitle.path)) {
        logger.info('%s is not a valid youtube-dl subtitle, skipping', subtitle.path)
        await remove(subtitle.path)
        continue
      }

      const videoCaption = new VideoCaptionModel({
        videoId,
        language: subtitle.language,
        filename: VideoCaptionModel.generateCaptionName(subtitle.language)
      }) as MVideoCaption

      // Move physical file
      await moveAndProcessCaptionFile(subtitle, videoCaption)

      await sequelizeTypescript.transaction(async t => {
        await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t)
      })

      logger.info('Added %s youtube-dl subtitle', subtitle.path)
    }
  } catch (err) {
    logger.warn('Cannot get video subtitles.', { err })
  }
}

async function hasUnicastURLsOnly (youtubeDLInfo: YoutubeDLInfo) {
  const hosts = youtubeDLInfo.urls.map(u => new URL(u).hostname)
  const uniqHosts = new Set(hosts)

  for (const h of uniqHosts) {
    if (await isResolvingToUnicastOnly(h) !== true) {
      return false
    }
  }

  return true
}