Created
November 18, 2020 20:52
-
-
Save CTimmerman/a96d46c157bcbf8fda0c51fd8b91b645 to your computer and use it in GitHub Desktop.
GIF Maker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://fireship.io/lessons/wasm-video-to-gif/ | |
// formatted using prettier --write "src/**/*.{js,jsx}" | |
import React, { useState, useEffect } from 'react'; | |
import './App.css'; | |
import { createFFmpeg, fetchFile } from '@ffmpeg/ffmpeg'; | |
const ffmpeg = createFFmpeg({ log: true }); | |
function App() { | |
const [ready, setReady] = useState(false); | |
const [video, setVideo] = useState(); | |
const [gif, setGif] = useState(); | |
const load = async () => { | |
await ffmpeg.load(); | |
setReady(true); | |
}; | |
useEffect(() => { | |
load(); | |
}, []); | |
const convertToGif = async () => { | |
ffmpeg.FS('writeFile', 'test.mp4', await fetchFile(video)); | |
await ffmpeg.run( | |
'-i', | |
'test.mp4', | |
'-t', | |
'2.5', | |
'-ss', | |
'2.0', | |
'-f', | |
'gif', | |
'out.gif', | |
); | |
const data = ffmpeg.FS('readFile', 'out.gif'); | |
const url = URL.createObjectURL( | |
new Blob([data.buffer], { type: 'image/gif' }), | |
); | |
setGif(url); | |
}; | |
return ready ? ( | |
<div className="App"> | |
{video && ( | |
<video controls width="250" src={URL.createObjectURL(video)}></video> | |
)} | |
<input type="file" onChange={(e) => setVideo(e.target.files?.item(0))} /> | |
<h3>Result</h3> | |
<button onClick={convertToGif}>Convert</button> | |
{gif && <img src={gif} width="250" />} | |
</div> | |
) : ( | |
<p>Loading...</p> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment