1 import { mapValues, pick } from 'lodash-es'
2 import { BytesPipe } from 'ngx-pipes'
3 import { Component, ElementRef, ViewChild } from '@angular/core'
4 import { AuthService, Notifier } from '@app/core'
5 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
8 import { NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
10 type DownloadType = 'video' | 'subtitles'
11 type FileMetadata = { [key: string]: { label: string, value: string }}
14 selector: 'my-video-download',
15 templateUrl: './video-download.component.html',
16 styleUrls: [ './video-download.component.scss' ]
18 export class VideoDownloadComponent {
19 @ViewChild('modal', { static: true }) modal: ElementRef
21 downloadType: 'direct' | 'torrent' = 'torrent'
22 resolutionId: number | string = -1
23 subtitleLanguageId: string
27 videoFileMetadataFormat: FileMetadata
28 videoFileMetadataVideoStream: FileMetadata | undefined
29 videoFileMetadataAudioStream: FileMetadata | undefined
30 videoCaptions: VideoCaption[]
31 activeModal: NgbActiveModal
33 type: DownloadType = 'video'
35 private bytesPipe: BytesPipe
36 private numbersPipe: NumberFormatterPipe
39 private notifier: Notifier,
40 private modalService: NgbModal,
41 private videoService: VideoService,
42 private auth: AuthService,
45 this.bytesPipe = new BytesPipe()
46 this.numbersPipe = new NumberFormatterPipe()
50 return this.type === 'video'
52 : this.i18n('subtitles')
56 if (!this.video) return []
58 return this.video.getFiles()
61 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
63 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
65 this.activeModal = this.modalService.open(this.modal, { centered: true })
67 this.resolutionId = this.getVideoFiles()[0].resolution.id
68 this.onResolutionIdChange()
69 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
73 this.video = undefined
74 this.videoCaptions = undefined
78 window.location.assign(this.getLink())
79 this.activeModal.close()
83 return this.type === 'subtitles' && this.videoCaptions
84 ? this.getSubtitlesLink()
85 : this.getVideoFileLink()
88 async onResolutionIdChange () {
89 this.videoFile = this.getVideoFile()
90 if (this.videoFile.metadata || !this.videoFile.metadataUrl) return
92 await this.hydrateMetadataFromMetadataUrl(this.videoFile)
94 this.videoFileMetadataFormat = this.videoFile
95 ? this.getMetadataFormat(this.videoFile.metadata.format)
97 this.videoFileMetadataVideoStream = this.videoFile
98 ? this.getMetadataStream(this.videoFile.metadata.streams, 'video')
100 this.videoFileMetadataAudioStream = this.videoFile
101 ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio')
106 // HTML select send us a string, so convert it to a number
107 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
109 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
111 console.error('Could not find file with resolution %d.', this.resolutionId)
117 getVideoFileLink () {
118 const file = this.videoFile
121 const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
122 ? '?access_token=' + this.auth.getAccessToken()
125 switch (this.downloadType) {
127 return file.fileDownloadUrl + suffix
130 return file.torrentDownloadUrl + suffix
134 getSubtitlesLink () {
135 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
138 activateCopiedMessage () {
139 this.notifier.success(this.i18n('Copied'))
142 switchToType (type: DownloadType) {
146 getMetadataFormat (format: any) {
147 const keyToTranslateFunction = {
148 'encoder': (value: string) => ({ label: this.i18n('Encoder'), value }),
149 'format_long_name': (value: string) => ({ label: this.i18n('Format name'), value }),
150 'size': (value: number) => ({ label: this.i18n('Size'), value: this.bytesPipe.transform(value, 2) }),
151 'bit_rate': (value: number) => ({
152 label: this.i18n('Bitrate'),
153 value: `${this.numbersPipe.transform(value)}bps`
158 const sanitizedFormat = Object.assign(format, format.tags)
159 delete sanitizedFormat.tags
162 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
163 (val, key) => keyToTranslateFunction[key](val)
167 getMetadataStream (streams: any[], type: 'video' | 'audio') {
168 const stream = streams.find(s => s.codec_type === type)
169 if (!stream) return undefined
171 let keyToTranslateFunction = {
172 'codec_long_name': (value: string) => ({ label: this.i18n('Codec'), value }),
173 'profile': (value: string) => ({ label: this.i18n('Profile'), value }),
174 'bit_rate': (value: number) => ({
175 label: this.i18n('Bitrate'),
176 value: `${this.numbersPipe.transform(value)}bps`
180 if (type === 'video') {
181 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
182 'width': (value: number) => ({ label: this.i18n('Resolution'), value: `${value}x${stream.height}` }),
183 'display_aspect_ratio': (value: string) => ({ label: this.i18n('Aspect ratio'), value }),
184 'avg_frame_rate': (value: string) => ({ label: this.i18n('Average frame rate'), value }),
185 'pix_fmt': (value: string) => ({ label: this.i18n('Pixel format'), value })
188 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
189 'sample_rate': (value: number) => ({ label: this.i18n('Sample rate'), value }),
190 'channel_layout': (value: number) => ({ label: this.i18n('Channel Layout'), value })
195 pick(stream, Object.keys(keyToTranslateFunction)),
196 (val, key) => keyToTranslateFunction[key](val)
200 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
201 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
202 observable.subscribe(res => file.metadata = res)
204 return observable.toPromise()