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'
8 type DownloadType = 'video' | 'subtitles'
9 type FileMetadata = { [key: string]: { label: string, value: string }}
12 selector: 'my-video-download',
13 templateUrl: './video-download.component.html',
14 styleUrls: [ './video-download.component.scss' ]
16 export class VideoDownloadComponent {
17 @ViewChild('modal', { static: true }) modal: ElementRef
19 downloadType: 'direct' | 'torrent' = 'torrent'
20 resolutionId: number | string = -1
21 subtitleLanguageId: string
25 videoFileMetadataFormat: FileMetadata
26 videoFileMetadataVideoStream: FileMetadata | undefined
27 videoFileMetadataAudioStream: FileMetadata | undefined
28 videoCaptions: VideoCaption[]
29 activeModal: NgbActiveModal
31 type: DownloadType = 'video'
33 private bytesPipe: BytesPipe
34 private numbersPipe: NumberFormatterPipe
37 @Inject(LOCALE_ID) private localeId: string,
38 private notifier: Notifier,
39 private modalService: NgbModal,
40 private videoService: VideoService,
41 private auth: AuthService
43 this.bytesPipe = new BytesPipe()
44 this.numbersPipe = new NumberFormatterPipe(this.localeId)
48 return this.type === 'video'
50 : $localize`subtitles`
54 if (!this.video) return []
56 return this.video.getFiles()
59 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
61 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
63 this.activeModal = this.modalService.open(this.modal, { centered: true })
65 this.resolutionId = this.getVideoFiles()[0].resolution.id
66 this.onResolutionIdChange()
67 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
71 this.video = undefined
72 this.videoCaptions = undefined
76 window.location.assign(this.getLink())
77 this.activeModal.close()
81 return this.type === 'subtitles' && this.videoCaptions
82 ? this.getSubtitlesLink()
83 : this.getVideoFileLink()
86 async onResolutionIdChange () {
87 this.videoFile = this.getVideoFile()
88 if (this.videoFile.metadata || !this.videoFile.metadataUrl) return
90 await this.hydrateMetadataFromMetadataUrl(this.videoFile)
92 this.videoFileMetadataFormat = this.videoFile
93 ? this.getMetadataFormat(this.videoFile.metadata.format)
95 this.videoFileMetadataVideoStream = this.videoFile
96 ? this.getMetadataStream(this.videoFile.metadata.streams, 'video')
98 this.videoFileMetadataAudioStream = this.videoFile
99 ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio')
104 // HTML select send us a string, so convert it to a number
105 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
107 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
109 console.error('Could not find file with resolution %d.', this.resolutionId)
115 getVideoFileLink () {
116 const file = this.videoFile
119 const suffix = this.isConfidentialVideo()
120 ? '?access_token=' + this.auth.getAccessToken()
123 switch (this.downloadType) {
125 return file.fileDownloadUrl + suffix
128 return file.torrentDownloadUrl + suffix
132 isConfidentialVideo () {
133 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
136 getSubtitlesLink () {
137 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
140 activateCopiedMessage () {
141 this.notifier.success($localize`Copied`)
144 switchToType (type: DownloadType) {
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`
160 const sanitizedFormat = Object.assign(format, format.tags)
161 delete sanitizedFormat.tags
164 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
165 (val, key) => keyToTranslateFunction[key](val)
169 getMetadataStream (streams: any[], type: 'video' | 'audio') {
170 const stream = streams.find(s => s.codec_type === type)
171 if (!stream) return undefined
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`
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 })
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 })
197 pick(stream, Object.keys(keyToTranslateFunction)),
198 (val, key) => keyToTranslateFunction[key](val)
202 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
203 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
204 observable.subscribe(res => file.metadata = res)
206 return observable.toPromise()