1 import { Component, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { ConfirmService, Notifier, ServerService } from '@app/core'
4 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
5 import { VideoDetails } from '@app/shared/shared-main'
6 import { LoadingBarService } from '@ngx-loading-bar/core'
7 import { secondsToTime } from '@shared/core-utils'
8 import { VideoStudioTask, VideoStudioTaskCut } from '@shared/models'
9 import { VideoStudioService } from '../shared'
12 selector: 'my-video-studio-edit',
13 templateUrl: './video-studio-edit.component.html',
14 styleUrls: [ './video-studio-edit.component.scss' ]
16 export class VideoStudioEditComponent extends FormReactive implements OnInit {
17 isRunningEdition = false
22 protected formValidatorService: FormValidatorService,
23 private serverService: ServerService,
24 private notifier: Notifier,
25 private router: Router,
26 private route: ActivatedRoute,
27 private videoStudioService: VideoStudioService,
28 private loadingBar: LoadingBarService,
29 private confirmService: ConfirmService
35 this.video = this.route.snapshot.data.video
37 const defaultValues = {
40 end: this.video.duration
61 get videoExtensions () {
62 return this.serverService.getHTMLConfig().video.file.extensions
65 get imageExtensions () {
66 return this.serverService.getHTMLConfig().video.image.extensions
70 if (this.isRunningEdition) return
72 const title = $localize`Are you sure you want to edit "${this.video.name}"?`
73 const listHTML = this.getTasksSummary().map(t => `<li>${t}</li>`).join('')
75 // eslint-disable-next-line max-len
76 const confirmHTML = $localize`The current video will be overwritten by this edited video and <strong>you won't be able to recover it</strong>.<br /><br />` +
77 $localize`As a reminder, the following tasks will be executed: <ol>${listHTML}</ol>`
79 if (await this.confirmService.confirm(confirmHTML, title) !== true) return
81 this.isRunningEdition = true
83 const tasks = this.buildTasks()
85 this.loadingBar.useRef().start()
87 return this.videoStudioService.editVideo(this.video.uuid, tasks)
90 this.notifier.success($localize`Edition tasks created.`)
92 // Don't redirect to old video version watch page that could be confusing for users
93 this.router.navigateByUrl('/my-library/videos')
97 this.loadingBar.useRef().complete()
98 this.isRunningEdition = false
99 this.notifier.error(err.message)
105 getIntroOutroTooltip () {
106 return $localize`(extensions: ${this.videoExtensions.join(', ')})`
109 getWatermarkTooltip () {
110 return $localize`(extensions: ${this.imageExtensions.join(', ')})`
114 return this.buildTasks().length === 0
118 const tasks = this.buildTasks()
120 return tasks.map(t => {
121 if (t.name === 'add-intro') {
122 return $localize`"${this.getFilename(t.options.file)}" will be added at the beginning of the video`
125 if (t.name === 'add-outro') {
126 return $localize`"${this.getFilename(t.options.file)}" will be added at the end of the video`
129 if (t.name === 'add-watermark') {
130 return $localize`"${this.getFilename(t.options.file)}" image watermark will be added to the video`
133 if (t.name === 'cut') {
134 const { start, end } = t.options
136 if (start !== undefined && end !== undefined) {
137 return $localize`Video will begin at ${secondsToTime(start)} and stop at ${secondsToTime(end)}`
140 if (start !== undefined) {
141 return $localize`Video will begin at ${secondsToTime(start)}`
144 if (end !== undefined) {
145 return $localize`Video will stop at ${secondsToTime(end)}`
153 private getFilename (obj: any) {
157 private buildTasks () {
158 const tasks: VideoStudioTask[] = []
159 const value = this.form.value
161 const cut = value['cut']
162 if (cut['start'] !== 0 || cut['end'] !== this.video.duration) {
164 const options: VideoStudioTaskCut['options'] = {}
165 if (cut['start'] !== 0) options.start = cut['start']
166 if (cut['end'] !== this.video.duration) options.end = cut['end']
174 if (value['add-intro']?.['file']) {
178 file: value['add-intro']['file']
183 if (value['add-outro']?.['file']) {
187 file: value['add-outro']['file']
192 if (value['add-watermark']?.['file']) {
194 name: 'add-watermark',
196 file: value['add-watermark']['file']