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