2023-07-31 07:34:36 -05:00
|
|
|
import {
|
|
|
|
ThumbnailType,
|
|
|
|
ThumbnailType_Type,
|
|
|
|
VideoImportCreate,
|
|
|
|
VideoImportPayload,
|
|
|
|
VideoImportState,
|
|
|
|
VideoPrivacy,
|
|
|
|
VideoState
|
|
|
|
} from '@peertube/peertube-models'
|
|
|
|
import { isVTTFileValid } from '@server/helpers/custom-validators/video-captions.js'
|
|
|
|
import { isVideoFileExtnameValid } from '@server/helpers/custom-validators/videos.js'
|
|
|
|
import { isResolvingToUnicastOnly } from '@server/helpers/dns.js'
|
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { YoutubeDLInfo, YoutubeDLWrapper } from '@server/helpers/youtube-dl/index.js'
|
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks.js'
|
|
|
|
import { ServerConfigManager } from '@server/lib/server-config-manager.js'
|
|
|
|
import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { buildCommentsPolicy, setVideoTags } from '@server/lib/video.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { VideoImportModel } from '@server/models/video/video-import.js'
|
|
|
|
import { VideoPasswordModel } from '@server/models/video/video-password.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { FilteredModelAttributes } from '@server/types/index.js'
|
2022-08-10 02:53:39 -05:00
|
|
|
import {
|
|
|
|
MChannelAccountDefault,
|
|
|
|
MChannelSync,
|
|
|
|
MThumbnail,
|
|
|
|
MUser,
|
2024-02-14 02:21:53 -06:00
|
|
|
MVideo,
|
|
|
|
MVideoAccountDefault, MVideoImportFormattable,
|
2022-08-10 02:53:39 -05:00
|
|
|
MVideoTag,
|
|
|
|
MVideoThumbnail,
|
|
|
|
MVideoWithBlacklistLight
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '@server/types/models/index.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { remove } from 'fs-extra/esm'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { getLocalVideoActivityPubUrl } from './activitypub/url.js'
|
|
|
|
import { updateLocalVideoMiniatureFromExisting, updateLocalVideoMiniatureFromUrl } from './thumbnail.js'
|
2024-02-14 02:21:53 -06:00
|
|
|
import { createLocalCaption } from './video-captions.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { replaceChapters, replaceChaptersFromDescriptionIfNeeded } from './video-chapters.js'
|
2022-08-10 02:53:39 -05:00
|
|
|
|
|
|
|
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
|
2023-06-29 02:48:55 -05:00
|
|
|
videoPasswords?: string[]
|
2022-08-10 02:53:39 -05:00
|
|
|
}): Promise<MVideoImportFormattable> {
|
2023-06-29 02:48:55 -05:00
|
|
|
const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user, videoPasswords } = parameters
|
2022-08-10 02:53:39 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-06-29 02:48:55 -05:00
|
|
|
if (videoCreated.privacy === VideoPrivacy.PASSWORD_PROTECTED) {
|
|
|
|
await VideoPasswordModel.addPasswords(videoPasswords, video.id, t)
|
|
|
|
}
|
|
|
|
|
2022-08-10 02:53:39 -05:00
|
|
|
await autoBlacklistVideoIfNeeded({
|
|
|
|
video: videoCreated,
|
|
|
|
user,
|
|
|
|
notify: false,
|
|
|
|
isRemote: false,
|
|
|
|
isNew: true,
|
2023-07-19 09:02:49 -05:00
|
|
|
isNewFile: true,
|
2022-08-10 02:53:39 -05:00
|
|
|
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,
|
2024-03-29 08:25:03 -05:00
|
|
|
commentsPolicy: buildCommentsPolicy(importDataOverride),
|
2022-08-10 02:53:39 -05:00
|
|
|
downloadEnabled: importDataOverride?.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
|
2022-09-16 06:31:59 -05:00
|
|
|
waitTranscoding: importDataOverride?.waitTranscoding ?? true,
|
2022-08-10 02:53:39 -05:00
|
|
|
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)
|
2022-09-16 02:37:07 -05:00
|
|
|
: importData.originallyPublishedAtWithoutTime
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-08-10 04:51:13 -05:00
|
|
|
userId: user.id,
|
|
|
|
videoChannelSyncId: channelSync?.id
|
2023-06-29 02:48:55 -05:00
|
|
|
},
|
|
|
|
videoPasswords: importDataOverride.videoPasswords
|
2022-08-10 02:53:39 -05:00
|
|
|
})
|
|
|
|
|
2023-08-28 03:55:04 -05:00
|
|
|
await sequelizeTypescript.transaction(async transaction => {
|
2024-03-15 11:01:14 -05:00
|
|
|
// Priority to explicitly set description
|
2023-08-28 03:55:04 -05:00
|
|
|
if (importDataOverride?.description) {
|
|
|
|
const inserted = await replaceChaptersFromDescriptionIfNeeded({ newDescription: importDataOverride.description, video, transaction })
|
|
|
|
if (inserted) return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then priority to youtube-dl chapters
|
|
|
|
if (youtubeDLInfo.chapters.length !== 0) {
|
|
|
|
logger.info(
|
|
|
|
`Inserting chapters in video ${video.uuid} from youtube-dl`,
|
|
|
|
{ chapters: youtubeDLInfo.chapters, tags: [ 'chapters', video.uuid ] }
|
|
|
|
)
|
|
|
|
|
|
|
|
await replaceChapters({ video, chapters: youtubeDLInfo.chapters, transaction })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (video.description) {
|
|
|
|
await replaceChaptersFromDescriptionIfNeeded({ newDescription: video.description, video, transaction })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-10 02:53:39 -05:00
|
|
|
// Get video subtitles
|
2024-02-14 02:21:53 -06:00
|
|
|
await processYoutubeSubtitles(youtubeDL, targetUrl, video)
|
2022-08-10 02:53:39 -05:00
|
|
|
|
|
|
|
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 {
|
2024-03-29 08:25:03 -05:00
|
|
|
YoutubeDlImportError, buildVideoFromImport, buildYoutubeDLImport, insertFromImportIntoDB
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function forgeThumbnail ({ inputPath, video, downloadUrl, type }: {
|
|
|
|
inputPath?: string
|
|
|
|
downloadUrl?: string
|
|
|
|
video: MVideoThumbnail
|
2023-07-31 07:34:36 -05:00
|
|
|
type: ThumbnailType_Type
|
2022-08-10 02:53:39 -05:00
|
|
|
}): Promise<MThumbnail> {
|
|
|
|
if (inputPath) {
|
2023-06-06 08:59:51 -05:00
|
|
|
return updateLocalVideoMiniatureFromExisting({
|
2022-08-10 02:53:39 -05:00
|
|
|
inputPath,
|
|
|
|
video,
|
|
|
|
type,
|
|
|
|
automaticallyGenerated: false
|
|
|
|
})
|
2023-06-07 01:53:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (downloadUrl) {
|
2022-08-10 02:53:39 -05:00
|
|
|
try {
|
2023-06-19 02:56:12 -05:00
|
|
|
return await updateLocalVideoMiniatureFromUrl({ downloadUrl, video, type })
|
2022-08-10 02:53:39 -05:00
|
|
|
} catch (err) {
|
2023-05-26 09:00:19 -05:00
|
|
|
logger.warn('Cannot process thumbnail %s from youtube-dl.', downloadUrl, { err })
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 01:53:14 -05:00
|
|
|
|
2022-08-10 02:53:39 -05:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:21:53 -06:00
|
|
|
async function processYoutubeSubtitles (youtubeDL: YoutubeDLWrapper, targetUrl: string, video: MVideo) {
|
2022-08-10 02:53:39 -05:00
|
|
|
try {
|
|
|
|
const subtitles = await youtubeDL.getSubtitles()
|
|
|
|
|
2023-05-26 09:00:19 -05:00
|
|
|
logger.info('Found %s subtitles candidates from youtube-dl import %s.', subtitles.length, targetUrl)
|
2022-08-10 02:53:39 -05:00
|
|
|
|
|
|
|
for (const subtitle of subtitles) {
|
|
|
|
if (!await isVTTFileValid(subtitle.path)) {
|
2023-05-26 09:00:19 -05:00
|
|
|
logger.info('%s is not a valid youtube-dl subtitle, skipping', subtitle.path)
|
2022-08-10 02:53:39 -05:00
|
|
|
await remove(subtitle.path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:21:53 -06:00
|
|
|
await createLocalCaption({ language: subtitle.language, path: subtitle.path, video })
|
2023-05-26 09:00:19 -05:00
|
|
|
|
|
|
|
logger.info('Added %s youtube-dl subtitle', subtitle.path)
|
2022-08-10 02:53:39 -05:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
}
|