1 import { Component, ElementRef, ViewChild } from '@angular/core'
2 import { VideoDetails } from '../../../shared/video/video-details.model'
3 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { AuthService, Notifier } from '@app/core'
6 import { VideoPrivacy, VideoCaption, VideoFile } from '@shared/models'
7 import { FfprobeFormat, FfprobeStream } from 'fluent-ffmpeg'
8 import { mapValues, pick } from 'lodash-es'
9 import { NumberFormatterPipe } from '@app/shared/angular/number-formatter.pipe'
10 import { BytesPipe } from 'ngx-pipes'
11 import { VideoService } from '../video.service'
13 type DownloadType = 'video' | 'subtitles'
14 type FileMetadata = { [key: string]: { label: string, value: string }}
17 selector: 'my-video-download',
18 templateUrl: './video-download.component.html',
19 styleUrls: [ './video-download.component.scss' ]
21 export class VideoDownloadComponent {
22 @ViewChild('modal', { static: true }) modal: ElementRef
24 downloadType: 'direct' | 'torrent' = 'torrent'
25 resolutionId: number | string = -1
26 subtitleLanguageId: string
30 videoFileMetadataFormat: FileMetadata
31 videoFileMetadataVideoStream: FileMetadata | undefined
32 videoFileMetadataAudioStream: FileMetadata | undefined
33 videoCaptions: VideoCaption[]
34 activeModal: NgbActiveModal
36 type: DownloadType = 'video'
38 private bytesPipe: BytesPipe
39 private numbersPipe: NumberFormatterPipe
42 private notifier: Notifier,
43 private modalService: NgbModal,
44 private videoService: VideoService,
45 private auth: AuthService,
48 this.bytesPipe = new BytesPipe()
49 this.numbersPipe = new NumberFormatterPipe()
53 return this.type === 'video'
55 : this.i18n('subtitles')
59 if (!this.video) return []
61 return this.video.getFiles()
64 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
66 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
68 this.activeModal = this.modalService.open(this.modal, { centered: true })
70 this.resolutionId = this.getVideoFiles()[0].resolution.id
71 this.onResolutionIdChange()
72 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
76 this.video = undefined
77 this.videoCaptions = undefined
81 window.location.assign(this.getLink())
82 this.activeModal.close()
86 return this.type === 'subtitles' && this.videoCaptions
87 ? this.getSubtitlesLink()
88 : this.getVideoFileLink()
91 async onResolutionIdChange () {
92 this.videoFile = this.getVideoFile()
93 if (this.videoFile.metadata || !this.videoFile.metadataUrl) return
95 await this.hydrateMetadataFromMetadataUrl(this.videoFile)
97 this.videoFileMetadataFormat = this.videoFile
98 ? this.getMetadataFormat(this.videoFile.metadata.format)
100 this.videoFileMetadataVideoStream = this.videoFile
101 ? this.getMetadataStream(this.videoFile.metadata.streams, 'video')
103 this.videoFileMetadataAudioStream = this.videoFile
104 ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio')
109 // HTML select send us a string, so convert it to a number
110 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
112 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
114 console.error('Could not find file with resolution %d.', this.resolutionId)
120 getVideoFileLink () {
121 const file = this.videoFile
124 const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
125 ? '?access_token=' + this.auth.getAccessToken()
128 switch (this.downloadType) {
130 return file.fileDownloadUrl + suffix
133 return file.torrentDownloadUrl + suffix
137 getSubtitlesLink () {
138 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
141 activateCopiedMessage () {
142 this.notifier.success(this.i18n('Copied'))
145 switchToType (type: DownloadType) {
149 getMetadataFormat (format: FfprobeFormat) {
150 const keyToTranslateFunction = {
151 'encoder': (value: string) => ({ label: this.i18n('Encoder'), value }),
152 'format_long_name': (value: string) => ({ label: this.i18n('Format name'), value }),
153 'size': (value: number) => ({ label: this.i18n('Size'), value: this.bytesPipe.transform(value, 2) }),
154 'bit_rate': (value: number) => ({
155 label: this.i18n('Bitrate'),
156 value: `${this.numbersPipe.transform(value)}bps`
161 const sanitizedFormat = Object.assign(format, format.tags)
162 delete sanitizedFormat.tags
165 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
166 (val, key) => keyToTranslateFunction[key](val)
170 getMetadataStream (streams: FfprobeStream[], type: 'video' | 'audio') {
171 const stream = streams.find(s => s.codec_type === type)
172 if (!stream) return undefined
174 let keyToTranslateFunction = {
175 'codec_long_name': (value: string) => ({ label: this.i18n('Codec'), value }),
176 'profile': (value: string) => ({ label: this.i18n('Profile'), value }),
177 'bit_rate': (value: number) => ({
178 label: this.i18n('Bitrate'),
179 value: `${this.numbersPipe.transform(value)}bps`
183 if (type === 'video') {
184 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
185 'width': (value: number) => ({ label: this.i18n('Resolution'), value: `${value}x${stream.height}` }),
186 'display_aspect_ratio': (value: string) => ({ label: this.i18n('Aspect ratio'), value }),
187 'avg_frame_rate': (value: string) => ({ label: this.i18n('Average frame rate'), value }),
188 'pix_fmt': (value: string) => ({ label: this.i18n('Pixel format'), value })
191 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
192 'sample_rate': (value: number) => ({ label: this.i18n('Sample rate'), value }),
193 'channel_layout': (value: number) => ({ label: this.i18n('Channel Layout'), value })
198 pick(stream, Object.keys(keyToTranslateFunction)),
199 (val, key) => keyToTranslateFunction[key](val)
203 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
204 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
205 observable.subscribe(res => file.metadata = res)
206 return observable.toPromise()