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