Last active
April 10, 2020 01:13
-
-
Save CreatiCoding/14a6332a6473c7f7aab663bc50d8c695 to your computer and use it in GitHub Desktop.
vue component for gif to mp4 convertor with ffmpeg
This file contains hidden or 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
| <template> | |
| <div id="app"> | |
| <div> | |
| <input type="file" @change="previewFile" /> | |
| <br /> | |
| <img src height="200" alt="이미지 미리보기..." /> | |
| <button @click="doConvert">다운로드</button> | |
| </div> | |
| </div> | |
| </template> | |
| <script> | |
| import { createWorker, setLogging } from "@ffmpeg/ffmpeg"; | |
| // setLogging(true); | |
| export default { | |
| name: "App", | |
| data() { | |
| return { | |
| gif_data: null | |
| }; | |
| }, | |
| methods: { | |
| previewFile() { | |
| var preview = document.querySelector("img"); | |
| var file = document.querySelector("input[type=file]").files[0]; | |
| var reader = new FileReader(); | |
| reader.addEventListener( | |
| "load", | |
| () => { | |
| preview.src = reader.result; | |
| this.gif_data = reader.result; | |
| }, | |
| false | |
| ); | |
| if (file) { | |
| reader.readAsDataURL(file); | |
| } | |
| }, | |
| doConvert() { | |
| const worker = createWorker(); | |
| // const worker = createWorker({ | |
| // logger: ({ message }) => console.log(message) | |
| // }); | |
| (async () => { | |
| await worker.load(); | |
| await worker.write("test.gif", this.gif_data); | |
| await worker.transcode( | |
| "test.gif", | |
| "result.mp4", | |
| " -pix_fmt yuv420p -c:v libx264 -profile:v main -level 3.1" | |
| ); | |
| const { data } = await worker.read("result.mp4"); | |
| const blob = new Blob([data], { type: "octet/stream" }); | |
| const url = window.URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| document.body.appendChild(a); | |
| a.href = url; | |
| a.download = "result.mp4"; | |
| a.click(); | |
| setTimeout(() => { | |
| window.URL.revokeObjectURL(url); | |
| document.body.removeChild(a); | |
| }, 0); | |
| // if can use fs, then | |
| // await fs.writeFileSync("result.mp4", data); | |
| await worker.terminate(); | |
| })(); | |
| } | |
| } | |
| }; | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment