|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useI18n } from 'vue-i18n'; |
| 3 | +import { ref } from 'vue'; |
| 4 | +import { FFmpeg } from '@ffmpeg/ffmpeg'; |
| 5 | +
|
| 6 | +import ffmpegClassWorkerUrl from '@ffmpeg/ffmpeg/worker?worker&url'; |
| 7 | +
|
| 8 | +import { toBlobURL } from '@ffmpeg/util'; |
| 9 | +
|
| 10 | +const { t } = useI18n(); |
| 11 | +
|
| 12 | +const logs = ref<string[]>([]); |
| 13 | +
|
| 14 | +const ffmpeg = new FFmpeg(); |
| 15 | +ffmpeg.on('log', ({ message }) => { |
| 16 | + logs.value.push(message); |
| 17 | +}); |
| 18 | +
|
| 19 | +const loading = ref(false); |
| 20 | +const error = ref(''); |
| 21 | +const loop = ref('5'); |
| 22 | +
|
| 23 | +async function loadFFmpeg() { |
| 24 | + if (!ffmpeg.loaded) { |
| 25 | + const baseURL = 'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.10/dist/esm'; |
| 26 | +
|
| 27 | + await ffmpeg.load({ |
| 28 | + coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'), |
| 29 | + wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'), |
| 30 | + classWorkerURL: ffmpegClassWorkerUrl, |
| 31 | + }); |
| 32 | + } |
| 33 | +} |
| 34 | +
|
| 35 | +async function onFileUploaded(gifFile: File) { |
| 36 | + loading.value = true; |
| 37 | + error.value = ''; |
| 38 | +
|
| 39 | + try { |
| 40 | + await loadFFmpeg(); |
| 41 | +
|
| 42 | + const inputName = 'input.gif'; |
| 43 | + const outputName = 'output.mp4'; |
| 44 | +
|
| 45 | + const buffer = new Uint8Array(await gifFile.arrayBuffer()); |
| 46 | + if (!buffer) { |
| 47 | + return; |
| 48 | + } |
| 49 | + ffmpeg.writeFile(inputName, buffer); |
| 50 | +
|
| 51 | + await ffmpeg.exec([ |
| 52 | + '-stream_loop', loop.value, |
| 53 | + '-i', inputName, |
| 54 | + '-movflags', 'faststart', |
| 55 | + '-pix_fmt', 'yuv420p', |
| 56 | + '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2', |
| 57 | + outputName, |
| 58 | + ]); |
| 59 | +
|
| 60 | + const data = await ffmpeg.readFile(outputName); |
| 61 | + const blob = new Blob([data], { type: 'video/mp4' }); |
| 62 | + const url = URL.createObjectURL(blob); |
| 63 | +
|
| 64 | + const a = document.createElement('a'); |
| 65 | + a.href = url; |
| 66 | + a.download = `${gifFile.name.replace(/\.gif$/, '')}.mp4`; |
| 67 | + a.click(); |
| 68 | +
|
| 69 | + URL.revokeObjectURL(url); |
| 70 | + } |
| 71 | + catch (err: any) { |
| 72 | + error.value = err.toString(); |
| 73 | + } |
| 74 | + finally { |
| 75 | + loading.value = false; |
| 76 | + } |
| 77 | +} |
| 78 | +</script> |
| 79 | + |
| 80 | +<template> |
| 81 | + <div> |
| 82 | + <n-space justify="center" mb-1> |
| 83 | + <n-form-item :label="t('tools.gif-to-mp4.texts.label-stream-loop')" label-placement="left"> |
| 84 | + <n-input-number-i18n v-model:value="loop" :min="1" /> |
| 85 | + </n-form-item> |
| 86 | + </n-space> |
| 87 | + |
| 88 | + <div style="flex: 0 0 100%"> |
| 89 | + <div mx-auto max-w-600px> |
| 90 | + <c-file-upload |
| 91 | + :title="t('tools.gif-to-mp4.texts.title-drag-and-drop-gif-here-or-click-to-select-a-file')" |
| 92 | + accept=".gif" |
| 93 | + @file-upload="onFileUploaded" |
| 94 | + /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div mt-3 flex justify-center> |
| 99 | + <c-alert v-if="error" type="error"> |
| 100 | + {{ error }} |
| 101 | + </c-alert> |
| 102 | + <n-spin |
| 103 | + v-if="loading" |
| 104 | + size="small" |
| 105 | + /> |
| 106 | + </div> |
| 107 | + |
| 108 | + <c-card :title="t('tools.gif-to-mp4.texts.title-logs')"> |
| 109 | + <pre>{{ logs.join('\n') }}</pre> |
| 110 | + </c-card> |
| 111 | + </div> |
| 112 | +</template> |
0 commit comments