]>
Commit | Line | Data |
---|---|---|
fbad87b0 C |
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' | |
516df59b | 15 | import { VideoModel } from '../../../models/video/video' |
fbad87b0 C |
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) | |
516df59b C |
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 | } | |
fbad87b0 C |
33 | |
34 | let tempVideoPath: string | |
516df59b C |
35 | let videoDestFile: string |
36 | let videoFile: VideoFileModel | |
fbad87b0 C |
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) | |
d7f83948 | 43 | const fps = await getVideoFileFPS(tempVideoPath) |
fbad87b0 C |
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 | } | |
516df59b C |
55 | videoFile = new VideoFileModel(videoFileData) |
56 | // Import if the import fails, to clean files | |
57 | videoImport.Video.VideoFiles = [ videoFile ] | |
fbad87b0 C |
58 | |
59 | // Move file | |
516df59b C |
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 | |
fbad87b0 C |
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 => { | |
516df59b C |
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 ] | |
fbad87b0 C |
95 | |
96 | // Update video DB object | |
516df59b C |
97 | video.duration = duration |
98 | video.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | |
99 | const videoUpdated = await video.save({ transaction: t }) | |
fbad87b0 C |
100 | |
101 | // Now we can federate the video | |
516df59b | 102 | await federateVideoIfNeeded(video, true, t) |
fbad87b0 C |
103 | |
104 | // Update video import object | |
105 | videoImport.state = VideoImportState.SUCCESS | |
106 | const videoImportUpdated = await videoImport.save({ transaction: t }) | |
107 | ||
108 | logger.info('Video %s imported.', videoImport.targetUrl) | |
109 | ||
110 | videoImportUpdated.Video = videoUpdated | |
111 | return videoImportUpdated | |
112 | }) | |
113 | ||
114 | // Create transcoding jobs? | |
115 | if (videoImportUpdated.Video.state === VideoState.TO_TRANSCODE) { | |
116 | // Put uuid because we don't have id auto incremented for now | |
117 | const dataInput = { | |
118 | videoUUID: videoImportUpdated.Video.uuid, | |
119 | isNewVideo: true | |
120 | } | |
121 | ||
122 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
123 | } | |
124 | ||
125 | } catch (err) { | |
126 | try { | |
127 | if (tempVideoPath) await unlinkPromise(tempVideoPath) | |
128 | } catch (errUnlink) { | |
516df59b | 129 | logger.warn('Cannot cleanup files after a video import error.', { err: errUnlink }) |
fbad87b0 C |
130 | } |
131 | ||
d7f83948 | 132 | videoImport.error = err.message |
fbad87b0 C |
133 | videoImport.state = VideoImportState.FAILED |
134 | await videoImport.save() | |
135 | ||
136 | throw err | |
137 | } | |
138 | } | |
139 | ||
140 | // --------------------------------------------------------------------------- | |
141 | ||
142 | export { | |
143 | processVideoImport | |
144 | } |