]>
Commit | Line | Data |
---|---|---|
54909304 | 1 | import { mapValues } from 'lodash-es' |
1378c0d3 | 2 | import { firstValueFrom } from 'rxjs' |
4bc45da3 | 3 | import { tap } from 'rxjs/operators' |
68018040 | 4 | import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core' |
3545e72c | 5 | import { HooksService } from '@app/core' |
4bc45da3 | 6 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
42b40636 | 7 | import { logger } from '@root-helpers/logger' |
3545e72c | 8 | import { videoRequiresAuth } from '@root-helpers/video' |
54909304 | 9 | import { objectKeysTyped, pick } from '@shared/core-utils' |
67ed6552 | 10 | import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models' |
3545e72c | 11 | import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoFileTokenService, VideoService } from '../shared-main' |
8ba9c205 RK |
12 | |
13 | type DownloadType = 'video' | 'subtitles' | |
54909304 | 14 | type FileMetadata = { [key: string]: { label: string, value: string | number } } |
cf02fbfb C |
15 | |
16 | @Component({ | |
a96aed15 C |
17 | selector: 'my-video-download', |
18 | templateUrl: './video-download.component.html', | |
0727cab0 | 19 | styleUrls: [ './video-download.component.scss' ] |
cf02fbfb | 20 | }) |
3a0fb65c | 21 | export class VideoDownloadComponent { |
f36da21e | 22 | @ViewChild('modal', { static: true }) modal: ElementRef |
cf02fbfb | 23 | |
7bd455cb | 24 | downloadType: 'direct' | 'torrent' = 'direct' |
262f8ff6 | 25 | |
09700934 | 26 | resolutionId: number | string = -1 |
8ba9c205 | 27 | subtitleLanguageId: string |
5f0805d3 | 28 | |
8319d6ae RK |
29 | videoFileMetadataFormat: FileMetadata |
30 | videoFileMetadataVideoStream: FileMetadata | undefined | |
31 | videoFileMetadataAudioStream: FileMetadata | undefined | |
3a0fb65c | 32 | |
cf3c3624 | 33 | isAdvancedCustomizationCollapsed = true |
34 | ||
8ba9c205 RK |
35 | type: DownloadType = 'video' |
36 | ||
3545e72c C |
37 | videoFileToken: string |
38 | ||
262f8ff6 C |
39 | private activeModal: NgbModalRef |
40 | ||
8319d6ae RK |
41 | private bytesPipe: BytesPipe |
42 | private numbersPipe: NumberFormatterPipe | |
43 | ||
262f8ff6 C |
44 | private video: VideoDetails |
45 | private videoCaptions: VideoCaption[] | |
46 | ||
bb5d7428 | 47 | constructor ( |
68018040 | 48 | @Inject(LOCALE_ID) private localeId: string, |
bb5d7428 | 49 | private modalService: NgbModal, |
8319d6ae | 50 | private videoService: VideoService, |
3545e72c | 51 | private videoFileTokenService: VideoFileTokenService, |
4bc45da3 | 52 | private hooks: HooksService |
8319d6ae RK |
53 | ) { |
54 | this.bytesPipe = new BytesPipe() | |
68018040 | 55 | this.numbersPipe = new NumberFormatterPipe(this.localeId) |
8319d6ae | 56 | } |
cf02fbfb | 57 | |
8ba9c205 RK |
58 | get typeText () { |
59 | return this.type === 'video' | |
66357162 C |
60 | ? $localize`video` |
61 | : $localize`subtitles` | |
8ba9c205 RK |
62 | } |
63 | ||
5a71acd2 C |
64 | getVideoFiles () { |
65 | if (!this.video) return [] | |
66 | ||
67 | return this.video.getFiles() | |
68 | } | |
69 | ||
262f8ff6 C |
70 | getCaptions () { |
71 | if (!this.videoCaptions) return [] | |
72 | ||
73 | return this.videoCaptions | |
74 | } | |
75 | ||
8ba9c205 | 76 | show (video: VideoDetails, videoCaptions?: VideoCaption[]) { |
3545e72c C |
77 | this.videoFileToken = undefined |
78 | ||
3a0fb65c | 79 | this.video = video |
262f8ff6 | 80 | this.videoCaptions = videoCaptions |
3a0fb65c | 81 | |
24e7916c | 82 | this.activeModal = this.modalService.open(this.modal, { centered: true }) |
3a0fb65c | 83 | |
262f8ff6 C |
84 | if (this.hasFiles()) { |
85 | this.onResolutionIdChange(this.getVideoFiles()[0].resolution.id) | |
86 | } | |
4bc45da3 | 87 | |
262f8ff6 C |
88 | if (this.hasCaptions()) { |
89 | this.subtitleLanguageId = this.videoCaptions[0].language.id | |
90 | } | |
4bc45da3 | 91 | |
3545e72c C |
92 | if (videoRequiresAuth(this.video)) { |
93 | this.videoFileTokenService.getVideoFileToken(this.video.uuid) | |
94 | .subscribe(({ token }) => this.videoFileToken = token) | |
95 | } | |
96 | ||
4bc45da3 C |
97 | this.activeModal.shown.subscribe(() => { |
98 | this.hooks.runAction('action:modal.video-download.shown', 'common') | |
99 | }) | |
5f0805d3 C |
100 | } |
101 | ||
3a0fb65c C |
102 | onClose () { |
103 | this.video = undefined | |
8ba9c205 | 104 | this.videoCaptions = undefined |
cf02fbfb | 105 | } |
5f0805d3 C |
106 | |
107 | download () { | |
bb5d7428 | 108 | window.location.assign(this.getLink()) |
262f8ff6 | 109 | |
11b3f14c | 110 | this.activeModal.close() |
bb5d7428 RK |
111 | } |
112 | ||
113 | getLink () { | |
8ba9c205 | 114 | return this.type === 'subtitles' && this.videoCaptions |
262f8ff6 | 115 | ? this.getCaptionLink() |
8319d6ae | 116 | : this.getVideoFileLink() |
8ba9c205 RK |
117 | } |
118 | ||
cf3c3624 | 119 | async onResolutionIdChange (resolutionId: number) { |
120 | this.resolutionId = resolutionId | |
8319d6ae | 121 | |
262f8ff6 C |
122 | const videoFile = this.getVideoFile() |
123 | ||
124 | if (!videoFile.metadata) { | |
125 | if (!videoFile.metadataUrl) return | |
cf3c3624 | 126 | |
262f8ff6 | 127 | await this.hydrateMetadataFromMetadataUrl(videoFile) |
cf3c3624 | 128 | } |
8319d6ae | 129 | |
262f8ff6 C |
130 | if (!videoFile.metadata) return |
131 | ||
132 | this.videoFileMetadataFormat = videoFile | |
133 | ? this.getMetadataFormat(videoFile.metadata.format) | |
8319d6ae | 134 | : undefined |
262f8ff6 C |
135 | this.videoFileMetadataVideoStream = videoFile |
136 | ? this.getMetadataStream(videoFile.metadata.streams, 'video') | |
8319d6ae | 137 | : undefined |
262f8ff6 C |
138 | this.videoFileMetadataAudioStream = videoFile |
139 | ? this.getMetadataStream(videoFile.metadata.streams, 'audio') | |
8319d6ae RK |
140 | : undefined |
141 | } | |
142 | ||
262f8ff6 C |
143 | onSubtitleIdChange (subtitleId: string) { |
144 | this.subtitleLanguageId = subtitleId | |
145 | } | |
146 | ||
147 | hasFiles () { | |
148 | return this.getVideoFiles().length !== 0 | |
149 | } | |
150 | ||
8319d6ae | 151 | getVideoFile () { |
262f8ff6 C |
152 | const file = this.getVideoFiles() |
153 | .find(f => f.resolution.id === this.resolutionId) | |
154 | ||
5f0805d3 | 155 | if (!file) { |
42b40636 | 156 | logger.error(`Could not find file with resolution ${this.resolutionId}`) |
262f8ff6 | 157 | return undefined |
5f0805d3 | 158 | } |
262f8ff6 | 159 | |
8319d6ae RK |
160 | return file |
161 | } | |
162 | ||
163 | getVideoFileLink () { | |
262f8ff6 C |
164 | const file = this.getVideoFile() |
165 | if (!file) return '' | |
5f0805d3 | 166 | |
51294901 | 167 | const suffix = this.isConfidentialVideo() |
3545e72c | 168 | ? '?videoFileToken=' + this.videoFileToken |
eccf70f0 C |
169 | : '' |
170 | ||
3a0fb65c C |
171 | switch (this.downloadType) { |
172 | case 'direct': | |
eccf70f0 | 173 | return file.fileDownloadUrl + suffix |
3a0fb65c C |
174 | |
175 | case 'torrent': | |
eccf70f0 | 176 | return file.torrentDownloadUrl + suffix |
3a0fb65c | 177 | } |
bb5d7428 RK |
178 | } |
179 | ||
262f8ff6 C |
180 | hasCaptions () { |
181 | return this.getCaptions().length !== 0 | |
182 | } | |
183 | ||
184 | getCaption () { | |
185 | const caption = this.getCaptions() | |
186 | .find(c => c.language.id === this.subtitleLanguageId) | |
187 | ||
188 | if (!caption) { | |
42b40636 | 189 | logger.error(`Cannot find caption ${this.subtitleLanguageId}`) |
262f8ff6 C |
190 | return undefined |
191 | } | |
192 | ||
193 | return caption | |
51294901 C |
194 | } |
195 | ||
262f8ff6 C |
196 | getCaptionLink () { |
197 | const caption = this.getCaption() | |
198 | if (!caption) return '' | |
199 | ||
200 | return window.location.origin + caption.captionPath | |
201 | } | |
202 | ||
203 | isConfidentialVideo () { | |
204 | return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL | |
8ba9c205 RK |
205 | } |
206 | ||
8ba9c205 RK |
207 | switchToType (type: DownloadType) { |
208 | this.type = type | |
209 | } | |
8319d6ae | 210 | |
262f8ff6 C |
211 | getFileMetadata () { |
212 | const file = this.getVideoFile() | |
213 | if (!file) return undefined | |
214 | ||
215 | return file.metadata | |
216 | } | |
217 | ||
218 | private getMetadataFormat (format: any) { | |
8319d6ae | 219 | const keyToTranslateFunction = { |
9df52d66 C |
220 | encoder: (value: string) => ({ label: $localize`Encoder`, value }), |
221 | format_long_name: (value: string) => ({ label: $localize`Format name`, value }), | |
54909304 C |
222 | size: (value: number | string) => ({ label: $localize`Size`, value: this.bytesPipe.transform(+value, 2) }), |
223 | bit_rate: (value: number | string) => ({ | |
66357162 | 224 | label: $localize`Bitrate`, |
54909304 | 225 | value: `${this.numbersPipe.transform(+value)}bps` |
8319d6ae RK |
226 | }) |
227 | } | |
228 | ||
229 | // flattening format | |
230 | const sanitizedFormat = Object.assign(format, format.tags) | |
231 | delete sanitizedFormat.tags | |
232 | ||
233 | return mapValues( | |
54909304 C |
234 | pick(sanitizedFormat, objectKeysTyped(keyToTranslateFunction)), |
235 | (val: string, key: keyof typeof keyToTranslateFunction) => keyToTranslateFunction[key](val) | |
8319d6ae RK |
236 | ) |
237 | } | |
238 | ||
262f8ff6 | 239 | private getMetadataStream (streams: any[], type: 'video' | 'audio') { |
8319d6ae RK |
240 | const stream = streams.find(s => s.codec_type === type) |
241 | if (!stream) return undefined | |
242 | ||
243 | let keyToTranslateFunction = { | |
9df52d66 C |
244 | codec_long_name: (value: string) => ({ label: $localize`Codec`, value }), |
245 | profile: (value: string) => ({ label: $localize`Profile`, value }), | |
54909304 | 246 | bit_rate: (value: number | string) => ({ |
66357162 | 247 | label: $localize`Bitrate`, |
54909304 | 248 | value: `${this.numbersPipe.transform(+value)}bps` |
8319d6ae RK |
249 | }) |
250 | } | |
251 | ||
252 | if (type === 'video') { | |
253 | keyToTranslateFunction = Object.assign(keyToTranslateFunction, { | |
54909304 | 254 | width: (value: string | number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }), |
9df52d66 C |
255 | display_aspect_ratio: (value: string) => ({ label: $localize`Aspect ratio`, value }), |
256 | avg_frame_rate: (value: string) => ({ label: $localize`Average frame rate`, value }), | |
257 | pix_fmt: (value: string) => ({ label: $localize`Pixel format`, value }) | |
8319d6ae RK |
258 | }) |
259 | } else { | |
260 | keyToTranslateFunction = Object.assign(keyToTranslateFunction, { | |
54909304 C |
261 | sample_rate: (value: string | number) => ({ label: $localize`Sample rate`, value }), |
262 | channel_layout: (value: string | number) => ({ label: $localize`Channel Layout`, value }) | |
8319d6ae RK |
263 | }) |
264 | } | |
265 | ||
266 | return mapValues( | |
267 | pick(stream, Object.keys(keyToTranslateFunction)), | |
54909304 | 268 | (val: string, key: keyof typeof keyToTranslateFunction) => keyToTranslateFunction[key](val) |
8319d6ae RK |
269 | ) |
270 | } | |
271 | ||
272 | private hydrateMetadataFromMetadataUrl (file: VideoFile) { | |
273 | const observable = this.videoService.getVideoFileMetadata(file.metadataUrl) | |
4bc45da3 | 274 | .pipe(tap(res => file.metadata = res)) |
583eb04b | 275 | |
1378c0d3 | 276 | return firstValueFrom(observable) |
8319d6ae | 277 | } |
cf02fbfb | 278 | } |