@xiping/node-utils 1.0.60 → 1.0.62
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 +45 -0
- package/lib/src/ffmpeg/README.md +370 -0
- package/lib/src/ffmpeg/README_cutVideo.md +220 -0
- package/lib/src/ffmpeg/check.js +3 -3
- package/lib/src/ffmpeg/cutVideo.js +17 -21
- package/lib/src/ffmpeg/extractAudio.d.ts +36 -0
- package/lib/src/ffmpeg/extractAudio.js +190 -0
- package/lib/src/ffmpeg/getThumbnail.js +16 -3
- package/lib/src/ffmpeg/getVideoInfo.js +20 -7
- package/lib/src/ffmpeg/index.d.ts +1 -0
- package/lib/src/ffmpeg/index.js +1 -0
- package/lib/src/ffmpeg/runSync.d.ts +15 -0
- package/lib/src/ffmpeg/runSync.js +20 -0
- package/lib/src/image/README.md +230 -0
- package/lib/src/srt-to-vtt/README.md +189 -0
- package/package.json +8 -10
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# SRT到VTT转换器
|
|
2
|
+
|
|
3
|
+
这个模块提供了在Node.js环境中将SRT字幕文件转换为WebVTT格式的功能。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- ✅ 从文件读取SRT并转换为VTT
|
|
8
|
+
- ✅ 支持批量转换多个SRT文件
|
|
9
|
+
- ✅ 支持字符串内容直接转换
|
|
10
|
+
- ✅ 自动验证SRT格式
|
|
11
|
+
- ✅ 完整的错误处理
|
|
12
|
+
- ✅ TypeScript支持
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @xiping/node-utils
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 使用方法
|
|
21
|
+
|
|
22
|
+
### 1. 从文件读取SRT并转换为VTT字符串
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { convertSrtFileToVtt } from '@xiping/node-utils/src/srt-to-vtt';
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const vttContent = convertSrtFileToVtt('./subtitles.srt');
|
|
29
|
+
console.log('转换后的VTT内容:', vttContent);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('转换失败:', error);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. 从文件读取SRT并保存为VTT文件
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { convertSrtFileToVttFile } from '@xiping/node-utils/src/srt-to-vtt';
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
// 指定输出路径
|
|
42
|
+
const outputPath = convertSrtFileToVttFile('./subtitles.srt', './output/subtitles.vtt');
|
|
43
|
+
console.log('VTT文件已保存到:', outputPath);
|
|
44
|
+
|
|
45
|
+
// 或者使用默认输出路径(同目录,扩展名改为.vtt)
|
|
46
|
+
const defaultOutputPath = convertSrtFileToVttFile('./subtitles.srt');
|
|
47
|
+
console.log('默认输出路径:', defaultOutputPath);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('文件转换失败:', error);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. 批量转换多个SRT文件
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { batchConvertSrtToVtt } from '@xiping/node-utils/src/srt-to-vtt';
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const srtFiles = [
|
|
60
|
+
'./subtitles1.srt',
|
|
61
|
+
'./subtitles2.srt',
|
|
62
|
+
'./subtitles3.srt'
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const results = batchConvertSrtToVtt(srtFiles, './output');
|
|
66
|
+
|
|
67
|
+
results.forEach(result => {
|
|
68
|
+
if (result.success) {
|
|
69
|
+
console.log(`✅ ${result.input} -> ${result.output}`);
|
|
70
|
+
} else {
|
|
71
|
+
console.error(`❌ ${result.input}: ${result.error}`);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('批量转换失败:', error);
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 4. 从字符串内容直接转换
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { convertSrtStringToVtt } from '@xiping/node-utils/src/srt-to-vtt';
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const srtContent = `1
|
|
86
|
+
00:00:00,000 --> 00:00:04,000
|
|
87
|
+
这是第一行字幕
|
|
88
|
+
|
|
89
|
+
2
|
|
90
|
+
00:00:04,000 --> 00:00:08,000
|
|
91
|
+
这是第二行字幕`;
|
|
92
|
+
|
|
93
|
+
const vttContent = convertSrtStringToVtt(srtContent);
|
|
94
|
+
console.log('转换后的VTT内容:', vttContent);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error('字符串转换失败:', error);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API参考
|
|
101
|
+
|
|
102
|
+
### convertSrtFileToVtt(srtFilePath: string): string
|
|
103
|
+
|
|
104
|
+
从文件读取SRT内容并转换为VTT格式字符串。
|
|
105
|
+
|
|
106
|
+
**参数:**
|
|
107
|
+
- `srtFilePath`: SRT文件路径
|
|
108
|
+
|
|
109
|
+
**返回值:**
|
|
110
|
+
- 转换后的VTT内容字符串
|
|
111
|
+
|
|
112
|
+
**异常:**
|
|
113
|
+
- 文件不存在时抛出错误
|
|
114
|
+
- 文件格式无效时抛出错误
|
|
115
|
+
|
|
116
|
+
### convertSrtFileToVttFile(srtFilePath: string, outputPath?: string): string
|
|
117
|
+
|
|
118
|
+
从文件读取SRT内容并转换为VTT格式,同时保存到文件。
|
|
119
|
+
|
|
120
|
+
**参数:**
|
|
121
|
+
- `srtFilePath`: SRT文件路径
|
|
122
|
+
- `outputPath`: 输出VTT文件路径(可选)
|
|
123
|
+
|
|
124
|
+
**返回值:**
|
|
125
|
+
- 输出文件路径
|
|
126
|
+
|
|
127
|
+
### batchConvertSrtToVtt(srtFilePaths: string[], outputDir?: string): Array<{ input: string; output: string; success: boolean; error?: string }>
|
|
128
|
+
|
|
129
|
+
批量转换多个SRT文件到VTT格式。
|
|
130
|
+
|
|
131
|
+
**参数:**
|
|
132
|
+
- `srtFilePaths`: SRT文件路径数组
|
|
133
|
+
- `outputDir`: 输出目录(可选)
|
|
134
|
+
|
|
135
|
+
**返回值:**
|
|
136
|
+
- 转换结果数组,包含每个文件的转换状态
|
|
137
|
+
|
|
138
|
+
### convertSrtStringToVtt(srtContent: string): string
|
|
139
|
+
|
|
140
|
+
从字符串内容直接转换为VTT格式。
|
|
141
|
+
|
|
142
|
+
**参数:**
|
|
143
|
+
- `srtContent`: SRT内容字符串
|
|
144
|
+
|
|
145
|
+
**返回值:**
|
|
146
|
+
- VTT格式字符串
|
|
147
|
+
|
|
148
|
+
## 格式说明
|
|
149
|
+
|
|
150
|
+
### SRT格式
|
|
151
|
+
```
|
|
152
|
+
1
|
|
153
|
+
00:00:00,000 --> 00:00:04,000
|
|
154
|
+
字幕内容
|
|
155
|
+
|
|
156
|
+
2
|
|
157
|
+
00:00:04,000 --> 00:00:08,000
|
|
158
|
+
字幕内容
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### WebVTT格式
|
|
162
|
+
```
|
|
163
|
+
WEBVTT
|
|
164
|
+
|
|
165
|
+
00:00:00.000 --> 00:00:04.000
|
|
166
|
+
字幕内容
|
|
167
|
+
|
|
168
|
+
00:00:04.000 --> 00:00:08.000
|
|
169
|
+
字幕内容
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## 主要差异
|
|
173
|
+
|
|
174
|
+
1. **时间格式**: SRT使用逗号分隔毫秒,VTT使用点分隔
|
|
175
|
+
2. **头部**: VTT需要`WEBVTT`头部
|
|
176
|
+
3. **索引**: SRT包含序号,VTT不包含
|
|
177
|
+
|
|
178
|
+
## 错误处理
|
|
179
|
+
|
|
180
|
+
所有函数都包含完整的错误处理:
|
|
181
|
+
|
|
182
|
+
- 文件不存在
|
|
183
|
+
- 文件格式错误
|
|
184
|
+
- 权限问题
|
|
185
|
+
- 编码问题
|
|
186
|
+
|
|
187
|
+
## 示例
|
|
188
|
+
|
|
189
|
+
查看 `example.ts` 文件获取完整的使用示例。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiping/node-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.62",
|
|
4
4
|
"description": "node-utils",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "The-End-Hero <527409987@qq.com>",
|
|
@@ -16,16 +16,16 @@
|
|
|
16
16
|
],
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/The-End-Hero/
|
|
19
|
+
"url": "git+https://github.com/The-End-Hero/xiping.git"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
23
|
-
"build": "tsc"
|
|
23
|
+
"build": "tsc && node scripts/copy-readmes.js"
|
|
24
24
|
},
|
|
25
25
|
"bugs": {
|
|
26
|
-
"url": "https://github.com/The-End-Hero/
|
|
26
|
+
"url": "https://github.com/The-End-Hero/xiping/issues"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "83ac9cffc81c4bbd5b78e1ecf621029630c98c20",
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public",
|
|
31
31
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -33,14 +33,12 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@xiping/subtitle": "1.0.52",
|
|
35
35
|
"chalk": "^5.6.2",
|
|
36
|
-
"
|
|
37
|
-
"sharp": "^0.34.4",
|
|
38
|
-
"shelljs": "0.10.0",
|
|
36
|
+
"sharp": "^0.34.5",
|
|
39
37
|
"srt-parser-2": "^1.2.3"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"@types/node": "^24.10.
|
|
40
|
+
"@types/node": "^24.10.13",
|
|
43
41
|
"tslib": "^2.8.1",
|
|
44
|
-
"typescript": "^5.9.
|
|
42
|
+
"typescript": "^5.9.3"
|
|
45
43
|
}
|
|
46
44
|
}
|