|
| 1 | +import { Style } from "../models/lyrics/common/lyrics"; |
| 2 | +import { padStart } from "../utils"; |
| 3 | + |
| 4 | +const compileWord = (word) => { |
| 5 | + const duration = Math.round((word["endTime"] - word["startTime"]) * 100); |
| 6 | + return `{\\K${duration}}${word["word"]}`; |
| 7 | +}; |
| 8 | + |
| 9 | +const formatTime = (time) => { |
| 10 | + const hour = parseInt(String(time / 3600)); |
| 11 | + const min = parseInt(String((time - 3600 * hour) / 60)); |
| 12 | + const sec = parseInt(String(time - 3600 * hour - 60 * min)); |
| 13 | + const mil = Math.min( |
| 14 | + Math.round((time - 3600 * hour - 60 * min - sec) * 100), |
| 15 | + 99 |
| 16 | + ); |
| 17 | + return `${hour}:${padStart(min, 2)}:${padStart(sec, 2)}.${padStart(mil, 2)}`; |
| 18 | +}; |
| 19 | + |
| 20 | +const formatColor = (color) => { |
| 21 | + return color.toUpperCase().replace("#", "&H"); |
| 22 | +}; |
| 23 | + |
| 24 | +class Subtitler { |
| 25 | + ASS_STYLES = ["K1", "K2"]; |
| 26 | + |
| 27 | + style = Style.Karaoke; |
| 28 | + // TODO: These constants should be configurable. |
| 29 | + ADVANCE = 5; |
| 30 | + DELAY = 1; |
| 31 | + FONTSIZE = 24; |
| 32 | + PRIMARY_COLOR = "#000000FF"; |
| 33 | + SECONDARY_COLOR = "#00FFFFFF"; |
| 34 | + OUTLINE_COLOR = "#00000000"; |
| 35 | + BACKGROUND_COLOR = "#00000000"; |
| 36 | + BOLD = false; |
| 37 | + OUTLINE = 2; |
| 38 | + SHADOW = 0; |
| 39 | + |
| 40 | + constructor(style) { |
| 41 | + this.style = style; |
| 42 | + } |
| 43 | + |
| 44 | + compile = (lines, lyrics) => { |
| 45 | + const style = this.bestStyle(lyrics.style()); |
| 46 | + const dialogues = this.dialogues(lines, style); |
| 47 | + const header = this.header(lyrics.title()); |
| 48 | + return `${header}\n${dialogues}`; |
| 49 | + }; |
| 50 | + |
| 51 | + header = (title) => { |
| 52 | + // prettier-ignore |
| 53 | + return `[Script Info] |
| 54 | +Title: ${title} |
| 55 | +ScriptType: v4.00+ |
| 56 | +
|
| 57 | +[V4+ Styles] |
| 58 | +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding |
| 59 | +Style: K1,Source Han Serif,${this.FONTSIZE},${formatColor(this.PRIMARY_COLOR)},${formatColor(this.SECONDARY_COLOR)},${formatColor(this.OUTLINE_COLOR)},${formatColor(this.BACKGROUND_COLOR)},${this.BOLD ? 1 : 0},0,0,0,100,100,0,0,1,${this.OUTLINE},${this.SHADOW},1,60,30,80,1 |
| 60 | +Style: K2,Source Han Serif,${this.FONTSIZE},${formatColor(this.PRIMARY_COLOR)},${formatColor(this.SECONDARY_COLOR)},${formatColor(this.OUTLINE_COLOR)},${formatColor(this.BACKGROUND_COLOR)},${this.BOLD ? 1 : 0},0,0,0,100,100,0,0,1,${this.OUTLINE},${this.SHADOW},3,30,60,40,1 |
| 61 | +[Events] |
| 62 | +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text |
| 63 | +`; |
| 64 | + }; |
| 65 | + |
| 66 | + dialogue = (line, style, assStyle, advance) => { |
| 67 | + let words; |
| 68 | + switch (style) { |
| 69 | + case Style.Traditional: |
| 70 | + words = `{\\K${Math.round(advance * 100)}}{\\K0}`; |
| 71 | + words += line["line"]; |
| 72 | + break; |
| 73 | + case Style.Karaoke: |
| 74 | + words = `{\\K${Math.round(advance * 100)}}`; |
| 75 | + words += line["words"].map(compileWord).join(""); |
| 76 | + break; |
| 77 | + default: |
| 78 | + throw new Error(`unexpected style "${style}"`); |
| 79 | + } |
| 80 | + return `Dialogue: 0,${formatTime(line["startTime"] - advance)},${formatTime( |
| 81 | + line["endTime"] + this.DELAY |
| 82 | + )},${assStyle},,0,0,0,,${words}`; |
| 83 | + }; |
| 84 | + |
| 85 | + dialogues = (lines, style) => { |
| 86 | + let result = []; |
| 87 | + let displays = new Array(this.ASS_STYLES.length).fill(0); |
| 88 | + for (const line of lines) { |
| 89 | + // Escape empty lines. |
| 90 | + if (!line["line"]) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + // Identify new paragraphs. |
| 95 | + let newParagraph = false; |
| 96 | + const lastEndTime = Math.max(...displays); |
| 97 | + if (lastEndTime < line["startTime"] - this.ADVANCE) { |
| 98 | + // Assert there is a new paragraph if there are more than `advance` |
| 99 | + // blank. |
| 100 | + newParagraph = true; |
| 101 | + } |
| 102 | + |
| 103 | + // Calculate lyrics show in advance time. |
| 104 | + const priorEndTime = Math.min(...displays); |
| 105 | + let index = 0; |
| 106 | + if (!newParagraph) { |
| 107 | + index = displays.indexOf(priorEndTime); |
| 108 | + } |
| 109 | + let advance = Math.max(line["startTime"] - priorEndTime, 0); |
| 110 | + if (newParagraph) { |
| 111 | + advance = Math.min(advance, this.ADVANCE); |
| 112 | + } |
| 113 | + |
| 114 | + const assStyle = this.ASS_STYLES[index]; |
| 115 | + result.push(this.dialogue(line, style, assStyle, advance)); |
| 116 | + |
| 117 | + // Clean up. |
| 118 | + if (newParagraph) { |
| 119 | + displays.fill(line["startTime"] - advance); |
| 120 | + } |
| 121 | + displays[index] = line["endTime"] + this.DELAY; |
| 122 | + } |
| 123 | + return result.join("\n"); |
| 124 | + }; |
| 125 | + |
| 126 | + bestStyle = (style) => { |
| 127 | + switch (style) { |
| 128 | + case Style.Traditional: |
| 129 | + switch (this.style) { |
| 130 | + case Style.Traditional: |
| 131 | + case Style.Karaoke: |
| 132 | + return Style.Traditional; |
| 133 | + default: |
| 134 | + throw new Error(`unexpected style "${this.style}"`); |
| 135 | + } |
| 136 | + case Style.Karaoke: |
| 137 | + switch (this.style) { |
| 138 | + case Style.Traditional: |
| 139 | + return Style.Traditional; |
| 140 | + case Style.Karaoke: |
| 141 | + return Style.Karaoke; |
| 142 | + default: |
| 143 | + throw new Error(`unexpected style "${this.style}"`); |
| 144 | + } |
| 145 | + default: |
| 146 | + throw new Error(`unexpected style "${style}"`); |
| 147 | + } |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +export default Subtitler; |
0 commit comments