]>
Commit | Line | Data |
---|---|---|
8319d6ae | 1 | import { mapValues, pick } from 'lodash-es' |
4bc45da3 C |
2 | import { pipe } from 'rxjs' |
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 | |
7c51916a | 21 | downloadType: 'direct' | 'torrent' = 'torrent' |
09700934 | 22 | resolutionId: number | string = -1 |
8ba9c205 | 23 | subtitleLanguageId: string |
5f0805d3 | 24 | |
8dfceec4 | 25 | video: VideoDetails |
8319d6ae RK |
26 | videoFile: VideoFile |
27 | videoFileMetadataFormat: FileMetadata | |
28 | videoFileMetadataVideoStream: FileMetadata | undefined | |
29 | videoFileMetadataAudioStream: FileMetadata | undefined | |
8ba9c205 | 30 | videoCaptions: VideoCaption[] |
4bc45da3 | 31 | activeModal: NgbModalRef |
3a0fb65c | 32 | |
8ba9c205 RK |
33 | type: DownloadType = 'video' |
34 | ||
8319d6ae RK |
35 | private bytesPipe: BytesPipe |
36 | private numbersPipe: NumberFormatterPipe | |
37 | ||
bb5d7428 | 38 | constructor ( |
68018040 | 39 | @Inject(LOCALE_ID) private localeId: string, |
f8b2c1b4 | 40 | private notifier: Notifier, |
bb5d7428 | 41 | private modalService: NgbModal, |
8319d6ae | 42 | private videoService: VideoService, |
4bc45da3 C |
43 | private auth: AuthService, |
44 | private hooks: HooksService | |
8319d6ae RK |
45 | ) { |
46 | this.bytesPipe = new BytesPipe() | |
68018040 | 47 | this.numbersPipe = new NumberFormatterPipe(this.localeId) |
8319d6ae | 48 | } |
cf02fbfb | 49 | |
8ba9c205 RK |
50 | get typeText () { |
51 | return this.type === 'video' | |
66357162 C |
52 | ? $localize`video` |
53 | : $localize`subtitles` | |
8ba9c205 RK |
54 | } |
55 | ||
5a71acd2 C |
56 | getVideoFiles () { |
57 | if (!this.video) return [] | |
58 | ||
59 | return this.video.getFiles() | |
60 | } | |
61 | ||
8ba9c205 | 62 | show (video: VideoDetails, videoCaptions?: VideoCaption[]) { |
3a0fb65c | 63 | this.video = video |
8ba9c205 | 64 | this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined |
3a0fb65c | 65 | |
24e7916c | 66 | this.activeModal = this.modalService.open(this.modal, { centered: true }) |
3a0fb65c | 67 | |
5a71acd2 | 68 | this.resolutionId = this.getVideoFiles()[0].resolution.id |
8319d6ae | 69 | this.onResolutionIdChange() |
4bc45da3 | 70 | |
8ba9c205 | 71 | if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id |
4bc45da3 C |
72 | |
73 | this.activeModal.shown.subscribe(() => { | |
74 | this.hooks.runAction('action:modal.video-download.shown', 'common') | |
75 | }) | |
5f0805d3 C |
76 | } |
77 | ||
3a0fb65c C |
78 | onClose () { |
79 | this.video = undefined | |
8ba9c205 | 80 | this.videoCaptions = undefined |
cf02fbfb | 81 | } |
5f0805d3 C |
82 | |
83 | download () { | |
bb5d7428 | 84 | window.location.assign(this.getLink()) |
11b3f14c | 85 | this.activeModal.close() |
bb5d7428 RK |
86 | } |
87 | ||
88 | getLink () { | |
8ba9c205 RK |
89 | return this.type === 'subtitles' && this.videoCaptions |
90 | ? this.getSubtitlesLink() | |
8319d6ae | 91 | : this.getVideoFileLink() |
8ba9c205 RK |
92 | } |
93 | ||
8319d6ae RK |
94 | async onResolutionIdChange () { |
95 | this.videoFile = this.getVideoFile() | |
96 | if (this.videoFile.metadata || !this.videoFile.metadataUrl) return | |
97 | ||
98 | await this.hydrateMetadataFromMetadataUrl(this.videoFile) | |
4bc45da3 | 99 | if (!this.videoFile.metadata) return |
8319d6ae RK |
100 | |
101 | this.videoFileMetadataFormat = this.videoFile | |
102 | ? this.getMetadataFormat(this.videoFile.metadata.format) | |
103 | : undefined | |
104 | this.videoFileMetadataVideoStream = this.videoFile | |
105 | ? this.getMetadataStream(this.videoFile.metadata.streams, 'video') | |
106 | : undefined | |
107 | this.videoFileMetadataAudioStream = this.videoFile | |
108 | ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio') | |
109 | : undefined | |
110 | } | |
111 | ||
112 | getVideoFile () { | |
00336945 | 113 | // HTML select send us a string, so convert it to a number |
09700934 | 114 | this.resolutionId = parseInt(this.resolutionId.toString(), 10) |
00336945 | 115 | |
5a71acd2 | 116 | const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId) |
5f0805d3 | 117 | if (!file) { |
09700934 | 118 | console.error('Could not find file with resolution %d.', this.resolutionId) |
5f0805d3 C |
119 | return |
120 | } | |
8319d6ae RK |
121 | return file |
122 | } | |
123 | ||
124 | getVideoFileLink () { | |
125 | const file = this.videoFile | |
126 | if (!file) return | |
5f0805d3 | 127 | |
51294901 | 128 | const suffix = this.isConfidentialVideo() |
eccf70f0 C |
129 | ? '?access_token=' + this.auth.getAccessToken() |
130 | : '' | |
131 | ||
3a0fb65c C |
132 | switch (this.downloadType) { |
133 | case 'direct': | |
eccf70f0 | 134 | return file.fileDownloadUrl + suffix |
3a0fb65c C |
135 | |
136 | case 'torrent': | |
eccf70f0 | 137 | return file.torrentDownloadUrl + suffix |
3a0fb65c | 138 | } |
bb5d7428 RK |
139 | } |
140 | ||
51294901 C |
141 | isConfidentialVideo () { |
142 | return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL | |
143 | } | |
144 | ||
8ba9c205 RK |
145 | getSubtitlesLink () { |
146 | return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath | |
147 | } | |
148 | ||
bb5d7428 | 149 | activateCopiedMessage () { |
66357162 | 150 | this.notifier.success($localize`Copied`) |
5f0805d3 | 151 | } |
8ba9c205 RK |
152 | |
153 | switchToType (type: DownloadType) { | |
154 | this.type = type | |
155 | } | |
8319d6ae | 156 | |
583eb04b | 157 | getMetadataFormat (format: any) { |
8319d6ae | 158 | const keyToTranslateFunction = { |
66357162 C |
159 | 'encoder': (value: string) => ({ label: $localize`Encoder`, value }), |
160 | 'format_long_name': (value: string) => ({ label: $localize`Format name`, value }), | |
161 | 'size': (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }), | |
8319d6ae | 162 | 'bit_rate': (value: number) => ({ |
66357162 | 163 | label: $localize`Bitrate`, |
8319d6ae RK |
164 | value: `${this.numbersPipe.transform(value)}bps` |
165 | }) | |
166 | } | |
167 | ||
168 | // flattening format | |
169 | const sanitizedFormat = Object.assign(format, format.tags) | |
170 | delete sanitizedFormat.tags | |
171 | ||
172 | return mapValues( | |
173 | pick(sanitizedFormat, Object.keys(keyToTranslateFunction)), | |
174 | (val, key) => keyToTranslateFunction[key](val) | |
175 | ) | |
176 | } | |
177 | ||
583eb04b | 178 | getMetadataStream (streams: any[], type: 'video' | 'audio') { |
8319d6ae RK |
179 | const stream = streams.find(s => s.codec_type === type) |
180 | if (!stream) return undefined | |
181 | ||
182 | let keyToTranslateFunction = { | |
66357162 C |
183 | 'codec_long_name': (value: string) => ({ label: $localize`Codec`, value }), |
184 | 'profile': (value: string) => ({ label: $localize`Profile`, value }), | |
8319d6ae | 185 | 'bit_rate': (value: number) => ({ |
66357162 | 186 | label: $localize`Bitrate`, |
8319d6ae RK |
187 | value: `${this.numbersPipe.transform(value)}bps` |
188 | }) | |
189 | } | |
190 | ||
191 | if (type === 'video') { | |
192 | keyToTranslateFunction = Object.assign(keyToTranslateFunction, { | |
66357162 C |
193 | 'width': (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }), |
194 | 'display_aspect_ratio': (value: string) => ({ label: $localize`Aspect ratio`, value }), | |
195 | 'avg_frame_rate': (value: string) => ({ label: $localize`Average frame rate`, value }), | |
196 | 'pix_fmt': (value: string) => ({ label: $localize`Pixel format`, value }) | |
8319d6ae RK |
197 | }) |
198 | } else { | |
199 | keyToTranslateFunction = Object.assign(keyToTranslateFunction, { | |
66357162 C |
200 | 'sample_rate': (value: number) => ({ label: $localize`Sample rate`, value }), |
201 | 'channel_layout': (value: number) => ({ label: $localize`Channel Layout`, value }) | |
8319d6ae RK |
202 | }) |
203 | } | |
204 | ||
205 | return mapValues( | |
206 | pick(stream, Object.keys(keyToTranslateFunction)), | |
207 | (val, key) => keyToTranslateFunction[key](val) | |
208 | ) | |
209 | } | |
210 | ||
211 | private hydrateMetadataFromMetadataUrl (file: VideoFile) { | |
212 | const observable = this.videoService.getVideoFileMetadata(file.metadataUrl) | |
4bc45da3 | 213 | .pipe(tap(res => file.metadata = res)) |
583eb04b | 214 | |
8319d6ae RK |
215 | return observable.toPromise() |
216 | } | |
cf02fbfb | 217 | } |