@@ -6,7 +6,7 @@ Learn the basics of using ffmpeg.wasm.
6
6
It is recommended to read [ overview] ( /docs/overview ) first.
7
7
:::
8
8
9
- ## Transcoding AVI video to MP4 video
9
+ ## Transcoding video
10
10
11
11
::: tip
12
12
` Load ffmpeg-core ` might take a few minutes to complete as it downloads
@@ -58,7 +58,7 @@ function() {
58
58
}
59
59
```
60
60
61
- ## Transcoding AVI video to MP4 video (multi-thread)
61
+ ## Transcoding video (multi-thread)
62
62
63
63
::: tip
64
64
` Load ffmpeg-core ` might take a few minutes to complete as it downloads
@@ -80,6 +80,8 @@ function() {
80
80
ffmpeg .on (" log" , ({ message }) => {
81
81
messageRef .current .innerHTML = message;
82
82
});
83
+ // toBlobURL is used to bypass CORS issue, urls with the same
84
+ // domain can be used directly.
83
85
await ffmpeg .load ({
84
86
coreURL: await toBlobURL (` ${ baseURL} /ffmpeg-core.js` ),
85
87
wasmURL: await toBlobURL (` ${ baseURL} /ffmpeg-core.wasm` ),
@@ -115,3 +117,108 @@ function() {
115
117
);
116
118
}
117
119
```
120
+ ::: caution
121
+ As SharedArrayBuffer is required for multithread version, make sure
122
+ you have have fulfilled [ Security Requirements] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements ) .
123
+ :::
124
+
125
+ ## Transcoding video with timeout
126
+
127
+ ``` jsx live
128
+ // import { FFmpeg } from '@ffmpeg/ffmpeg';
129
+ // import { fetchFile } from '@ffmpeg/util';
130
+ function () {
131
+ const [loaded , setLoaded ] = useState (false );
132
+ const ffmpegRef = useRef (new FFmpeg ());
133
+ const videoRef = useRef (null );
134
+ const messageRef = useRef (null );
135
+
136
+ const load = async () => {
137
+ const ffmpeg = ffmpegRef .current ;
138
+ ffmpeg .on (" log" , ({ message }) => {
139
+ messageRef .current .innerHTML = message;
140
+ });
141
+ await ffmpeg .load ();
142
+ setLoaded (true );
143
+ }
144
+
145
+ const transcode = async () => {
146
+ const ffmpeg = ffmpegRef .current ;
147
+ await ffmpeg .writeFile (
148
+ " input.avi" ,
149
+ await fetchFile (' /video/video-15s.avi' )
150
+ );
151
+ // The exec should stop after 1 second.
152
+ await ffmpeg .exec ([' -i' , ' input.avi' , ' output.mp4' ], 1 );
153
+ const data = await ffmpeg .readFile (' output.mp4' );
154
+ videoRef .current .src =
155
+ URL .createObjectURL (new Blob ([data .buffer ], {type: ' video/mp4' }));
156
+ }
157
+
158
+ return (loaded
159
+ ? (
160
+ <>
161
+ < video ref= {videoRef} controls>< / video>< br/ >
162
+ < button onClick= {transcode}> Transcode avi to mp4< / button>
163
+ < p ref= {messageRef}>< / p>
164
+ < / >
165
+ )
166
+ : (
167
+ < button onClick= {load}> Load ffmpeg- core< / button>
168
+ )
169
+ );
170
+ }
171
+ ```
172
+
173
+ ## Transcoding video with progress
174
+
175
+ ``` jsx live
176
+ // import { FFmpeg } from '@ffmpeg/ffmpeg';
177
+ // import { fetchFile } from '@ffmpeg/util';
178
+ function () {
179
+ const [loaded , setLoaded ] = useState (false );
180
+ const ffmpegRef = useRef (new FFmpeg ());
181
+ const videoRef = useRef (null );
182
+ const messageRef = useRef (null );
183
+
184
+ const load = async () => {
185
+ const ffmpeg = ffmpegRef .current ;
186
+ // Listen to progress event instead of log.
187
+ ffmpeg .on (" progress" , (progress ) => {
188
+ messageRef .current .innerHTML = ` ${ progress * 100 } %` ;
189
+ });
190
+ await ffmpeg .load ();
191
+ setLoaded (true );
192
+ }
193
+
194
+ const transcode = async () => {
195
+ const ffmpeg = ffmpegRef .current ;
196
+ await ffmpeg .writeFile (
197
+ " input.avi" ,
198
+ await fetchFile (' /video/video-15s.avi' )
199
+ );
200
+ await ffmpeg .exec ([' -i' , ' input.avi' , ' output.mp4' ]);
201
+ const data = await ffmpeg .readFile (' output.mp4' );
202
+ videoRef .current .src =
203
+ URL .createObjectURL (new Blob ([data .buffer ], {type: ' video/mp4' }));
204
+ }
205
+
206
+ return (loaded
207
+ ? (
208
+ <>
209
+ < video ref= {videoRef} controls>< / video>< br/ >
210
+ < button onClick= {transcode}> Transcode avi to mp4< / button>
211
+ < p ref= {messageRef}>< / p>
212
+ < / >
213
+ )
214
+ : (
215
+ < button onClick= {load}> Load ffmpeg- core< / button>
216
+ )
217
+ );
218
+ }
219
+ ```
220
+
221
+ ::: caution
222
+ ` progress ` is an experimental feature and might not work for many cases
223
+ (ex. concat video files, convert image files, ...). Please use with caution.
224
+ :::
0 commit comments