]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-import.ts
Merge branch 'release/beta-10' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-import.ts
1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { downloadYoutubeDLVideo } from '../../../helpers/youtube-dl'
4 import { VideoImportModel } from '../../../models/video/video-import'
5 import { VideoImportState } from '../../../../shared/models/videos'
6 import { getDurationFromVideoFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
7 import { extname, join } from 'path'
8 import { VideoFileModel } from '../../../models/video/video-file'
9 import { renamePromise, statPromise, unlinkPromise } from '../../../helpers/core-utils'
10 import { CONFIG, sequelizeTypescript } from '../../../initializers'
11 import { doRequestAndSaveToFile } from '../../../helpers/requests'
12 import { VideoState } from '../../../../shared'
13 import { JobQueue } from '../index'
14 import { federateVideoIfNeeded } from '../../activitypub'
15 import { VideoModel } from '../../../models/video/video'
16
17 export type VideoImportPayload = {
18 type: 'youtube-dl'
19 videoImportId: number
20 thumbnailUrl: string
21 downloadThumbnail: boolean
22 downloadPreview: boolean
23 }
24
25 async function processVideoImport (job: Bull.Job) {
26 const payload = job.data as VideoImportPayload
27 logger.info('Processing video import in job %d.', job.id)
28
29 const videoImport = await VideoImportModel.loadAndPopulateVideo(payload.videoImportId)
30 if (!videoImport || !videoImport.Video) {
31 throw new Error('Cannot import video %s: the video import or video linked to this import does not exist anymore.')
32 }
33
34 let tempVideoPath: string
35 let videoDestFile: string
36 let videoFile: VideoFileModel
37 try {
38 // Download video from youtubeDL
39 tempVideoPath = await downloadYoutubeDLVideo(videoImport.targetUrl)
40
41 // Get information about this video
42 const { videoFileResolution } = await getVideoFileResolution(tempVideoPath)
43 const fps = await getVideoFileFPS(tempVideoPath)
44 const stats = await statPromise(tempVideoPath)
45 const duration = await getDurationFromVideoFile(tempVideoPath)
46
47 // Create video file object in database
48 const videoFileData = {
49 extname: extname(tempVideoPath),
50 resolution: videoFileResolution,
51 size: stats.size,
52 fps,
53 videoId: videoImport.videoId
54 }
55 videoFile = new VideoFileModel(videoFileData)
56 // Import if the import fails, to clean files
57 videoImport.Video.VideoFiles = [ videoFile ]
58
59 // Move file
60 videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile))
61 await renamePromise(tempVideoPath, videoDestFile)
62 tempVideoPath = null // This path is not used anymore
63
64 // Process thumbnail
65 if (payload.downloadThumbnail) {
66 if (payload.thumbnailUrl) {
67 const destThumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, videoImport.Video.getThumbnailName())
68 await doRequestAndSaveToFile({ method: 'GET', uri: payload.thumbnailUrl }, destThumbnailPath)
69 } else {
70 await videoImport.Video.createThumbnail(videoFile)
71 }
72 }
73
74 // Process preview
75 if (payload.downloadPreview) {
76 if (payload.thumbnailUrl) {
77 const destPreviewPath = join(CONFIG.STORAGE.PREVIEWS_DIR, videoImport.Video.getPreviewName())
78 await doRequestAndSaveToFile({ method: 'GET', uri: payload.thumbnailUrl }, destPreviewPath)
79 } else {
80 await videoImport.Video.createPreview(videoFile)
81 }
82 }
83
84 // Create torrent
85 await videoImport.Video.createTorrentAndSetInfoHash(videoFile)
86
87 const videoImportUpdated: VideoImportModel = await sequelizeTypescript.transaction(async t => {
88 // Refresh video
89 const video = await VideoModel.load(videoImport.videoId, t)
90 if (!video) throw new Error('Video linked to import ' + videoImport.videoId + ' does not exist anymore.')
91 videoImport.Video = video
92
93 const videoFileCreated = await videoFile.save({ transaction: t })
94 video.VideoFiles = [ videoFileCreated ]
95
96 // Update video DB object
97 video.duration = duration
98 video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED
99 const videoUpdated = await video.save({ transaction: t })
100
101 // Now we can federate the video (reload from database, we need more attributes)
102 const videoForFederation = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
103 await federateVideoIfNeeded(videoForFederation, true, t)
104
105 // Update video import object
106 videoImport.state = VideoImportState.SUCCESS
107 const videoImportUpdated = await videoImport.save({ transaction: t })
108
109 logger.info('Video %s imported.', videoImport.targetUrl)
110
111 videoImportUpdated.Video = videoUpdated
112 return videoImportUpdated
113 })
114
115 // Create transcoding jobs?
116 if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) {
117 // Put uuid because we don't have id auto incremented for now
118 const dataInput = {
119 videoUUID: videoImportUpdated.Video.uuid,
120 isNewVideo: true
121 }
122
123 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
124 }
125
126 } catch (err) {
127 try {
128 if (tempVideoPath) await unlinkPromise(tempVideoPath)
129 } catch (errUnlink) {
130 logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink })
131 }
132
133 videoImport.error = err.message
134 videoImport.state = VideoImportState.FAILED
135 await videoImport.save()
136
137 throw err
138 }
139 }
140
141 // ---------------------------------------------------------------------------
142
143 export {
144 processVideoImport
145 }