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
73 const title = $localize`Are you sure you want to edit "${this.video.name}"?`
74 const listHTML = this.getTasksSummary().map(t => `<li>${t}</li>`).join('')
76 // eslint-disable-next-line max-len
77 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 />` +
78 $localize`As a reminder, the following tasks will be executed: <ol>${listHTML}</ol>`
80 if (await this.confirmService.confirm(confirmHTML, title) !== true) return
82 this.isRunningEdition = true
84 const tasks = this.buildTasks()
86 this.loadingBar.useRef().start()
88 return this.videoStudioService.editVideo(this.video.uuid, tasks)
91 this.notifier.success($localize`Edition tasks created.`)
93 // Don't redirect to old video version watch page that could be confusing for users
94 this.router.navigateByUrl('/my-library/videos')
98 this.loadingBar.useRef().complete()
99 this.isRunningEdition = false
100 this.notifier.error(err.message)
106 getIntroOutroTooltip () {
107 return $localize`(extensions: ${this.videoExtensions.join(', ')})`
110 getWatermarkTooltip () {
111 return $localize`(extensions: ${this.imageExtensions.join(', ')})`
115 return this.buildTasks().length === 0
119 const tasks = this.buildTasks()
121 return tasks.map(t => {
122 if (t.name === 'add-intro') {
123 return $localize`"${this.getFilename(t.options.file)}" will be added at the beginning of the video`
126 if (t.name === 'add-outro') {
127 return $localize`"${this.getFilename(t.options.file)}" will be added at the end of the video`
130 if (t.name === 'add-watermark') {
131 return $localize`"${this.getFilename(t.options.file)}" image watermark will be added to the video`
134 if (t.name === 'cut') {
135 const { start, end } = t.options
137 if (start !== undefined && end !== undefined) {
138 return $localize`Video will begin at ${secondsToTime(start)} and stop at ${secondsToTime(end)}`
141 if (start !== undefined) {
142 return $localize`Video will begin at ${secondsToTime(start)}`
145 if (end !== undefined) {
146 return $localize`Video will stop at ${secondsToTime(end)}`
154 private getFilename (obj: any) {
158 private buildTasks () {
159 const tasks: VideoStudioTask[] = []
160 const value = this.form.value
162 const cut = value['cut']
163 if (cut['start'] !== 0 || cut['end'] !== this.video.duration) {
165 const options: VideoStudioTaskCut['options'] = {}
166 if (cut['start'] !== 0) options.start = cut['start']
167 if (cut['end'] !== this.video.duration) options.end = cut['end']
175 if (value['add-intro']?.['file']) {
179 file: value['add-intro']['file']
184 if (value['add-outro']?.['file']) {
188 file: value['add-outro']['file']
193 if (value['add-watermark']?.['file']) {
195 name: 'add-watermark',
197 file: value['add-watermark']['file']