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