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