diff options
Diffstat (limited to 'server/helpers/video.ts')
-rw-r--r-- | server/helpers/video.ts | 63 |
1 files changed, 56 insertions, 7 deletions
diff --git a/server/helpers/video.ts b/server/helpers/video.ts index 5b9c026b1..6f76cbdfc 100644 --- a/server/helpers/video.ts +++ b/server/helpers/video.ts | |||
@@ -1,17 +1,26 @@ | |||
1 | import { VideoModel } from '../models/video/video' | 1 | import { VideoModel } from '../models/video/video' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Bluebird from 'bluebird' |
3 | import { | 3 | import { |
4 | isStreamingPlaylist, | ||
5 | MStreamingPlaylistVideo, | ||
6 | MVideo, | ||
4 | MVideoAccountLightBlacklistAllFiles, | 7 | MVideoAccountLightBlacklistAllFiles, |
8 | MVideoFile, | ||
5 | MVideoFullLight, | 9 | MVideoFullLight, |
6 | MVideoIdThumbnail, | 10 | MVideoIdThumbnail, |
11 | MVideoImmutable, | ||
7 | MVideoThumbnail, | 12 | MVideoThumbnail, |
8 | MVideoWithRights | 13 | MVideoWithRights |
9 | } from '@server/typings/models' | 14 | } from '@server/typings/models' |
10 | import { Response } from 'express' | 15 | import { Response } from 'express' |
16 | import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants' | ||
17 | import { JobQueue } from '@server/lib/job-queue' | ||
18 | import { VideoTranscodingPayload } from '@shared/models' | ||
11 | 19 | ||
12 | type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 20 | type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' |
13 | 21 | ||
14 | function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight> | 22 | function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight> |
23 | function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable> | ||
15 | function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail> | 24 | function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail> |
16 | function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights> | 25 | function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights> |
17 | function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail> | 26 | function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail> |
@@ -19,14 +28,16 @@ function fetchVideo ( | |||
19 | id: number | string, | 28 | id: number | string, |
20 | fetchType: VideoFetchType, | 29 | fetchType: VideoFetchType, |
21 | userId?: number | 30 | userId?: number |
22 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail> | 31 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> |
23 | function fetchVideo ( | 32 | function fetchVideo ( |
24 | id: number | string, | 33 | id: number | string, |
25 | fetchType: VideoFetchType, | 34 | fetchType: VideoFetchType, |
26 | userId?: number | 35 | userId?: number |
27 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail> { | 36 | ): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> { |
28 | if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) | 37 | if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) |
29 | 38 | ||
39 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) | ||
40 | |||
30 | if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) | 41 | if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) |
31 | 42 | ||
32 | if (fetchType === 'only-video') return VideoModel.load(id) | 43 | if (fetchType === 'only-video') return VideoModel.load(id) |
@@ -34,14 +45,23 @@ function fetchVideo ( | |||
34 | if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) | 45 | if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) |
35 | } | 46 | } |
36 | 47 | ||
37 | type VideoFetchByUrlType = 'all' | 'only-video' | 48 | type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' |
38 | 49 | ||
39 | function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles> | 50 | function fetchVideoByUrl (url: string, fetchType: 'all'): Bluebird<MVideoAccountLightBlacklistAllFiles> |
51 | function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable> | ||
40 | function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail> | 52 | function fetchVideoByUrl (url: string, fetchType: 'only-video'): Bluebird<MVideoThumbnail> |
41 | function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> | 53 | function fetchVideoByUrl ( |
42 | function fetchVideoByUrl (url: string, fetchType: VideoFetchByUrlType): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> { | 54 | url: string, |
55 | fetchType: VideoFetchByUrlType | ||
56 | ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> | ||
57 | function fetchVideoByUrl ( | ||
58 | url: string, | ||
59 | fetchType: VideoFetchByUrlType | ||
60 | ): Bluebird<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { | ||
43 | if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) | 61 | if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) |
44 | 62 | ||
63 | if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url) | ||
64 | |||
45 | if (fetchType === 'only-video') return VideoModel.loadByUrl(url) | 65 | if (fetchType === 'only-video') return VideoModel.loadByUrl(url) |
46 | } | 66 | } |
47 | 67 | ||
@@ -49,10 +69,39 @@ function getVideoWithAttributes (res: Response) { | |||
49 | return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights | 69 | return res.locals.videoAll || res.locals.onlyVideo || res.locals.onlyVideoWithRights |
50 | } | 70 | } |
51 | 71 | ||
72 | function addOptimizeOrMergeAudioJob (video: MVideo, videoFile: MVideoFile) { | ||
73 | let dataInput: VideoTranscodingPayload | ||
74 | |||
75 | if (videoFile.isAudio()) { | ||
76 | dataInput = { | ||
77 | type: 'merge-audio' as 'merge-audio', | ||
78 | resolution: DEFAULT_AUDIO_RESOLUTION, | ||
79 | videoUUID: video.uuid, | ||
80 | isNewVideo: true | ||
81 | } | ||
82 | } else { | ||
83 | dataInput = { | ||
84 | type: 'optimize' as 'optimize', | ||
85 | videoUUID: video.uuid, | ||
86 | isNewVideo: true | ||
87 | } | ||
88 | } | ||
89 | |||
90 | return JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: dataInput }) | ||
91 | } | ||
92 | |||
93 | function extractVideo (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) { | ||
94 | return isStreamingPlaylist(videoOrPlaylist) | ||
95 | ? videoOrPlaylist.Video | ||
96 | : videoOrPlaylist | ||
97 | } | ||
98 | |||
52 | export { | 99 | export { |
53 | VideoFetchType, | 100 | VideoFetchType, |
54 | VideoFetchByUrlType, | 101 | VideoFetchByUrlType, |
55 | fetchVideo, | 102 | fetchVideo, |
56 | getVideoWithAttributes, | 103 | getVideoWithAttributes, |
57 | fetchVideoByUrl | 104 | fetchVideoByUrl, |
105 | addOptimizeOrMergeAudioJob, | ||
106 | extractVideo | ||
58 | } | 107 | } |