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