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