]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-download.component.ts
Fix instance accordion line height
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
CommitLineData
8319d6ae 1import { mapValues, pick } from 'lodash-es'
1378c0d3 2import { firstValueFrom } from 'rxjs'
4bc45da3 3import { tap } from 'rxjs/operators'
68018040 4import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
3545e72c 5import { HooksService } from '@app/core'
4bc45da3 6import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
42b40636 7import { logger } from '@root-helpers/logger'
3545e72c 8import { videoRequiresAuth } from '@root-helpers/video'
67ed6552 9import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
3545e72c 10import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoFileTokenService, VideoService } from '../shared-main'
8ba9c205
RK
11
12type DownloadType = 'video' | 'subtitles'
7dcd7d81 13type FileMetadata = { [key: string]: { label: string, value: string } }
cf02fbfb
C
14
15@Component({
a96aed15
C
16 selector: 'my-video-download',
17 templateUrl: './video-download.component.html',
0727cab0 18 styleUrls: [ './video-download.component.scss' ]
cf02fbfb 19})
3a0fb65c 20export class VideoDownloadComponent {
f36da21e 21 @ViewChild('modal', { static: true }) modal: ElementRef
cf02fbfb 22
7bd455cb 23 downloadType: 'direct' | 'torrent' = 'direct'
262f8ff6 24
09700934 25 resolutionId: number | string = -1
8ba9c205 26 subtitleLanguageId: string
5f0805d3 27
8319d6ae
RK
28 videoFileMetadataFormat: FileMetadata
29 videoFileMetadataVideoStream: FileMetadata | undefined
30 videoFileMetadataAudioStream: FileMetadata | undefined
3a0fb65c 31
cf3c3624 32 isAdvancedCustomizationCollapsed = true
33
8ba9c205
RK
34 type: DownloadType = 'video'
35
3545e72c
C
36 videoFileToken: string
37
262f8ff6
C
38 private activeModal: NgbModalRef
39
8319d6ae
RK
40 private bytesPipe: BytesPipe
41 private numbersPipe: NumberFormatterPipe
42
262f8ff6
C
43 private video: VideoDetails
44 private videoCaptions: VideoCaption[]
45
bb5d7428 46 constructor (
68018040 47 @Inject(LOCALE_ID) private localeId: string,
bb5d7428 48 private modalService: NgbModal,
8319d6ae 49 private videoService: VideoService,
3545e72c 50 private videoFileTokenService: VideoFileTokenService,
4bc45da3 51 private hooks: HooksService
8319d6ae
RK
52 ) {
53 this.bytesPipe = new BytesPipe()
68018040 54 this.numbersPipe = new NumberFormatterPipe(this.localeId)
8319d6ae 55 }
cf02fbfb 56
8ba9c205
RK
57 get typeText () {
58 return this.type === 'video'
66357162
C
59 ? $localize`video`
60 : $localize`subtitles`
8ba9c205
RK
61 }
62
5a71acd2
C
63 getVideoFiles () {
64 if (!this.video) return []
65
66 return this.video.getFiles()
67 }
68
262f8ff6
C
69 getCaptions () {
70 if (!this.videoCaptions) return []
71
72 return this.videoCaptions
73 }
74
8ba9c205 75 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
3545e72c
C
76 this.videoFileToken = undefined
77
3a0fb65c 78 this.video = video
262f8ff6 79 this.videoCaptions = videoCaptions
3a0fb65c 80
24e7916c 81 this.activeModal = this.modalService.open(this.modal, { centered: true })
3a0fb65c 82
262f8ff6
C
83 if (this.hasFiles()) {
84 this.onResolutionIdChange(this.getVideoFiles()[0].resolution.id)
85 }
4bc45da3 86
262f8ff6
C
87 if (this.hasCaptions()) {
88 this.subtitleLanguageId = this.videoCaptions[0].language.id
89 }
4bc45da3 90
3545e72c
C
91 if (videoRequiresAuth(this.video)) {
92 this.videoFileTokenService.getVideoFileToken(this.video.uuid)
93 .subscribe(({ token }) => this.videoFileToken = token)
94 }
95
4bc45da3
C
96 this.activeModal.shown.subscribe(() => {
97 this.hooks.runAction('action:modal.video-download.shown', 'common')
98 })
5f0805d3
C
99 }
100
3a0fb65c
C
101 onClose () {
102 this.video = undefined
8ba9c205 103 this.videoCaptions = undefined
cf02fbfb 104 }
5f0805d3
C
105
106 download () {
bb5d7428 107 window.location.assign(this.getLink())
262f8ff6 108
11b3f14c 109 this.activeModal.close()
bb5d7428
RK
110 }
111
112 getLink () {
8ba9c205 113 return this.type === 'subtitles' && this.videoCaptions
262f8ff6 114 ? this.getCaptionLink()
8319d6ae 115 : this.getVideoFileLink()
8ba9c205
RK
116 }
117
cf3c3624 118 async onResolutionIdChange (resolutionId: number) {
119 this.resolutionId = resolutionId
8319d6ae 120
262f8ff6
C
121 const videoFile = this.getVideoFile()
122
123 if (!videoFile.metadata) {
124 if (!videoFile.metadataUrl) return
cf3c3624 125
262f8ff6 126 await this.hydrateMetadataFromMetadataUrl(videoFile)
cf3c3624 127 }
8319d6ae 128
262f8ff6
C
129 if (!videoFile.metadata) return
130
131 this.videoFileMetadataFormat = videoFile
132 ? this.getMetadataFormat(videoFile.metadata.format)
8319d6ae 133 : undefined
262f8ff6
C
134 this.videoFileMetadataVideoStream = videoFile
135 ? this.getMetadataStream(videoFile.metadata.streams, 'video')
8319d6ae 136 : undefined
262f8ff6
C
137 this.videoFileMetadataAudioStream = videoFile
138 ? this.getMetadataStream(videoFile.metadata.streams, 'audio')
8319d6ae
RK
139 : undefined
140 }
141
262f8ff6
C
142 onSubtitleIdChange (subtitleId: string) {
143 this.subtitleLanguageId = subtitleId
144 }
145
146 hasFiles () {
147 return this.getVideoFiles().length !== 0
148 }
149
8319d6ae 150 getVideoFile () {
262f8ff6
C
151 const file = this.getVideoFiles()
152 .find(f => f.resolution.id === this.resolutionId)
153
5f0805d3 154 if (!file) {
42b40636 155 logger.error(`Could not find file with resolution ${this.resolutionId}`)
262f8ff6 156 return undefined
5f0805d3 157 }
262f8ff6 158
8319d6ae
RK
159 return file
160 }
161
162 getVideoFileLink () {
262f8ff6
C
163 const file = this.getVideoFile()
164 if (!file) return ''
5f0805d3 165
51294901 166 const suffix = this.isConfidentialVideo()
3545e72c 167 ? '?videoFileToken=' + this.videoFileToken
eccf70f0
C
168 : ''
169
3a0fb65c
C
170 switch (this.downloadType) {
171 case 'direct':
eccf70f0 172 return file.fileDownloadUrl + suffix
3a0fb65c
C
173
174 case 'torrent':
eccf70f0 175 return file.torrentDownloadUrl + suffix
3a0fb65c 176 }
bb5d7428
RK
177 }
178
262f8ff6
C
179 hasCaptions () {
180 return this.getCaptions().length !== 0
181 }
182
183 getCaption () {
184 const caption = this.getCaptions()
185 .find(c => c.language.id === this.subtitleLanguageId)
186
187 if (!caption) {
42b40636 188 logger.error(`Cannot find caption ${this.subtitleLanguageId}`)
262f8ff6
C
189 return undefined
190 }
191
192 return caption
51294901
C
193 }
194
262f8ff6
C
195 getCaptionLink () {
196 const caption = this.getCaption()
197 if (!caption) return ''
198
199 return window.location.origin + caption.captionPath
200 }
201
202 isConfidentialVideo () {
203 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
8ba9c205
RK
204 }
205
8ba9c205
RK
206 switchToType (type: DownloadType) {
207 this.type = type
208 }
8319d6ae 209
262f8ff6
C
210 getFileMetadata () {
211 const file = this.getVideoFile()
212 if (!file) return undefined
213
214 return file.metadata
215 }
216
217 private getMetadataFormat (format: any) {
8319d6ae 218 const keyToTranslateFunction = {
9df52d66
C
219 encoder: (value: string) => ({ label: $localize`Encoder`, value }),
220 format_long_name: (value: string) => ({ label: $localize`Format name`, value }),
221 size: (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
222 bit_rate: (value: number) => ({
66357162 223 label: $localize`Bitrate`,
8319d6ae
RK
224 value: `${this.numbersPipe.transform(value)}bps`
225 })
226 }
227
228 // flattening format
229 const sanitizedFormat = Object.assign(format, format.tags)
230 delete sanitizedFormat.tags
231
232 return mapValues(
233 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
234 (val, key) => keyToTranslateFunction[key](val)
235 )
236 }
237
262f8ff6 238 private getMetadataStream (streams: any[], type: 'video' | 'audio') {
8319d6ae
RK
239 const stream = streams.find(s => s.codec_type === type)
240 if (!stream) return undefined
241
242 let keyToTranslateFunction = {
9df52d66
C
243 codec_long_name: (value: string) => ({ label: $localize`Codec`, value }),
244 profile: (value: string) => ({ label: $localize`Profile`, value }),
245 bit_rate: (value: number) => ({
66357162 246 label: $localize`Bitrate`,
8319d6ae
RK
247 value: `${this.numbersPipe.transform(value)}bps`
248 })
249 }
250
251 if (type === 'video') {
252 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
9df52d66
C
253 width: (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
254 display_aspect_ratio: (value: string) => ({ label: $localize`Aspect ratio`, value }),
255 avg_frame_rate: (value: string) => ({ label: $localize`Average frame rate`, value }),
256 pix_fmt: (value: string) => ({ label: $localize`Pixel format`, value })
8319d6ae
RK
257 })
258 } else {
259 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
9df52d66
C
260 sample_rate: (value: number) => ({ label: $localize`Sample rate`, value }),
261 channel_layout: (value: number) => ({ label: $localize`Channel Layout`, value })
8319d6ae
RK
262 })
263 }
264
265 return mapValues(
266 pick(stream, Object.keys(keyToTranslateFunction)),
267 (val, key) => keyToTranslateFunction[key](val)
268 )
269 }
270
271 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
272 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
4bc45da3 273 .pipe(tap(res => file.metadata = res))
583eb04b 274
1378c0d3 275 return firstValueFrom(observable)
8319d6ae 276 }
cf02fbfb 277}