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