@nxtedition/lib 14.0.15 → 14.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ass.js +30 -35
- package/package.json +1 -1
package/ass.js
CHANGED
|
@@ -2,35 +2,23 @@ const fp = require('lodash/fp')
|
|
|
2
2
|
|
|
3
3
|
module.exports = { encodeASS }
|
|
4
4
|
|
|
5
|
-
function encodeASS(events, { styles = {},
|
|
6
|
-
return [
|
|
7
|
-
encASSHeader(options),
|
|
8
|
-
encASSStyles(styles),
|
|
9
|
-
encASSEvents(
|
|
10
|
-
events.map((event) => {
|
|
11
|
-
// NOTE: :subtitle uses `end`, :event uses `duration`
|
|
12
|
-
if (event.end == null) {
|
|
13
|
-
return { ...event, end: event.duration != null ? event.start + event.duration : null }
|
|
14
|
-
}
|
|
15
|
-
return event
|
|
16
|
-
})
|
|
17
|
-
),
|
|
18
|
-
].join('\n')
|
|
5
|
+
function encodeASS(events, { styles = {}, width = 1920, height = 1080 } = {}) {
|
|
6
|
+
return [encASSHeader({ width, height }), encASSStyles(styles), encASSEvents(events)].join('\n')
|
|
19
7
|
}
|
|
20
8
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
s
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
9
|
+
function formatDialogues(events) {
|
|
10
|
+
let s = ''
|
|
11
|
+
for (const { start, duration, end = duration != null ? start + duration : null, text, style } of [
|
|
12
|
+
...events,
|
|
13
|
+
].sort((a, b) => a.start - b.start)) {
|
|
14
|
+
if (typeof text === 'string' && text.length > 0 && Number.isFinite(start)) {
|
|
15
|
+
s += `Dialogue: 0,${formatASSTime(start) || '0:00:00.00'},${
|
|
16
|
+
formatASSTime(end) || '9:59:59.00'
|
|
17
|
+
},${style || 'nxt-default'},,0000,0000,0000,,${(text || '').replace(/\\n|\n/, '\\N')}\n`
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return s
|
|
21
|
+
}
|
|
34
22
|
|
|
35
23
|
function encASSEvents(events) {
|
|
36
24
|
return `[Events]
|
|
@@ -100,7 +88,7 @@ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour,
|
|
|
100
88
|
${formatStyles(styles)}`
|
|
101
89
|
}
|
|
102
90
|
|
|
103
|
-
function encASSHeader({ width, height }
|
|
91
|
+
function encASSHeader({ width, height }) {
|
|
104
92
|
// WrapStyle
|
|
105
93
|
// 0: smart wrapping, lines are evenly broken
|
|
106
94
|
// 1: end-of-line word wrapping, only \N breaks
|
|
@@ -114,19 +102,26 @@ WrapStyle: 1
|
|
|
114
102
|
`
|
|
115
103
|
}
|
|
116
104
|
|
|
117
|
-
function pad0(rep, x) {
|
|
118
|
-
return String(x || 0).padStart(rep, '0')
|
|
119
|
-
}
|
|
120
|
-
|
|
121
105
|
function formatASSTime(seconds) {
|
|
122
106
|
if (seconds == null) {
|
|
123
107
|
return ''
|
|
124
108
|
}
|
|
125
|
-
|
|
126
|
-
|
|
109
|
+
|
|
110
|
+
const h = Math.floor(seconds / 3600)
|
|
111
|
+
seconds -= h * 3600
|
|
127
112
|
const m = Math.floor(seconds / 60)
|
|
128
113
|
seconds -= m * 60
|
|
129
114
|
const s = Math.floor(seconds)
|
|
130
115
|
const cs = Math.floor((seconds - s) * 100)
|
|
131
|
-
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
h +
|
|
119
|
+
':' +
|
|
120
|
+
(m === 0 ? '00' : m < 10 ? '0' + m : m) +
|
|
121
|
+
':' +
|
|
122
|
+
(s === 0 ? '00' : s < 10 ? '0' + s : s) +
|
|
123
|
+
':' +
|
|
124
|
+
(cs === 0 ? '00' : cs < 10 ? '0' + cs : cs) +
|
|
125
|
+
':'
|
|
126
|
+
)
|
|
132
127
|
}
|