1
- API
2
- ===
1
+ # API
3
2
4
3
- [ createWorker()] ( #create-worker )
5
4
- [ Worker.load] ( #worker-load )
14
13
---
15
14
16
15
<a name =" create-worker " ></a >
16
+
17
17
## createWorker(options): Worker
18
18
19
19
createWorker is a factory function that creates a ffmpeg worker, a worker is basically a Web Worker in browser and Child Process in Node.
@@ -27,17 +27,18 @@ createWorker is a factory function that creates a ffmpeg worker, a worker is bas
27
27
- ` logger ` a function to log the progress, a quick example is ` m => console.log(m) `
28
28
- ` progress ` a function to trace the progress, a quick example is ` p => console.log(p) `
29
29
30
-
31
30
** Examples:**
32
31
33
32
``` javascript
34
33
const { createWorker } = FFmpeg;
35
34
const worker = createWorker ({
36
- corePath: ' ./node_modules/@ffmpeg/core/ffmpeg-core.js' ,
37
- logger : m => console .log (m),
35
+ corePath: " ./node_modules/@ffmpeg/core/ffmpeg-core.js" ,
36
+ logger : m => console .log (m)
38
37
});
39
38
```
39
+
40
40
<a name =" worker-load " ></a >
41
+
41
42
### Worker.load(jobId): Promise
42
43
43
44
Worker.load() loads ffmpeg-core.js script (download from remote if not presented), it makes Web Worker/Child Process ready for next action.
@@ -55,6 +56,7 @@ Worker.load() loads ffmpeg-core.js script (download from remote if not presented
55
56
```
56
57
57
58
<a name =" worker-write " ></a >
59
+
58
60
### Worker.write(path, data): Promise
59
61
60
62
Worker.write() writes data to specific path in Emscripten file system, it is an essential step before doing any other tasks.
@@ -68,11 +70,15 @@ Worker.write() writes data to specific path in Emscripten file system, it is an
68
70
69
71
``` javascript
70
72
(async () => {
71
- await worker .write (' flame.avi' , ' http://localhost:3000/tests/assets/flame.avi' );
73
+ await worker .write (
74
+ " flame.avi" ,
75
+ " http://localhost:3000/tests/assets/flame.avi"
76
+ );
72
77
})();
73
78
```
74
79
75
80
<a name =" worker-writeText " ></a >
81
+
76
82
### Worker.writeText(path, text): Promise
77
83
78
84
Worker.write() writes text data to specific path in Emscripten file system.
@@ -86,11 +92,12 @@ Worker.write() writes text data to specific path in Emscripten file system.
86
92
87
93
``` javascript
88
94
(async () => {
89
- await worker .write (' sub.srt' , ' ...' );
95
+ await worker .write (" sub.srt" , " ..." );
90
96
})();
91
97
```
92
98
93
99
<a name =" worker-read " ></a >
100
+
94
101
### Worker.read(path, del): Promise
95
102
96
103
Worker.read() reads data from file system, often used to get output data after specific task.
@@ -104,11 +111,12 @@ Worker.read() reads data from file system, often used to get output data after s
104
111
105
112
``` javascript
106
113
(async () => {
107
- const { data } = await worker .read (' output.mp4' );
114
+ const { data } = await worker .read (" output.mp4" );
108
115
})();
109
116
```
110
117
111
118
<a name =" worker-remove " ></a >
119
+
112
120
### Worker.remove(path): Promise
113
121
114
122
Worker.remove() removes files in file system, it will be better to delete unused files if you need to run ffmpeg.js multiple times.
@@ -121,11 +129,12 @@ Worker.remove() removes files in file system, it will be better to delete unused
121
129
122
130
``` javascript
123
131
(async () => {
124
- await worker .remove (' output.mp4' );
132
+ await worker .remove (" output.mp4" );
125
133
})();
126
134
```
127
135
128
136
<a name =" worker-transcode " ></a >
137
+
129
138
### Worker.transcode(inputPath, outputPath, options, del, jobId): Promise
130
139
131
140
Worker.transcode() transcode a video file to another format.
@@ -142,11 +151,12 @@ Worker.transcode() transcode a video file to another format.
142
151
143
152
``` javascript
144
153
(async () => {
145
- await worker .transcode (' flame.avi' , ' output.mp4' , ' -s 1920x1080' );
154
+ await worker .transcode (" flame.avi" , " output.mp4" , " -s 1920x1080" );
146
155
})();
147
156
```
148
157
149
158
<a name =" worker-trim " ></a >
159
+
150
160
### Worker.trim(inputPath, outputPath, from, to, options, del, jobId): Promise
151
161
152
162
Worker.trim() trims video to specific interval.
@@ -165,11 +175,34 @@ Worker.trim() trims video to specific interval.
165
175
166
176
``` javascript
167
177
(async () => {
168
- await worker .trim (' flame.avi' , ' output.mp4' , 1 , 2 );
178
+ await worker .trim (" flame.avi" , " output.mp4" , 1 , 2 );
179
+ })();
180
+ ```
181
+
182
+ <a name =" worker-concatDemuxer " ></a >
183
+
184
+ ### Worker.concatDemuxer(inputPaths, outputPath, options, del, jobId): Promise
185
+
186
+ Worker.concatDemuxer() concatenates multiple videos using concatDemuxer. This method won't encode the videos again. But it has its limitations. See [ Concat demuxer Wiki] ( https://trac.ffmpeg.org/wiki/Concatenate )
187
+
188
+ ** Arguments:**
189
+
190
+ - ` inputPaths ` input file paths as an Array, the input files should be written through Worker.write()
191
+ - ` outputPath ` output file path, can be read with Worker.read() later
192
+ - ` options ` a string to add extra arguments to ffmpeg
193
+ - ` del ` a boolean to determine whether to delete input file after the task is done, default: true
194
+ - ` jobId ` check Worker.load()
195
+
196
+ ** Examples:**
197
+
198
+ ``` javascript
199
+ (async () => {
200
+ await worker .trim ([" flame-1.avi" , " flame-2.avi" ], " output.mp4" );
169
201
})();
170
202
```
171
203
172
204
<a name =" worker-run " ></a >
205
+
173
206
### Worker.run(args, options, jobId): Promise
174
207
175
208
Worker.run() is similar to FFmpeg cli tool, aims to provide maximum flexiblity for users.
@@ -184,6 +217,9 @@ Worker.run() is similar to FFmpeg cli tool, aims to provide maximum flexiblity f
184
217
185
218
``` javascript
186
219
(async () => {
187
- await worker .run (' -i /data/flame.avi -s 1920x1080 output.mp4' , { inputPath: ' flame.avi' , outputPath: ' output.mp4' });
220
+ await worker .run (" -i /data/flame.avi -s 1920x1080 output.mp4" , {
221
+ inputPath: " flame.avi" ,
222
+ outputPath: " output.mp4"
223
+ });
188
224
})();
189
225
```
0 commit comments