import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
import { extname, join } from 'path'
import { VideoFileModel } from '../../../models/video/video-file'
-import { CONFIG, PREVIEWS_SIZE, sequelizeTypescript, THUMBNAILS_SIZE, VIDEO_IMPORT_TIMEOUT } from '../../../initializers'
-import { doRequestAndSaveToFile, downloadImage } from '../../../helpers/requests'
+import { VIDEO_IMPORT_TIMEOUT } from '../../../initializers/constants'
import { VideoState } from '../../../../shared'
import { JobQueue } from '../index'
import { federateVideoIfNeeded } from '../../activitypub'
import { VideoModel } from '../../../models/video/video'
import { downloadWebTorrentVideo } from '../../../helpers/webtorrent'
import { getSecureTorrentName } from '../../../helpers/utils'
-import { remove, rename, stat } from 'fs-extra'
+import { move, remove, stat } from 'fs-extra'
+import { Notifier } from '../../notifier'
+import { CONFIG } from '../../../initializers/config'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { ThumbnailModel } from '../../../models/video/thumbnail'
+import { createVideoThumbnailFromUrl, generateVideoThumbnail } from '../../thumbnail'
+import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type'
type VideoImportYoutubeDLPayload = {
type: 'youtube-dl'
let tempVideoPath: string
let videoDestFile: string
let videoFile: VideoFileModel
+
try {
// Download video from youtubeDL
tempVideoPath = await downloader()
// Move file
videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile))
- await rename(tempVideoPath, videoDestFile)
+ await move(tempVideoPath, videoDestFile)
tempVideoPath = null // This path is not used anymore
// Process thumbnail
- if (options.downloadThumbnail) {
- if (options.thumbnailUrl) {
- const destThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName())
- await downloadImage(options.thumbnailUrl, destThumbnailPath, THUMBNAILS_SIZE)
- } else {
- await videoImport.Video.createThumbnail(videoFile)
- }
- } else if (options.generateThumbnail) {
- await videoImport.Video.createThumbnail(videoFile)
+ let thumbnailModel: ThumbnailModel
+ if (options.downloadThumbnail && options.thumbnailUrl) {
+ thumbnailModel = await createVideoThumbnailFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.THUMBNAIL)
+ } else if (options.generateThumbnail || options.downloadThumbnail) {
+ thumbnailModel = await generateVideoThumbnail(videoImport.Video, videoFile, ThumbnailType.THUMBNAIL)
}
// Process preview
- if (options.downloadPreview) {
- if (options.thumbnailUrl) {
- const destPreviewPath = join(CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName())
- await downloadImage(options.thumbnailUrl, destPreviewPath, PREVIEWS_SIZE)
- } else {
- await videoImport.Video.createPreview(videoFile)
- }
- } else if (options.generatePreview) {
- await videoImport.Video.createPreview(videoFile)
+ let previewModel: ThumbnailModel
+ if (options.downloadPreview && options.thumbnailUrl) {
+ previewModel = await createVideoThumbnailFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.PREVIEW)
+ } else if (options.generatePreview || options.downloadPreview) {
+ previewModel = await generateVideoThumbnail(videoImport.Video, videoFile, ThumbnailType.PREVIEW)
}
// Create torrent
// Update video DB object
video.duration = duration
video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED
- const videoUpdated = await video.save({ transaction: t })
+ await video.save({ transaction: t })
+
+ if (thumbnailModel) {
+ thumbnailModel.videoId = video.id
+ video.addThumbnail(await thumbnailModel.save({ transaction: t }))
+ }
+ if (previewModel) {
+ previewModel.videoId = video.id
+ video.addThumbnail(await previewModel.save({ transaction: t }))
+ }
// Now we can federate the video (reload from database, we need more attributes)
const videoForFederation = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
logger.info('Video %s imported.', video.uuid)
- videoImportUpdated.Video = videoUpdated
+ videoImportUpdated.Video = videoForFederation
return videoImportUpdated
})
+ Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true)
+
+ if (videoImportUpdated.Video.VideoBlacklist) {
+ Notifier.Instance.notifyOnVideoAutoBlacklist(videoImportUpdated.Video)
+ } else {
+ Notifier.Instance.notifyOnNewVideo(videoImportUpdated.Video)
+ }
+
// Create transcoding jobs?
if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) {
// Put uuid because we don't have id auto incremented for now
isNewVideo: true
}
- await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
+ await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
}
} catch (err) {
videoImport.state = VideoImportState.FAILED
await videoImport.save()
+ Notifier.Instance.notifyOnFinishedVideoImport(videoImport, false)
+
throw err
}
}