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