1 import { mapValues, pick } from 'lodash-es'
2 import { firstValueFrom } from 'rxjs'
3 import { tap } from 'rxjs/operators'
4 import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
5 import { AuthService, HooksService, Notifier } from '@app/core'
6 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
7 import { logger } from '@root-helpers/logger'
8 import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
9 import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
11 type DownloadType = 'video' | 'subtitles'
12 type FileMetadata = { [key: string]: { label: string, value: string }}
15 selector: 'my-video-download',
16 templateUrl: './video-download.component.html',
17 styleUrls: [ './video-download.component.scss' ]
19 export class VideoDownloadComponent {
20 @ViewChild('modal', { static: true }) modal: ElementRef
22 downloadType: 'direct' | 'torrent' = 'direct'
24 resolutionId: number | string = -1
25 subtitleLanguageId: string
27 videoFileMetadataFormat: FileMetadata
28 videoFileMetadataVideoStream: FileMetadata | undefined
29 videoFileMetadataAudioStream: FileMetadata | undefined
31 isAdvancedCustomizationCollapsed = true
33 type: DownloadType = 'video'
35 private activeModal: NgbModalRef
37 private bytesPipe: BytesPipe
38 private numbersPipe: NumberFormatterPipe
40 private video: VideoDetails
41 private videoCaptions: VideoCaption[]
44 @Inject(LOCALE_ID) private localeId: string,
45 private notifier: Notifier,
46 private modalService: NgbModal,
47 private videoService: VideoService,
48 private auth: AuthService,
49 private hooks: HooksService
51 this.bytesPipe = new BytesPipe()
52 this.numbersPipe = new NumberFormatterPipe(this.localeId)
56 return this.type === 'video'
58 : $localize`subtitles`
62 if (!this.video) return []
64 return this.video.getFiles()
68 if (!this.videoCaptions) return []
70 return this.videoCaptions
73 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
75 this.videoCaptions = videoCaptions
77 this.activeModal = this.modalService.open(this.modal, { centered: true })
79 if (this.hasFiles()) {
80 this.onResolutionIdChange(this.getVideoFiles()[0].resolution.id)
83 if (this.hasCaptions()) {
84 this.subtitleLanguageId = this.videoCaptions[0].language.id
87 this.activeModal.shown.subscribe(() => {
88 this.hooks.runAction('action:modal.video-download.shown', 'common')
93 this.video = undefined
94 this.videoCaptions = undefined
98 window.location.assign(this.getLink())
100 this.activeModal.close()
104 return this.type === 'subtitles' && this.videoCaptions
105 ? this.getCaptionLink()
106 : this.getVideoFileLink()
109 async onResolutionIdChange (resolutionId: number) {
110 this.resolutionId = resolutionId
112 const videoFile = this.getVideoFile()
114 if (!videoFile.metadata) {
115 if (!videoFile.metadataUrl) return
117 await this.hydrateMetadataFromMetadataUrl(videoFile)
120 if (!videoFile.metadata) return
122 this.videoFileMetadataFormat = videoFile
123 ? this.getMetadataFormat(videoFile.metadata.format)
125 this.videoFileMetadataVideoStream = videoFile
126 ? this.getMetadataStream(videoFile.metadata.streams, 'video')
128 this.videoFileMetadataAudioStream = videoFile
129 ? this.getMetadataStream(videoFile.metadata.streams, 'audio')
133 onSubtitleIdChange (subtitleId: string) {
134 this.subtitleLanguageId = subtitleId
138 return this.getVideoFiles().length !== 0
142 const file = this.getVideoFiles()
143 .find(f => f.resolution.id === this.resolutionId)
146 logger.error(`Could not find file with resolution ${this.resolutionId}`)
153 getVideoFileLink () {
154 const file = this.getVideoFile()
157 const suffix = this.isConfidentialVideo()
158 ? '?access_token=' + this.auth.getAccessToken()
161 switch (this.downloadType) {
163 return file.fileDownloadUrl + suffix
166 return file.torrentDownloadUrl + suffix
171 return this.getCaptions().length !== 0
175 const caption = this.getCaptions()
176 .find(c => c.language.id === this.subtitleLanguageId)
179 logger.error(`Cannot find caption ${this.subtitleLanguageId}`)
187 const caption = this.getCaption()
188 if (!caption) return ''
190 return window.location.origin + caption.captionPath
193 isConfidentialVideo () {
194 return this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
197 switchToType (type: DownloadType) {
202 const file = this.getVideoFile()
203 if (!file) return undefined
208 private getMetadataFormat (format: any) {
209 const keyToTranslateFunction = {
210 encoder: (value: string) => ({ label: $localize`Encoder`, value }),
211 format_long_name: (value: string) => ({ label: $localize`Format name`, value }),
212 size: (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
213 bit_rate: (value: number) => ({
214 label: $localize`Bitrate`,
215 value: `${this.numbersPipe.transform(value)}bps`
220 const sanitizedFormat = Object.assign(format, format.tags)
221 delete sanitizedFormat.tags
224 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
225 (val, key) => keyToTranslateFunction[key](val)
229 private getMetadataStream (streams: any[], type: 'video' | 'audio') {
230 const stream = streams.find(s => s.codec_type === type)
231 if (!stream) return undefined
233 let keyToTranslateFunction = {
234 codec_long_name: (value: string) => ({ label: $localize`Codec`, value }),
235 profile: (value: string) => ({ label: $localize`Profile`, value }),
236 bit_rate: (value: number) => ({
237 label: $localize`Bitrate`,
238 value: `${this.numbersPipe.transform(value)}bps`
242 if (type === 'video') {
243 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
244 width: (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
245 display_aspect_ratio: (value: string) => ({ label: $localize`Aspect ratio`, value }),
246 avg_frame_rate: (value: string) => ({ label: $localize`Average frame rate`, value }),
247 pix_fmt: (value: string) => ({ label: $localize`Pixel format`, value })
250 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
251 sample_rate: (value: number) => ({ label: $localize`Sample rate`, value }),
252 channel_layout: (value: number) => ({ label: $localize`Channel Layout`, value })
257 pick(stream, Object.keys(keyToTranslateFunction)),
258 (val, key) => keyToTranslateFunction[key](val)
262 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
263 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
264 .pipe(tap(res => file.metadata = res))
266 return firstValueFrom(observable)