|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 The Diffusion Studio Authors |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla |
| 5 | + * Public License, v. 2.0 that can be found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Timestamp } from '../../models'; |
| 9 | +import { Serializer, serializable } from '../../services'; |
| 10 | +import { ComplexTextClip, Font } from '../../clips'; |
| 11 | + |
| 12 | +import type { SingleColorCaptionPresetConfig, CaptionPresetType, ScreenSize } from './preset.types'; |
| 13 | +import type { CaptionPresetStrategy } from './preset.interface'; |
| 14 | +import type { GeneratorOptions } from '../../models'; |
| 15 | +import type { CaptionTrack } from './caption'; |
| 16 | +import type { hex } from '../../types'; |
| 17 | + |
| 18 | +export class VerdantCaptionPreset extends Serializer implements CaptionPresetStrategy { |
| 19 | + private _initialized = false; |
| 20 | + |
| 21 | + @serializable() |
| 22 | + public generatorOptions: GeneratorOptions; |
| 23 | + |
| 24 | + @serializable() |
| 25 | + readonly type: CaptionPresetType = 'VERDANT'; |
| 26 | + |
| 27 | + @serializable() |
| 28 | + public color: hex; |
| 29 | + |
| 30 | + @serializable(ComplexTextClip) |
| 31 | + public clip: ComplexTextClip | undefined; |
| 32 | + |
| 33 | + public constructor(config: Partial<SingleColorCaptionPresetConfig> = {}) { |
| 34 | + super(); |
| 35 | + |
| 36 | + this.generatorOptions = config.generatorOptions ?? { duration: [1] }; |
| 37 | + this.clip = config.clip; |
| 38 | + this.color = config.color ?? '#69E34C'; |
| 39 | + } |
| 40 | + |
| 41 | + public async init(composition?: ScreenSize) { |
| 42 | + if (this._initialized || !composition) return; |
| 43 | + |
| 44 | + if (!this.clip) { |
| 45 | + this.clip = await new ComplexTextClip({ |
| 46 | + textAlign: 'center', |
| 47 | + textBaseline: 'middle', |
| 48 | + fontSize: 15, |
| 49 | + fillStyle: '#FFFFFF', |
| 50 | + shadow: { |
| 51 | + color: '#000000', |
| 52 | + blur: 4, |
| 53 | + alpha: 0.7, |
| 54 | + angle: Math.PI / 4, |
| 55 | + distance: 2, |
| 56 | + }, |
| 57 | + stroke: { |
| 58 | + width: 3, |
| 59 | + color: '#000000', |
| 60 | + }, |
| 61 | + maxWidth: composition.width * 0.5, |
| 62 | + leading: 1.1, |
| 63 | + font: Font.fromFamily({ family: 'Montserrat', weight: '800' }), |
| 64 | + textCase: 'upper', |
| 65 | + styles: [{ |
| 66 | + fillStyle: this.color, |
| 67 | + fontSize: 19, |
| 68 | + }], |
| 69 | + position: 'center', |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + await this.clip.font.load(); |
| 74 | + this._initialized = true; |
| 75 | + } |
| 76 | + |
| 77 | + public async applyTo(track: CaptionTrack): Promise<void> { |
| 78 | + await this.init(track.composition); |
| 79 | + |
| 80 | + if (!this.clip || !track.clip?.transcript) { |
| 81 | + throw new Error('Preset needs to be initialized first'); |
| 82 | + } |
| 83 | + |
| 84 | + const offset = track.clip?.offset ?? new Timestamp(); |
| 85 | + |
| 86 | + // add captions |
| 87 | + for (const sequence of track.clip.transcript.iter(this.generatorOptions)) { |
| 88 | + for (let i = 0; i < sequence.words.length; i++) { |
| 89 | + const tokens = sequence.words.map((s) => s.text); |
| 90 | + const clip = this.clip.copy().set({ |
| 91 | + text: tokens.join(' '), |
| 92 | + stop: sequence.words[i].stop.add(offset), |
| 93 | + start: sequence.words[i].start.add(offset), |
| 94 | + segments: [{ |
| 95 | + index: 0, |
| 96 | + start: tokens.slice(0, i).join(' ').length, |
| 97 | + stop: tokens.slice(0, i + 1).join(' ').length, |
| 98 | + }], |
| 99 | + }); |
| 100 | + await track.appendClip(clip); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments