Update usage

This commit is contained in:
Jerome Wu 2023-07-26 00:23:09 +08:00
parent 566593182b
commit 8370ef5937
8 changed files with 729 additions and 720 deletions

View File

@ -3,30 +3,111 @@
Learn the basics of using ffmpeg.wasm. Learn the basics of using ffmpeg.wasm.
:::note :::note
It is recommended to read [overview](/docs/overview) to fully understand the It is recommended to read [overview](/docs/overview) first.
rationale.
::: :::
## Basic ## Transcoding AVI video to MP4 video
Converting an AVI video to a MP4 video: :::tip
`Load ffmpeg-core` might take a few minutes to complete as it downloads
a ~31 MB ffmpeg-core.wasm.
:::
```js ```jsx live
import { FFmpeg } from "@ffmpeg/ffmpeg"; function() {
import { fetchFile } from "@ffmpeg/util"; const [loaded, setLoaded] = useState(false);
const ffmpegRef = useRef(new FFmpeg());
const videoRef = useRef(null);
const messageRef = useRef(null);
const videoURL = "https://github.com/ffmpegwasm/testdata/raw/master/video-15s.avi"; const load = async () => {
const ffmpeg = ffmpegRef.current;
(async () => { ffmpeg.on("log", ({ message }) => {
const ffmpeg = new FFmpeg(); messageRef.current.innerHTML = message;
// Create a web worker and the worker loads WebAssembly code. });
await ffmpeg.load(); await ffmpeg.load();
// Write a video file to FS. setLoaded(true);
await ffmpeg.writeFile("input.avi", await fetchFile(videoURL)); }
// Execute ffmpeg command.
await ffmpeg.exec(["-i", "input.avi", "output.mp4"]); const transcode = async () => {
// Read the output video file from FS, the output file is a Uint8Array typed const ffmpeg = ffmpegRef.current;
// array. await ffmpeg.writeFile(
const data = await ffmpeg.readFile("output.mp4"); "input.avi",
})(); await fetchFile('/video/video-15s.avi')
);
await ffmpeg.exec(['-i', 'input.avi', 'output.mp4']);
const data = await ffmpeg.readFile('output.mp4');
videoRef.current.src =
URL.createObjectURL(new Blob([data.buffer], {type: 'video/mp4'}));
}
return (loaded
? (
<>
<video ref={videoRef} controls></video><br/>
<button onClick={transcode}>Transcode avi to mp4</button>
<p ref={messageRef}></p>
</>
)
: (
<button onClick={load}>Load ffmpeg-core</button>
)
);
}
```
## Transcoding AVI video to MP4 video (multi-thread)
:::tip
`Load ffmpeg-core` might take a few minutes to complete as it downloads
a ~31 MB ffmpeg-core.wasm.
:::
```jsx live
function() {
const [loaded, setLoaded] = useState(false);
const ffmpegRef = useRef(new FFmpeg());
const videoRef = useRef(null);
const messageRef = useRef(null);
const load = async () => {
const baseURL = 'https://unpkg.com/@ffmpeg/core-mt@0.12.0-alpha.2/dist/umd'
const ffmpeg = ffmpegRef.current;
ffmpeg.on("log", ({ message }) => {
messageRef.current.innerHTML = message;
});
await ffmpeg.load({
coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`),
wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`),
workerURL: await toBlobURL(`${baseURL}/ffmpeg-core.worker.js`),
thread: true,
});
setLoaded(true);
}
const transcode = async () => {
const ffmpeg = ffmpegRef.current;
await ffmpeg.writeFile(
"input.avi",
await fetchFile('/video/video-15s.avi')
);
await ffmpeg.exec(['-i', 'input.avi', 'output.mp4']);
const data = await ffmpeg.readFile('output.mp4');
videoRef.current.src =
URL.createObjectURL(new Blob([data.buffer], {type: 'video/mp4'}));
}
return (loaded
? (
<>
<video ref={videoRef} controls></video><br/>
<button onClick={transcode}>Transcode avi to mp4</button>
<p ref={messageRef}></p>
</>
)
: (
<button onClick={load}>Load ffmpeg-core</button>
)
);
}
``` ```

View File

@ -144,6 +144,7 @@ const config = {
}, },
], ],
], ],
themes: ["@docusaurus/theme-live-codeblock"],
}; };
module.exports = config; module.exports = config;

View File

@ -18,6 +18,7 @@
"dependencies": { "dependencies": {
"@docusaurus/core": "^2.4.1", "@docusaurus/core": "^2.4.1",
"@docusaurus/preset-classic": "^2.4.1", "@docusaurus/preset-classic": "^2.4.1",
"@docusaurus/theme-live-codeblock": "^2.4.1",
"@emotion/react": "^11.10.4", "@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4", "@emotion/styled": "^11.10.4",
"@ffmpeg/ffmpeg": "^0.12.0-alpha.0", "@ffmpeg/ffmpeg": "^0.12.0-alpha.0",

View File

@ -0,0 +1,13 @@
import React from "react";
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { fetchFile, toBlobURL } from "@ffmpeg/util";
// Add react-live imports you need here
const ReactLiveScope = {
React,
...React,
FFmpeg,
fetchFile,
toBlobURL,
};
export default ReactLiveScope;

Binary file not shown.

Binary file not shown.

1296
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,23 +22,6 @@ export interface FFMessageLoadConfig {
* @defaultValue `https://unpkg.com/@ffmpeg/core-mt@${CORE_VERSION}/dist/umd/ffmpeg-core.worker.js`; * @defaultValue `https://unpkg.com/@ffmpeg/core-mt@${CORE_VERSION}/dist/umd/ffmpeg-core.worker.js`;
*/ */
workerURL?: string; workerURL?: string;
/**
* When `blob` is true, the content of `coreURL`, `wasmURL` and `workerURL`
* will be fetched and convert to blob URL. This avoids problems like CORS
* and provides download progress than can be listened like below:
*
* @example
* ```ts
* const ffmpeg = new FFmpeg();
* ffmpeg.on(FFmpeg.DOWNLOAD, (ev) => {
* console.log(ev);
* })
* await ffmpeg.load();
* ```
*
* @defaultValue `true`
*/
blob?: boolean;
/** /**
* When `thread` is true, ffmpeg imports `ffmpeg-core.worker.js` and thus * When `thread` is true, ffmpeg imports `ffmpeg-core.worker.js` and thus
* makes multi-threaded core work. * makes multi-threaded core work.