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 { Video, VideoDetails } from '@app/shared/shared-main'
6 import { LoadingBarService } from '@ngx-loading-bar/core'
7 import { secondsToTime } from '@shared/core-utils'
8 import { VideoEditorTask, VideoEditorTaskCut } from '@shared/models'
9 import { VideoEditorService } from '../shared'
12 selector: 'my-video-editor-edit',
13 templateUrl: './video-editor-edit.component.html',
14 styleUrls: [ './video-editor-edit.component.scss' ]
16 export class VideoEditorEditComponent 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 videoEditorService: VideoEditorService,
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.videoEditorService.editVideo(this.video.uuid, tasks)
90 this.notifier.success($localize`Video updated.`)
91 this.router.navigateByUrl(Video.buildWatchUrl(this.video))
95 this.loadingBar.useRef().complete()
96 this.isRunningEdition = false
97 this.notifier.error(err.message)
103 getIntroOutroTooltip () {
104 return $localize`(extensions: ${this.videoExtensions.join(', ')})`
107 getWatermarkTooltip () {
108 return $localize`(extensions: ${this.imageExtensions.join(', ')})`
112 return this.buildTasks().length === 0
116 const tasks = this.buildTasks()
118 return tasks.map(t => {
119 if (t.name === 'add-intro') {
120 return $localize`"${this.getFilename(t.options.file)}" will be added at the beggining of the video`
123 if (t.name === 'add-outro') {
124 return $localize`"${this.getFilename(t.options.file)}" will be added at the end of the video`
127 if (t.name === 'add-watermark') {
128 return $localize`"${this.getFilename(t.options.file)}" image watermark will be added to the video`
131 if (t.name === 'cut') {
132 const { start, end } = t.options
134 if (start !== undefined && end !== undefined) {
135 return $localize`Video will begin at ${secondsToTime(start)} and stop at ${secondsToTime(end)}`
138 if (start !== undefined) {
139 return $localize`Video will begin at ${secondsToTime(start)}`
142 if (end !== undefined) {
143 return $localize`Video will stop at ${secondsToTime(end)}`
151 private getFilename (obj: any) {
155 private buildTasks () {
156 const tasks: VideoEditorTask[] = []
157 const value = this.form.value
159 const cut = value['cut']
160 if (cut['start'] !== 0 || cut['end'] !== this.video.duration) {
162 const options: VideoEditorTaskCut['options'] = {}
163 if (cut['start'] !== 0) options.start = cut['start']
164 if (cut['end'] !== this.video.duration) options.end = cut['end']
172 if (value['add-intro']?.['file']) {
176 file: value['add-intro']['file']
181 if (value['add-outro']?.['file']) {
185 file: value['add-outro']['file']
190 if (value['add-watermark']?.['file']) {
192 name: 'add-watermark',
194 file: value['add-watermark']['file']