|
3 | 3 | Learn the basics of using ffmpeg.wasm.
|
4 | 4 |
|
5 | 5 | :::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. |
8 | 7 | :::
|
9 | 8 |
|
10 |
| -## Basic |
| 9 | +## Transcoding AVI video to MP4 video |
11 | 10 |
|
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); |
13 | 72 |
|
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 | + } |
17 | 87 |
|
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 | + } |
19 | 99 |
|
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 | +} |
32 | 113 | ```
|
0 commit comments