1 import { mapValues, pick } from 'lodash-es'
2 import { tap } from 'rxjs/operators'
3 import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
4 import { AuthService, HooksService, Notifier } from '@app/core'
5 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
6 import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
7 import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
9 type DownloadType = 'video' | 'subtitles'
10 type FileMetadata = { [key: string]: { label: string, value: string }}
13 selector: 'my-video-download',
14 templateUrl: './video-download.component.html',
15 styleUrls: [ './video-download.component.scss' ]
17 export class VideoDownloadComponent {
18 @ViewChild('modal', { static: true }) modal: ElementRef
20 downloadType: 'direct' | 'torrent' = 'direct'
22 resolutionId: number | string = -1
23 subtitleLanguageId: string
25 videoFileMetadataFormat: FileMetadata
26 videoFileMetadataVideoStream: FileMetadata | undefined
27 videoFileMetadataAudioStream: FileMetadata | undefined
29 isAdvancedCustomizationCollapsed = true
31 type: DownloadType = 'video'
33 private activeModal: NgbModalRef
35 private bytesPipe: BytesPipe
36 private numbersPipe: NumberFormatterPipe
38 private video: VideoDetails
39 private videoCaptions: VideoCaption[]
42 @Inject(LOCALE_ID) private localeId: string,
43 private notifier: Notifier,
44 private modalService: NgbModal,
45 private videoService: VideoService,
46 private auth: AuthService,
47 private hooks: HooksService
49 this.bytesPipe = new BytesPipe()
50 this.numbersPipe = new NumberFormatterPipe(this.localeId)
54 return this.type === 'video'
56 : $localize`subtitles`
60 if (!this.video) return []
62 return this.video.getFiles()
66 if (!this.videoCaptions) return []
68 return this.videoCaptions
71 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
73 this.videoCaptions = videoCaptions
75 this.activeModal = this.modalService.open(this.modal, { centered: true })
77 if (this.hasFiles()) {
78 this.onResolutionIdChange(this.getVideoFiles()[0].resolution.id)
81 if (this.hasCaptions()) {
82 this.subtitleLanguageId = this.videoCaptions[0].language.id
85 this.activeModal.shown.subscribe(() => {
86 this.hooks.runAction('action:modal.video-download.shown', 'common')
91 this.video = undefined
92 this.videoCaptions = undefined
96 window.location.assign(this.getLink())
98 this.activeModal.close()
102 return this.type === 'subtitles' && this.videoCaptions
103 ? this.getCaptionLink()
104 : this.getVideoFileLink()
107 async onResolutionIdChange (resolutionId: number) {
108 this.resolutionId = resolutionId
110 const videoFile = this.getVideoFile()
112 if (!videoFile.metadata) {
113 if (!videoFile.metadataUrl) return
115 await this.hydrateMetadataFromMetadataUrl(videoFile)
118 if (!videoFile.metadata) return
120 this.videoFileMetadataFormat = videoFile
121 ? this.getMetadataFormat(videoFile.metadata.format)
123 this.videoFileMetadataVideoStream = videoFile
124 ? this.getMetadataStream(videoFile.metadata.streams, 'video')
126 this.videoFileMetadataAudioStream = videoFile
127 ? this.getMetadataStream(videoFile.metadata.streams, 'audio')
131 onSubtitleIdChange (subtitleId: string) {
132 this.subtitleLanguageId = subtitleId
136 return this.getVideoFiles().length !== 0
140 const file = this.getVideoFiles()
141 .find(f => f.resolution.id === this.resolutionId)
144 console.error('Could not find file with resolution %d.', this.resolutionId)
151 getVideoFileLink () {
152 const file = this.getVideoFile()
155 const suffix = this.isConfidentialVideo()
156 ? '?access_token=' + this.auth.getAccessToken()
159 switch (this.downloadType) {
161 return file.fileDownloadUrl + suffix
164 return file.torrentDownloadUrl + suffix
169 return this.getCaptions().length !== 0
173 const caption = this.getCaptions()
174 .find(c => c.language.id === this.subtitleLanguageId)
177 console.error('Cannot find caption %s.', this.subtitleLanguageId)
185 const caption = this.getCaption()
186 if (!caption) return ''
188 return window.location.origin + caption.captionPath
191 isConfidentialVideo () {
192 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
195 activateCopiedMessage () {
196 this.notifier.success($localize`Copied`)
199 switchToType (type: DownloadType) {
204 const file = this.getVideoFile()
205 if (!file) return undefined
210 private getMetadataFormat (format: any) {
211 const keyToTranslateFunction = {
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) }),
215 'bit_rate': (value: number) => ({
216 label: $localize`Bitrate`,
217 value: `${this.numbersPipe.transform(value)}bps`
222 const sanitizedFormat = Object.assign(format, format.tags)
223 delete sanitizedFormat.tags
226 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
227 (val, key) => keyToTranslateFunction[key](val)
231 private getMetadataStream (streams: any[], type: 'video' | 'audio') {
232 const stream = streams.find(s => s.codec_type === type)
233 if (!stream) return undefined
235 let keyToTranslateFunction = {
236 'codec_long_name': (value: string) => ({ label: $localize`Codec`, value }),
237 'profile': (value: string) => ({ label: $localize`Profile`, value }),
238 'bit_rate': (value: number) => ({
239 label: $localize`Bitrate`,
240 value: `${this.numbersPipe.transform(value)}bps`
244 if (type === 'video') {
245 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
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 })
252 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
253 'sample_rate': (value: number) => ({ label: $localize`Sample rate`, value }),
254 'channel_layout': (value: number) => ({ label: $localize`Channel Layout`, value })
259 pick(stream, Object.keys(keyToTranslateFunction)),
260 (val, key) => keyToTranslateFunction[key](val)
264 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
265 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
266 .pipe(tap(res => file.metadata = res))
268 return observable.toPromise()