diff options
-rw-r--r-- | server/lib/job-queue/handlers/video-file-import.ts | 78 | ||||
-rw-r--r-- | server/lib/job-queue/handlers/video-transcoding.ts | 34 | ||||
-rw-r--r-- | server/lib/job-queue/job-queue.ts | 8 | ||||
-rw-r--r-- | server/lib/video-transcoding.ts | 49 |
4 files changed, 89 insertions, 80 deletions
diff --git a/server/lib/job-queue/handlers/video-file-import.ts b/server/lib/job-queue/handlers/video-file-import.ts new file mode 100644 index 000000000..921d9a083 --- /dev/null +++ b/server/lib/job-queue/handlers/video-file-import.ts | |||
@@ -0,0 +1,78 @@ | |||
1 | import * as Bull from 'bull' | ||
2 | import { logger } from '../../../helpers/logger' | ||
3 | import { VideoModel } from '../../../models/video/video' | ||
4 | import { publishVideoIfNeeded } from './video-transcoding' | ||
5 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' | ||
6 | import { copy, stat } from 'fs-extra' | ||
7 | import { VideoFileModel } from '../../../models/video/video-file' | ||
8 | import { extname } from 'path' | ||
9 | |||
10 | export type VideoFileImportPayload = { | ||
11 | videoUUID: string, | ||
12 | filePath: string | ||
13 | } | ||
14 | |||
15 | async function processVideoFileImport (job: Bull.Job) { | ||
16 | const payload = job.data as VideoFileImportPayload | ||
17 | logger.info('Processing video file import in job %d.', job.id) | ||
18 | |||
19 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | ||
20 | // No video, maybe deleted? | ||
21 | if (!video) { | ||
22 | logger.info('Do not process job %d, video does not exist.', job.id) | ||
23 | return undefined | ||
24 | } | ||
25 | |||
26 | await updateVideoFile(video, payload.filePath) | ||
27 | |||
28 | await publishVideoIfNeeded(video) | ||
29 | return video | ||
30 | } | ||
31 | |||
32 | // --------------------------------------------------------------------------- | ||
33 | |||
34 | export { | ||
35 | processVideoFileImport | ||
36 | } | ||
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | async function updateVideoFile (video: VideoModel, inputFilePath: string) { | ||
41 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) | ||
42 | const { size } = await stat(inputFilePath) | ||
43 | const fps = await getVideoFileFPS(inputFilePath) | ||
44 | |||
45 | let updatedVideoFile = new VideoFileModel({ | ||
46 | resolution: videoFileResolution, | ||
47 | extname: extname(inputFilePath), | ||
48 | size, | ||
49 | fps, | ||
50 | videoId: video.id | ||
51 | }) | ||
52 | |||
53 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) | ||
54 | |||
55 | if (currentVideoFile) { | ||
56 | // Remove old file and old torrent | ||
57 | await video.removeFile(currentVideoFile) | ||
58 | await video.removeTorrent(currentVideoFile) | ||
59 | // Remove the old video file from the array | ||
60 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | ||
61 | |||
62 | // Update the database | ||
63 | currentVideoFile.set('extname', updatedVideoFile.extname) | ||
64 | currentVideoFile.set('size', updatedVideoFile.size) | ||
65 | currentVideoFile.set('fps', updatedVideoFile.fps) | ||
66 | |||
67 | updatedVideoFile = currentVideoFile | ||
68 | } | ||
69 | |||
70 | const outputPath = video.getVideoFilePath(updatedVideoFile) | ||
71 | await copy(inputFilePath, outputPath) | ||
72 | |||
73 | await video.createTorrentAndSetInfoHash(updatedVideoFile) | ||
74 | |||
75 | await updatedVideoFile.save() | ||
76 | |||
77 | video.VideoFiles.push(updatedVideoFile) | ||
78 | } | ||
diff --git a/server/lib/job-queue/handlers/video-transcoding.ts b/server/lib/job-queue/handlers/video-transcoding.ts index ceee83f13..d9dad795e 100644 --- a/server/lib/job-queue/handlers/video-transcoding.ts +++ b/server/lib/job-queue/handlers/video-transcoding.ts | |||
@@ -5,10 +5,10 @@ import { VideoModel } from '../../../models/video/video' | |||
5 | import { JobQueue } from '../job-queue' | 5 | import { JobQueue } from '../job-queue' |
6 | import { federateVideoIfNeeded } from '../../activitypub' | 6 | import { federateVideoIfNeeded } from '../../activitypub' |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
8 | import { sequelizeTypescript, CONFIG } from '../../../initializers' | 8 | import { CONFIG, sequelizeTypescript } from '../../../initializers' |
9 | import * as Bluebird from 'bluebird' | 9 | import * as Bluebird from 'bluebird' |
10 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' | 10 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' |
11 | import { generateHlsPlaylist, importVideoFile, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding' | 11 | import { generateHlsPlaylist, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding' |
12 | import { Notifier } from '../../notifier' | 12 | import { Notifier } from '../../notifier' |
13 | 13 | ||
14 | export type VideoTranscodingPayload = { | 14 | export type VideoTranscodingPayload = { |
@@ -19,28 +19,6 @@ export type VideoTranscodingPayload = { | |||
19 | generateHlsPlaylist?: boolean | 19 | generateHlsPlaylist?: boolean |
20 | } | 20 | } |
21 | 21 | ||
22 | export type VideoFileImportPayload = { | ||
23 | videoUUID: string, | ||
24 | filePath: string | ||
25 | } | ||
26 | |||
27 | async function processVideoFileImport (job: Bull.Job) { | ||
28 | const payload = job.data as VideoFileImportPayload | ||
29 | logger.info('Processing video file import in job %d.', job.id) | ||
30 | |||
31 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) | ||
32 | // No video, maybe deleted? | ||
33 | if (!video) { | ||
34 | logger.info('Do not process job %d, video does not exist.', job.id) | ||
35 | return undefined | ||
36 | } | ||
37 | |||
38 | await importVideoFile(video, payload.filePath) | ||
39 | |||
40 | await onVideoFileTranscoderOrImportSuccess(video) | ||
41 | return video | ||
42 | } | ||
43 | |||
44 | async function processVideoTranscoding (job: Bull.Job) { | 22 | async function processVideoTranscoding (job: Bull.Job) { |
45 | const payload = job.data as VideoTranscodingPayload | 23 | const payload = job.data as VideoTranscodingPayload |
46 | logger.info('Processing video file in job %d.', job.id) | 24 | logger.info('Processing video file in job %d.', job.id) |
@@ -59,7 +37,7 @@ async function processVideoTranscoding (job: Bull.Job) { | |||
59 | } else if (payload.resolution) { // Transcoding in other resolution | 37 | } else if (payload.resolution) { // Transcoding in other resolution |
60 | await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false) | 38 | await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false) |
61 | 39 | ||
62 | await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video, payload) | 40 | await retryTransactionWrapper(publishVideoIfNeeded, video, payload) |
63 | } else { | 41 | } else { |
64 | await optimizeVideofile(video) | 42 | await optimizeVideofile(video) |
65 | 43 | ||
@@ -83,9 +61,7 @@ async function onHlsPlaylistGenerationSuccess (video: VideoModel) { | |||
83 | }) | 61 | }) |
84 | } | 62 | } |
85 | 63 | ||
86 | async function onVideoFileTranscoderOrImportSuccess (video: VideoModel, payload?: VideoTranscodingPayload) { | 64 | async function publishVideoIfNeeded (video: VideoModel, payload?: VideoTranscodingPayload) { |
87 | if (video === undefined) return undefined | ||
88 | |||
89 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { | 65 | const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => { |
90 | // Maybe the video changed in database, refresh it | 66 | // Maybe the video changed in database, refresh it |
91 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) | 67 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
@@ -183,7 +159,7 @@ async function onVideoFileOptimizerSuccess (videoArg: VideoModel, payload: Video | |||
183 | 159 | ||
184 | export { | 160 | export { |
185 | processVideoTranscoding, | 161 | processVideoTranscoding, |
186 | processVideoFileImport | 162 | publishVideoIfNeeded |
187 | } | 163 | } |
188 | 164 | ||
189 | // --------------------------------------------------------------------------- | 165 | // --------------------------------------------------------------------------- |
diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index e73042163..cee0941c7 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts | |||
@@ -7,16 +7,12 @@ import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from | |||
7 | import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' | 7 | import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' |
8 | import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' | 8 | import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' |
9 | import { EmailPayload, processEmail } from './handlers/email' | 9 | import { EmailPayload, processEmail } from './handlers/email' |
10 | import { | 10 | import { processVideoTranscoding, VideoTranscodingPayload } from './handlers/video-transcoding' |
11 | processVideoFileImport, | ||
12 | processVideoTranscoding, | ||
13 | VideoFileImportPayload, | ||
14 | VideoTranscodingPayload | ||
15 | } from './handlers/video-transcoding' | ||
16 | import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow' | 11 | import { ActivitypubFollowPayload, processActivityPubFollow } from './handlers/activitypub-follow' |
17 | import { processVideoImport, VideoImportPayload } from './handlers/video-import' | 12 | import { processVideoImport, VideoImportPayload } from './handlers/video-import' |
18 | import { processVideosViews } from './handlers/video-views' | 13 | import { processVideosViews } from './handlers/video-views' |
19 | import { refreshAPObject, RefreshPayload } from './handlers/activitypub-refresher' | 14 | import { refreshAPObject, RefreshPayload } from './handlers/activitypub-refresher' |
15 | import { processVideoFileImport, VideoFileImportPayload } from './handlers/video-file-import' | ||
20 | 16 | ||
21 | type CreateJobArgument = | 17 | type CreateJobArgument = |
22 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | | 18 | { type: 'activitypub-http-broadcast', payload: ActivitypubHttpBroadcastPayload } | |
diff --git a/server/lib/video-transcoding.ts b/server/lib/video-transcoding.ts index e932c0e55..c29e34ab5 100644 --- a/server/lib/video-transcoding.ts +++ b/server/lib/video-transcoding.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY } from '../initializers' | 1 | import { CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY } from '../initializers' |
2 | import { extname, join } from 'path' | 2 | import { join } from 'path' |
3 | import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils' | 3 | import { getVideoFileFPS, transcode } from '../helpers/ffmpeg-utils' |
4 | import { copy, ensureDir, move, remove, stat } from 'fs-extra' | 4 | import { ensureDir, move, remove, stat } from 'fs-extra' |
5 | import { logger } from '../helpers/logger' | 5 | import { logger } from '../helpers/logger' |
6 | import { VideoResolution } from '../../shared/models/videos' | 6 | import { VideoResolution } from '../../shared/models/videos' |
7 | import { VideoFileModel } from '../models/video/video-file' | 7 | import { VideoFileModel } from '../models/video/video-file' |
@@ -123,49 +123,8 @@ async function generateHlsPlaylist (video: VideoModel, resolution: VideoResoluti | |||
123 | }) | 123 | }) |
124 | } | 124 | } |
125 | 125 | ||
126 | async function importVideoFile (video: VideoModel, inputFilePath: string) { | ||
127 | const { videoFileResolution } = await getVideoFileResolution(inputFilePath) | ||
128 | const { size } = await stat(inputFilePath) | ||
129 | const fps = await getVideoFileFPS(inputFilePath) | ||
130 | |||
131 | let updatedVideoFile = new VideoFileModel({ | ||
132 | resolution: videoFileResolution, | ||
133 | extname: extname(inputFilePath), | ||
134 | size, | ||
135 | fps, | ||
136 | videoId: video.id | ||
137 | }) | ||
138 | |||
139 | const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === updatedVideoFile.resolution) | ||
140 | |||
141 | if (currentVideoFile) { | ||
142 | // Remove old file and old torrent | ||
143 | await video.removeFile(currentVideoFile) | ||
144 | await video.removeTorrent(currentVideoFile) | ||
145 | // Remove the old video file from the array | ||
146 | video.VideoFiles = video.VideoFiles.filter(f => f !== currentVideoFile) | ||
147 | |||
148 | // Update the database | ||
149 | currentVideoFile.set('extname', updatedVideoFile.extname) | ||
150 | currentVideoFile.set('size', updatedVideoFile.size) | ||
151 | currentVideoFile.set('fps', updatedVideoFile.fps) | ||
152 | |||
153 | updatedVideoFile = currentVideoFile | ||
154 | } | ||
155 | |||
156 | const outputPath = video.getVideoFilePath(updatedVideoFile) | ||
157 | await copy(inputFilePath, outputPath) | ||
158 | |||
159 | await video.createTorrentAndSetInfoHash(updatedVideoFile) | ||
160 | |||
161 | await updatedVideoFile.save() | ||
162 | |||
163 | video.VideoFiles.push(updatedVideoFile) | ||
164 | } | ||
165 | |||
166 | export { | 126 | export { |
167 | generateHlsPlaylist, | 127 | generateHlsPlaylist, |
168 | optimizeVideofile, | 128 | optimizeVideofile, |
169 | transcodeOriginalVideofile, | 129 | transcodeOriginalVideofile |
170 | importVideoFile | ||
171 | } | 130 | } |