]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-video-miniature/video-download.component.ts
Reorganize client shared modules
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
1 import { FfprobeFormat, FfprobeStream } from 'fluent-ffmpeg'
2 import { mapValues, pick } from 'lodash-es'
3 import { BytesPipe } from 'ngx-pipes'
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'
10
11 type DownloadType = 'video' | 'subtitles'
12 type FileMetadata = { [key: string]: { label: string, value: string }}
13
14 @Component({
15 selector: 'my-video-download',
16 templateUrl: './video-download.component.html',
17 styleUrls: [ './video-download.component.scss' ]
18 })
19 export class VideoDownloadComponent {
20 @ViewChild('modal', { static: true }) modal: ElementRef
21
22 downloadType: 'direct' | 'torrent' = 'torrent'
23 resolutionId: number | string = -1
24 subtitleLanguageId: string
25
26 video: VideoDetails
27 videoFile: VideoFile
28 videoFileMetadataFormat: FileMetadata
29 videoFileMetadataVideoStream: FileMetadata | undefined
30 videoFileMetadataAudioStream: FileMetadata | undefined
31 videoCaptions: VideoCaption[]
32 activeModal: NgbActiveModal
33
34 type: DownloadType = 'video'
35
36 private bytesPipe: BytesPipe
37 private numbersPipe: NumberFormatterPipe
38
39 constructor (
40 private notifier: Notifier,
41 private modalService: NgbModal,
42 private videoService: VideoService,
43 private auth: AuthService,
44 private i18n: I18n
45 ) {
46 this.bytesPipe = new BytesPipe()
47 this.numbersPipe = new NumberFormatterPipe()
48 }
49
50 get typeText () {
51 return this.type === 'video'
52 ? this.i18n('video')
53 : this.i18n('subtitles')
54 }
55
56 getVideoFiles () {
57 if (!this.video) return []
58
59 return this.video.getFiles()
60 }
61
62 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
63 this.video = video
64 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
65
66 this.activeModal = this.modalService.open(this.modal, { centered: true })
67
68 this.resolutionId = this.getVideoFiles()[0].resolution.id
69 this.onResolutionIdChange()
70 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
71 }
72
73 onClose () {
74 this.video = undefined
75 this.videoCaptions = undefined
76 }
77
78 download () {
79 window.location.assign(this.getLink())
80 this.activeModal.close()
81 }
82
83 getLink () {
84 return this.type === 'subtitles' && this.videoCaptions
85 ? this.getSubtitlesLink()
86 : this.getVideoFileLink()
87 }
88
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 () {
107 // HTML select send us a string, so convert it to a number
108 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
109
110 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
111 if (!file) {
112 console.error('Could not find file with resolution %d.', this.resolutionId)
113 return
114 }
115 return file
116 }
117
118 getVideoFileLink () {
119 const file = this.videoFile
120 if (!file) return
121
122 const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
123 ? '?access_token=' + this.auth.getAccessToken()
124 : ''
125
126 switch (this.downloadType) {
127 case 'direct':
128 return file.fileDownloadUrl + suffix
129
130 case 'torrent':
131 return file.torrentDownloadUrl + suffix
132 }
133 }
134
135 getSubtitlesLink () {
136 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
137 }
138
139 activateCopiedMessage () {
140 this.notifier.success(this.i18n('Copied'))
141 }
142
143 switchToType (type: DownloadType) {
144 this.type = type
145 }
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 }
206 }