Skip to content

Commit 1775afe

Browse files
committed
Update usage
1 parent 8f50bd3 commit 1775afe

File tree

2 files changed

+1711
-16238
lines changed

2 files changed

+1711
-16238
lines changed

apps/website/docs/getting-started/usage.md

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Learn the basics of using ffmpeg.wasm.
66
It is recommended to read [overview](/docs/overview) first.
77
:::
88

9-
## Transcoding AVI video to MP4 video
9+
## Transcoding video
1010

1111
:::tip
1212
`Load ffmpeg-core` might take a few minutes to complete as it downloads
@@ -58,7 +58,7 @@ function() {
5858
}
5959
```
6060

61-
## Transcoding AVI video to MP4 video (multi-thread)
61+
## Transcoding video (multi-thread)
6262

6363
:::tip
6464
`Load ffmpeg-core` might take a few minutes to complete as it downloads
@@ -80,6 +80,8 @@ function() {
8080
ffmpeg.on("log", ({ message }) => {
8181
messageRef.current.innerHTML = message;
8282
});
83+
// toBlobURL is used to bypass CORS issue, urls with the same
84+
// domain can be used directly.
8385
await ffmpeg.load({
8486
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`),
8587
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`),
@@ -115,3 +117,108 @@ function() {
115117
);
116118
}
117119
```
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

Comments
 (0)