transl8-srt 1.0.1
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/README.md +176 -0
- package/dist/index.cjs +7469 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +96 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +7440 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# transl8-srt
|
|
2
|
+
|
|
3
|
+
Fast, accurate, cost-effective subtitle file translation using OpenAI.
|
|
4
|
+
Supports `.srt` and `.vtt`. Optimised for sermon content; works for general video too.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install transl8-srt openai
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Set your OpenAI key:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
export OPENAI_API_KEY=sk-...
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Translate a string in memory
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import {translate} from "transl8-srt";
|
|
28
|
+
|
|
29
|
+
const srtContent = `
|
|
30
|
+
1
|
|
31
|
+
00:00:01,000 --> 00:00:03,500
|
|
32
|
+
And the grace of God
|
|
33
|
+
|
|
34
|
+
2
|
|
35
|
+
00:00:03,500 --> 00:00:05,800
|
|
36
|
+
is sufficient for every one of us.
|
|
37
|
+
`.trim();
|
|
38
|
+
|
|
39
|
+
const result = await translate(srtContent, {
|
|
40
|
+
targetLang: "fr",
|
|
41
|
+
meta: {
|
|
42
|
+
speakerName: "Pastor Emeka Eze",
|
|
43
|
+
churchName: "Covenant Christian Centre",
|
|
44
|
+
seriesTitle: "The Grace Revolution",
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
console.log(result.toSRT());
|
|
49
|
+
// 1
|
|
50
|
+
// 00:00:01,000 --> 00:00:03,500
|
|
51
|
+
// Et la grâce de Dieu
|
|
52
|
+
//
|
|
53
|
+
// 2
|
|
54
|
+
// 00:00:03,500 --> 00:00:05,800
|
|
55
|
+
// est suffisante pour chacun d'entre nous.
|
|
56
|
+
|
|
57
|
+
console.log(result.stats);
|
|
58
|
+
// {
|
|
59
|
+
// totalCues: 2,
|
|
60
|
+
// totalSentences: 1,
|
|
61
|
+
// totalBatches: 1,
|
|
62
|
+
// inputTokens: 312,
|
|
63
|
+
// outputTokens: 48,
|
|
64
|
+
// estimatedCostUsd: 0.0000528,
|
|
65
|
+
// durationMs: 843
|
|
66
|
+
// }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
### Translate directly from/to file
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import {translateFile} from "transl8-srt";
|
|
75
|
+
|
|
76
|
+
const result = await translateFile(
|
|
77
|
+
"./sermon-english.srt",
|
|
78
|
+
"./sermon-yoruba.srt",
|
|
79
|
+
{
|
|
80
|
+
targetLang: "yo", // Yoruba — auto-routes to gpt-4o
|
|
81
|
+
meta: {
|
|
82
|
+
speakerName: "Pastor David",
|
|
83
|
+
churchName: "House on the Rock",
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
console.log(`Done in ${result.stats.durationMs}ms`);
|
|
89
|
+
console.log(`Cost: $${result.stats.estimatedCostUsd.toFixed(6)}`);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
### Translate to multiple languages in parallel
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import {translate} from "transl8-srt";
|
|
98
|
+
import {readFile, writeFile} from "fs/promises";
|
|
99
|
+
|
|
100
|
+
const srt = await readFile("./sermon.srt", "utf-8");
|
|
101
|
+
|
|
102
|
+
const languages = ["fr", "pt", "yo", "ig", "ha"] as const;
|
|
103
|
+
|
|
104
|
+
const results = await Promise.all(
|
|
105
|
+
languages.map((lang) =>
|
|
106
|
+
translate(srt, {
|
|
107
|
+
targetLang: lang,
|
|
108
|
+
meta: {speakerName: "Pastor Emeka", churchName: "RCCG"},
|
|
109
|
+
})
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
for (const [i, lang] of languages.entries()) {
|
|
114
|
+
await writeFile(`./sermon-${lang}.srt`, results[i].toSRT(), "utf-8");
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
### Bring your own OpenAI client (custom config / Azure)
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import OpenAI from "openai";
|
|
124
|
+
import {translate} from "transl8-srt";
|
|
125
|
+
|
|
126
|
+
const client = new OpenAI({
|
|
127
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
128
|
+
baseURL: process.env.OPENAI_BASE_URL, // e.g. Azure OpenAI endpoint
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const result = await translate(content, {targetLang: "es"}, client);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Options reference
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
interface TranslateOptions {
|
|
140
|
+
targetLang: string; // Required. BCP-47 language tag: "fr", "yo", "pt", etc.
|
|
141
|
+
sourceLang?: string; // Default: "en"
|
|
142
|
+
domain?: "sermon" | "general"; // Default: "sermon"
|
|
143
|
+
meta?: {
|
|
144
|
+
speakerName?: string; // Kept verbatim — not translated
|
|
145
|
+
churchName?: string; // Kept verbatim — not translated
|
|
146
|
+
seriesTitle?: string; // Kept verbatim — not translated
|
|
147
|
+
glossary?: Record<string, string>; // Custom term mappings
|
|
148
|
+
};
|
|
149
|
+
batchSize?: number; // Sentences per API call. Default: 25
|
|
150
|
+
contextOverlap?: number; // Overlap sentences from prior batch. Default: 4
|
|
151
|
+
concurrency?: number; // Parallel API requests. Default: 8
|
|
152
|
+
model?: "gpt-4o" | "gpt-4o-mini"; // Auto-selected based on targetLang if omitted
|
|
153
|
+
primer?: boolean; // AI-inferred style profile. Default: false
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Auto model selection
|
|
158
|
+
|
|
159
|
+
| Target language | Auto model |
|
|
160
|
+
|----------------------------------------------|------------------------------|
|
|
161
|
+
| French, Portuguese, Spanish, German, Italian | `gpt-4o-mini` (~12× cheaper) |
|
|
162
|
+
| Yoruba, Igbo, Hausa, Swahili, Zulu | `gpt-4o` (quality) |
|
|
163
|
+
| Arabic, Amharic, and other non-Latin scripts | `gpt-4o` (quality) |
|
|
164
|
+
|
|
165
|
+
Override with `model: "gpt-4o"` to force the full model on any language.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Typical costs
|
|
170
|
+
|
|
171
|
+
| Content | Cues | Model | Cost |
|
|
172
|
+
|--------------------|--------|-----------------|----------|
|
|
173
|
+
| Reel (1–2 min) | ~80 | gpt-4o-mini | ~$0.0003 |
|
|
174
|
+
| Sermon (30 min) | ~2,000 | gpt-4o-mini | ~$0.008 |
|
|
175
|
+
| Sermon (30 min) | ~2,000 | gpt-4o (Yoruba) | ~$0.05 |
|
|
176
|
+
| Full film (90 min) | ~6,000 | gpt-4o-mini | ~$0.02 |
|