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, FormReactiveService } from '@app/shared/shared-forms'
5 import { VideoDetails } from '@app/shared/shared-main'
6 import { LoadingBarService } from '@ngx-loading-bar/core'
7 import { logger } from '@root-helpers/logger'
8 import { secondsToTime } from '@shared/core-utils'
9 import { VideoStudioTask, VideoStudioTaskCut } from '@shared/models'
10 import { VideoStudioService } from '../shared'
13 selector: 'my-video-studio-edit',
14 templateUrl: './video-studio-edit.component.html',
15 styleUrls: [ './video-studio-edit.component.scss' ]
17 export class VideoStudioEditComponent extends FormReactive implements OnInit {
18 isRunningEdition = false
23 protected formReactiveService: FormReactiveService,
24 private serverService: ServerService,
25 private notifier: Notifier,
26 private router: Router,
27 private route: ActivatedRoute,
28 private videoStudioService: VideoStudioService,
29 private loadingBar: LoadingBarService,
30 private confirmService: ConfirmService
36 this.video = this.route.snapshot.data.video
38 const defaultValues = {
41 end: this.video.duration
62 get videoExtensions () {
63 return this.serverService.getHTMLConfig().video.file.extensions
66 get imageExtensions () {
67 return this.serverService.getHTMLConfig().video.image.extensions
71 if (this.isRunningEdition) return
72 if (!this.form.valid) return
73 if (this.noEdition()) return
75 const title = $localize`Are you sure you want to edit "${this.video.name}"?`
76 const listHTML = this.getTasksSummary().map(t => `<li>${t}</li>`).join('')
78 // eslint-disable-next-line max-len
79 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 />` +
80 $localize`As a reminder, the following tasks will be executed: <ol>${listHTML}</ol>`
82 if (await this.confirmService.confirm(confirmHTML, title) !== true) return
84 this.isRunningEdition = true
86 const tasks = this.buildTasks()
88 this.loadingBar.useRef().start()
90 return this.videoStudioService.editVideo(this.video.uuid, tasks)
93 this.notifier.success($localize`Edition tasks created.`)
95 // Don't redirect to old video version watch page that could be confusing for users
96 this.router.navigateByUrl('/my-library/videos')
100 this.loadingBar.useRef().complete()
101 this.isRunningEdition = false
102 this.notifier.error(err.message)
108 getIntroOutroTooltip () {
109 return $localize`(extensions: ${this.videoExtensions.join(', ')})`
112 getWatermarkTooltip () {
113 return $localize`(extensions: ${this.imageExtensions.join(', ')})`
117 return this.buildTasks().length === 0
121 const tasks = this.buildTasks()
123 return tasks.map(t => {
124 if (t.name === 'add-intro') {
125 return $localize`"${this.getFilename(t.options.file)}" will be added at the beginning of the video`
128 if (t.name === 'add-outro') {
129 return $localize`"${this.getFilename(t.options.file)}" will be added at the end of the video`
132 if (t.name === 'add-watermark') {
133 return $localize`"${this.getFilename(t.options.file)}" image watermark will be added to the video`
136 if (t.name === 'cut') {
137 const { start, end } = t.options
139 if (start !== undefined && end !== undefined) {
140 return $localize`Video will begin at ${secondsToTime(start)} and stop at ${secondsToTime(end)}`
143 if (start !== undefined) {
144 return $localize`Video will begin at ${secondsToTime(start)}`
147 if (end !== undefined) {
148 return $localize`Video will stop at ${secondsToTime(end)}`
156 private getFilename (obj: any) {
160 private buildTasks () {
161 const tasks: VideoStudioTask[] = []
162 const value = this.form.value
164 const cut = value['cut']
165 if (cut['start'] !== 0 || cut['end'] !== this.video.duration) {
167 const options: VideoStudioTaskCut['options'] = {}
168 if (cut['start'] !== 0) options.start = cut['start']
169 if (cut['end'] !== this.video.duration) options.end = cut['end']
177 if (value['add-intro']?.['file']) {
181 file: value['add-intro']['file']
186 if (value['add-outro']?.['file']) {
190 file: value['add-outro']['file']
195 if (value['add-watermark']?.['file']) {
197 name: 'add-watermark',
199 file: value['add-watermark']['file']