Skip to content

Commit f714c17

Browse files
committed
Update usage
1 parent 7d0d51b commit f714c17

File tree

8 files changed

+735
-720
lines changed

8 files changed

+735
-720
lines changed

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

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,111 @@
33
Learn the basics of using ffmpeg.wasm.
44

55
:::note
6-
It is recommended to read [overview](/docs/overview) to fully understand the
7-
rationale.
6+
It is recommended to read [overview](/docs/overview) first.
87
:::
98

10-
## Basic
9+
## Transcoding AVI video to MP4 video
1110

12-
Converting an AVI video to a MP4 video:
11+
:::tip
12+
`Load ffmpeg-core` might take a few minutes to complete as it downloads
13+
a ~31 MB ffmpeg-core.wasm.
14+
:::
15+
16+
```jsx live
17+
function() {
18+
const [loaded, setLoaded] = useState(false);
19+
const ffmpegRef = useRef(new FFmpeg());
20+
const videoRef = useRef(null);
21+
const messageRef = useRef(null);
22+
23+
const load = async () => {
24+
const ffmpeg = ffmpegRef.current;
25+
ffmpeg.on("log", ({ message }) => {
26+
messageRef.current.innerHTML = message;
27+
});
28+
await ffmpeg.load();
29+
setLoaded(true);
30+
}
31+
32+
const transcode = async () => {
33+
const ffmpeg = ffmpegRef.current;
34+
await ffmpeg.writeFile(
35+
"input.avi",
36+
await fetchFile('/video/video-15s.avi')
37+
);
38+
await ffmpeg.exec(['-i', 'input.avi', 'output.mp4']);
39+
const data = await ffmpeg.readFile('output.mp4');
40+
videoRef.current.src =
41+
URL.createObjectURL(new Blob([data.buffer], {type: 'video/mp4'}));
42+
}
43+
44+
return (loaded
45+
? (
46+
<>
47+
<video ref={videoRef} controls></video><br/>
48+
<button onClick={transcode}>Transcode avi to mp4</button>
49+
<p ref={messageRef}></p>
50+
</>
51+
)
52+
: (
53+
<button onClick={load}>Load ffmpeg-core</button>
54+
)
55+
);
56+
}
57+
```
58+
59+
## Transcoding AVI video to MP4 video (multi-thread)
60+
61+
:::tip
62+
`Load ffmpeg-core` might take a few minutes to complete as it downloads
63+
a ~31 MB ffmpeg-core.wasm.
64+
:::
65+
66+
```jsx live
67+
function() {
68+
const [loaded, setLoaded] = useState(false);
69+
const ffmpegRef = useRef(new FFmpeg());
70+
const videoRef = useRef(null);
71+
const messageRef = useRef(null);
1372

14-
```js
15-
import { FFmpeg } from "@ffmpeg/ffmpeg";
16-
import { fetchFile } from "@ffmpeg/util";
73+
const load = async () => {
74+
const baseURL = 'https://unpkg.com/@ffmpeg/[email protected]/dist/umd'
75+
const ffmpeg = ffmpegRef.current;
76+
ffmpeg.on("log", ({ message }) => {
77+
messageRef.current.innerHTML = message;
78+
});
79+
await ffmpeg.load({
80+
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`),
81+
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`),
82+
workerURL: await toBlobURL(`${baseURL}/ffmpeg-core.worker.js`),
83+
thread: true,
84+
});
85+
setLoaded(true);
86+
}
1787

18-
const videoURL = "https://github.com/ffmpegwasm/testdata/raw/master/video-15s.avi";
88+
const transcode = async () => {
89+
const ffmpeg = ffmpegRef.current;
90+
await ffmpeg.writeFile(
91+
"input.avi",
92+
await fetchFile('/video/video-15s.avi')
93+
);
94+
await ffmpeg.exec(['-i', 'input.avi', 'output.mp4']);
95+
const data = await ffmpeg.readFile('output.mp4');
96+
videoRef.current.src =
97+
URL.createObjectURL(new Blob([data.buffer], {type: 'video/mp4'}));
98+
}
1999

20-
(async () => {
21-
const ffmpeg = new FFmpeg();
22-
// Create a web worker and the worker loads WebAssembly code.
23-
await ffmpeg.load();
24-
// Write a video file to FS.
25-
await ffmpeg.writeFile("input.avi", await fetchFile(videoURL));
26-
// Execute ffmpeg command.
27-
await ffmpeg.exec(["-i", "input.avi", "output.mp4"]);
28-
// Read the output video file from FS, the output file is a Uint8Array typed
29-
// array.
30-
const data = await ffmpeg.readFile("output.mp4");
31-
})();
100+
return (loaded
101+
? (
102+
<>
103+
<video ref={videoRef} controls></video><br/>
104+
<button onClick={transcode}>Transcode avi to mp4</button>
105+
<p ref={messageRef}></p>
106+
</>
107+
)
108+
: (
109+
<button onClick={load}>Load ffmpeg-core</button>
110+
)
111+
);
112+
}
32113
```

apps/website/docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const config = {
144144
},
145145
],
146146
],
147+
themes: ["@docusaurus/theme-live-codeblock"],
147148
};
148149

149150
module.exports = config;

apps/website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"@docusaurus/core": "^2.4.1",
2020
"@docusaurus/preset-classic": "^2.4.1",
21+
"@docusaurus/theme-live-codeblock": "^2.4.1",
2122
"@emotion/react": "^11.10.4",
2223
"@emotion/styled": "^11.10.4",
2324
"@ffmpeg/ffmpeg": "^0.12.0-alpha.0",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
import { FFmpeg } from "@ffmpeg/ffmpeg";
3+
import { fetchFile, toBlobURL } from "@ffmpeg/util";
4+
5+
// Add react-live imports you need here
6+
const ReactLiveScope = {
7+
React,
8+
...React,
9+
FFmpeg,
10+
fetchFile,
11+
toBlobURL,
12+
};
13+
export default ReactLiveScope;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:3eb7acdfdc29b83a012483f1b38413a6a179ae34025611a022fc07e7418a1767
3+
size 1441726
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:a281bfa75571537d7e2c0179589fb3648066be1d5409510c4ddb68d3712f6f97
3+
size 289280

0 commit comments

Comments
 (0)