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