]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-download.component.ts
Fix hls error handling
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-download.component.ts
CommitLineData
8319d6ae 1import { mapValues, pick } from 'lodash-es'
67ed6552
C
2import { Component, ElementRef, ViewChild } from '@angular/core'
3import { AuthService, Notifier } from '@app/core'
4import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'
67ed6552 5import { VideoCaption, VideoFile, VideoPrivacy } from '@shared/models'
94676e63 6import { BytesPipe, NumberFormatterPipe, VideoDetails, VideoService } from '../shared-main'
8ba9c205
RK
7
8type DownloadType = 'video' | 'subtitles'
8319d6ae 9type FileMetadata = { [key: string]: { label: string, value: string }}
cf02fbfb
C
10
11@Component({
a96aed15
C
12 selector: 'my-video-download',
13 templateUrl: './video-download.component.html',
0727cab0 14 styleUrls: [ './video-download.component.scss' ]
cf02fbfb 15})
3a0fb65c 16export class VideoDownloadComponent {
f36da21e 17 @ViewChild('modal', { static: true }) modal: ElementRef
cf02fbfb 18
7c51916a 19 downloadType: 'direct' | 'torrent' = 'torrent'
09700934 20 resolutionId: number | string = -1
8ba9c205 21 subtitleLanguageId: string
5f0805d3 22
8dfceec4 23 video: VideoDetails
8319d6ae
RK
24 videoFile: VideoFile
25 videoFileMetadataFormat: FileMetadata
26 videoFileMetadataVideoStream: FileMetadata | undefined
27 videoFileMetadataAudioStream: FileMetadata | undefined
8ba9c205 28 videoCaptions: VideoCaption[]
11b3f14c 29 activeModal: NgbActiveModal
3a0fb65c 30
8ba9c205
RK
31 type: DownloadType = 'video'
32
8319d6ae
RK
33 private bytesPipe: BytesPipe
34 private numbersPipe: NumberFormatterPipe
35
bb5d7428 36 constructor (
f8b2c1b4 37 private notifier: Notifier,
bb5d7428 38 private modalService: NgbModal,
8319d6ae 39 private videoService: VideoService,
66357162 40 private auth: AuthService
8319d6ae
RK
41 ) {
42 this.bytesPipe = new BytesPipe()
43 this.numbersPipe = new NumberFormatterPipe()
44 }
cf02fbfb 45
8ba9c205
RK
46 get typeText () {
47 return this.type === 'video'
66357162
C
48 ? $localize`video`
49 : $localize`subtitles`
8ba9c205
RK
50 }
51
5a71acd2
C
52 getVideoFiles () {
53 if (!this.video) return []
54
55 return this.video.getFiles()
56 }
57
8ba9c205 58 show (video: VideoDetails, videoCaptions?: VideoCaption[]) {
3a0fb65c 59 this.video = video
8ba9c205 60 this.videoCaptions = videoCaptions && videoCaptions.length ? videoCaptions : undefined
3a0fb65c 61
24e7916c 62 this.activeModal = this.modalService.open(this.modal, { centered: true })
3a0fb65c 63
5a71acd2 64 this.resolutionId = this.getVideoFiles()[0].resolution.id
8319d6ae 65 this.onResolutionIdChange()
8ba9c205 66 if (this.videoCaptions) this.subtitleLanguageId = this.videoCaptions[0].language.id
5f0805d3
C
67 }
68
3a0fb65c
C
69 onClose () {
70 this.video = undefined
8ba9c205 71 this.videoCaptions = undefined
cf02fbfb 72 }
5f0805d3
C
73
74 download () {
bb5d7428 75 window.location.assign(this.getLink())
11b3f14c 76 this.activeModal.close()
bb5d7428
RK
77 }
78
79 getLink () {
8ba9c205
RK
80 return this.type === 'subtitles' && this.videoCaptions
81 ? this.getSubtitlesLink()
8319d6ae 82 : this.getVideoFileLink()
8ba9c205
RK
83 }
84
8319d6ae
RK
85 async onResolutionIdChange () {
86 this.videoFile = this.getVideoFile()
87 if (this.videoFile.metadata || !this.videoFile.metadataUrl) return
88
89 await this.hydrateMetadataFromMetadataUrl(this.videoFile)
90
91 this.videoFileMetadataFormat = this.videoFile
92 ? this.getMetadataFormat(this.videoFile.metadata.format)
93 : undefined
94 this.videoFileMetadataVideoStream = this.videoFile
95 ? this.getMetadataStream(this.videoFile.metadata.streams, 'video')
96 : undefined
97 this.videoFileMetadataAudioStream = this.videoFile
98 ? this.getMetadataStream(this.videoFile.metadata.streams, 'audio')
99 : undefined
100 }
101
102 getVideoFile () {
00336945 103 // HTML select send us a string, so convert it to a number
09700934 104 this.resolutionId = parseInt(this.resolutionId.toString(), 10)
00336945 105
5a71acd2 106 const file = this.getVideoFiles().find(f => f.resolution.id === this.resolutionId)
5f0805d3 107 if (!file) {
09700934 108 console.error('Could not find file with resolution %d.', this.resolutionId)
5f0805d3
C
109 return
110 }
8319d6ae
RK
111 return file
112 }
113
114 getVideoFileLink () {
115 const file = this.videoFile
116 if (!file) return
5f0805d3 117
22a73cb8 118 const suffix = this.video.privacy.id === VideoPrivacy.PRIVATE || this.video.privacy.id === VideoPrivacy.INTERNAL
eccf70f0
C
119 ? '?access_token=' + this.auth.getAccessToken()
120 : ''
121
3a0fb65c
C
122 switch (this.downloadType) {
123 case 'direct':
eccf70f0 124 return file.fileDownloadUrl + suffix
3a0fb65c
C
125
126 case 'torrent':
eccf70f0 127 return file.torrentDownloadUrl + suffix
3a0fb65c 128 }
bb5d7428
RK
129 }
130
8ba9c205
RK
131 getSubtitlesLink () {
132 return window.location.origin + this.videoCaptions.find(caption => caption.language.id === this.subtitleLanguageId).captionPath
133 }
134
bb5d7428 135 activateCopiedMessage () {
66357162 136 this.notifier.success($localize`Copied`)
5f0805d3 137 }
8ba9c205
RK
138
139 switchToType (type: DownloadType) {
140 this.type = type
141 }
8319d6ae 142
583eb04b 143 getMetadataFormat (format: any) {
8319d6ae 144 const keyToTranslateFunction = {
66357162
C
145 'encoder': (value: string) => ({ label: $localize`Encoder`, value }),
146 'format_long_name': (value: string) => ({ label: $localize`Format name`, value }),
147 'size': (value: number) => ({ label: $localize`Size`, value: this.bytesPipe.transform(value, 2) }),
8319d6ae 148 'bit_rate': (value: number) => ({
66357162 149 label: $localize`Bitrate`,
8319d6ae
RK
150 value: `${this.numbersPipe.transform(value)}bps`
151 })
152 }
153
154 // flattening format
155 const sanitizedFormat = Object.assign(format, format.tags)
156 delete sanitizedFormat.tags
157
158 return mapValues(
159 pick(sanitizedFormat, Object.keys(keyToTranslateFunction)),
160 (val, key) => keyToTranslateFunction[key](val)
161 )
162 }
163
583eb04b 164 getMetadataStream (streams: any[], type: 'video' | 'audio') {
8319d6ae
RK
165 const stream = streams.find(s => s.codec_type === type)
166 if (!stream) return undefined
167
168 let keyToTranslateFunction = {
66357162
C
169 'codec_long_name': (value: string) => ({ label: $localize`Codec`, value }),
170 'profile': (value: string) => ({ label: $localize`Profile`, value }),
8319d6ae 171 'bit_rate': (value: number) => ({
66357162 172 label: $localize`Bitrate`,
8319d6ae
RK
173 value: `${this.numbersPipe.transform(value)}bps`
174 })
175 }
176
177 if (type === 'video') {
178 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
66357162
C
179 'width': (value: number) => ({ label: $localize`Resolution`, value: `${value}x${stream.height}` }),
180 'display_aspect_ratio': (value: string) => ({ label: $localize`Aspect ratio`, value }),
181 'avg_frame_rate': (value: string) => ({ label: $localize`Average frame rate`, value }),
182 'pix_fmt': (value: string) => ({ label: $localize`Pixel format`, value })
8319d6ae
RK
183 })
184 } else {
185 keyToTranslateFunction = Object.assign(keyToTranslateFunction, {
66357162
C
186 'sample_rate': (value: number) => ({ label: $localize`Sample rate`, value }),
187 'channel_layout': (value: number) => ({ label: $localize`Channel Layout`, value })
8319d6ae
RK
188 })
189 }
190
191 return mapValues(
192 pick(stream, Object.keys(keyToTranslateFunction)),
193 (val, key) => keyToTranslateFunction[key](val)
194 )
195 }
196
197 private hydrateMetadataFromMetadataUrl (file: VideoFile) {
198 const observable = this.videoService.getVideoFileMetadata(file.metadataUrl)
199 observable.subscribe(res => file.metadata = res)
583eb04b 200
8319d6ae
RK
201 return observable.toPromise()
202 }
cf02fbfb 203}