]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file.ts
Add checkbox to check every rows
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
CommitLineData
94831479 1import * as Bull from 'bull'
2186386c 2import { VideoResolution, VideoState } from '../../../../shared'
da854ddd 3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { VideoModel } from '../../../models/video/video'
94a5ff8a 5import { JobQueue } from '../job-queue'
2186386c
C
6import { federateVideoIfNeeded } from '../../activitypub'
7import { retryTransactionWrapper } from '../../../helpers/database-utils'
8import { sequelizeTypescript } from '../../../initializers'
94831479 9import * as Bluebird from 'bluebird'
06215f15 10import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
098eb377 11import { importVideoFile, transcodeOriginalVideofile, optimizeOriginalVideofile } from '../../video-transcoding'
40298b02 12
94a5ff8a
C
13export type VideoFilePayload = {
14 videoUUID: string
2186386c 15 isNewVideo?: boolean
0c948c16 16 resolution?: VideoResolution
056aa7f2 17 isPortraitMode?: boolean
94a5ff8a
C
18}
19
28be8916 20export type VideoFileImportPayload = {
0138af92
FF
21 videoUUID: string,
22 filePath: string
23}
24
94831479 25async function processVideoFileImport (job: Bull.Job) {
28be8916
C
26 const payload = job.data as VideoFileImportPayload
27 logger.info('Processing video file import in job %d.', job.id)
0138af92 28
627621c1 29 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
0138af92
FF
30 // No video, maybe deleted?
31 if (!video) {
c1e791ba 32 logger.info('Do not process job %d, video does not exist.', job.id)
0138af92
FF
33 return undefined
34 }
35
098eb377 36 await importVideoFile(video, payload.filePath)
0138af92
FF
37
38 await onVideoFileTranscoderOrImportSuccess(video)
39 return video
40}
41
94831479 42async function processVideoFile (job: Bull.Job) {
94a5ff8a
C
43 const payload = job.data as VideoFilePayload
44 logger.info('Processing video file in job %d.', job.id)
45
627621c1 46 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
f5028693
C
47 // No video, maybe deleted?
48 if (!video) {
c1e791ba 49 logger.info('Do not process job %d, video does not exist.', job.id)
f5028693
C
50 return undefined
51 }
52
94a5ff8a
C
53 // Transcoding in other resolution
54 if (payload.resolution) {
098eb377 55 await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false)
2186386c 56
90d4bb81 57 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
94a5ff8a 58 } else {
098eb377 59 await optimizeOriginalVideofile(video)
2186386c 60
90d4bb81 61 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
94a5ff8a 62 }
031094f7 63
f5028693 64 return video
40298b02
C
65}
66
0138af92 67async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
94a5ff8a
C
68 if (video === undefined) return undefined
69
2186386c
C
70 return sequelizeTypescript.transaction(async t => {
71 // Maybe the video changed in database, refresh it
627621c1 72 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
2186386c
C
73 // Video does not exist anymore
74 if (!videoDatabase) return undefined
94a5ff8a 75
1b952dd4
C
76 let isNewVideo = false
77
2186386c 78 // We transcoded the video file in another format, now we can publish it
1b952dd4
C
79 if (videoDatabase.state !== VideoState.PUBLISHED) {
80 isNewVideo = true
81
82 videoDatabase.state = VideoState.PUBLISHED
83 videoDatabase.publishedAt = new Date()
84 videoDatabase = await videoDatabase.save({ transaction: t })
85 }
2186386c
C
86
87 // If the video was not published, we consider it is a new one for other instances
2186386c 88 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
94a5ff8a 89
2186386c
C
90 return undefined
91 })
40298b02
C
92}
93
0c948c16 94async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
031094f7
C
95 if (video === undefined) return undefined
96
2186386c
C
97 // Outside the transaction (IO on disk)
98 const { videoFileResolution } = await video.getOriginalFileResolution()
99
100 return sequelizeTypescript.transaction(async t => {
101 // Maybe the video changed in database, refresh it
627621c1 102 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
2186386c
C
103 // Video does not exist anymore
104 if (!videoDatabase) return undefined
105
106 // Create transcoding jobs if there are enabled resolutions
107 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
108 logger.info(
109 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
110 { resolutions: resolutionsEnabled }
111 )
112
113 if (resolutionsEnabled.length !== 0) {
94831479 114 const tasks: Bluebird<any>[] = []
2186386c
C
115
116 for (const resolution of resolutionsEnabled) {
117 const dataInput = {
118 videoUUID: videoDatabase.uuid,
119 resolution
120 }
121
122 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
123 tasks.push(p)
124 }
f5028693 125
2186386c 126 await Promise.all(tasks)
a7977280 127
2186386c
C
128 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
129 } else {
130 // No transcoding to do, it's now published
131 video.state = VideoState.PUBLISHED
132 video = await video.save({ transaction: t })
a7977280 133
2186386c 134 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
f5028693 135 }
a7977280 136
2186386c
C
137 return federateVideoIfNeeded(video, isNewVideo, t)
138 })
40298b02
C
139}
140
141// ---------------------------------------------------------------------------
142
143export {
0138af92 144 processVideoFile,
28be8916 145 processVideoFileImport
40298b02 146}