|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>MP3 Resampler</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <h1>MP3 Resampler</h1> |
| 9 | + <input type="file" id="audioFile" accept=".mp3"><br><br> |
| 10 | + <label>Target Sample Rate (Hz): </label> |
| 11 | + <input type="number" id="sampleRate" value="44100"><br><br> |
| 12 | + <button id="resampleBtn">Resample</button> |
| 13 | + <p id="status"></p> |
| 14 | + |
| 15 | + <script type="module"> |
| 16 | + import { createFFmpeg, fetchFile } from 'https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg@0.12.15/dist/ffmpeg.min.js'; |
| 17 | + |
| 18 | + const ffmpeg = createFFmpeg({ log: true }); |
| 19 | + |
| 20 | + document.getElementById('resampleBtn').addEventListener('click', async () => { |
| 21 | + const fileInput = document.getElementById('audioFile'); |
| 22 | + const rateInput = document.getElementById('sampleRate'); |
| 23 | + const status = document.getElementById('status'); |
| 24 | + |
| 25 | + if (!fileInput.files.length) { |
| 26 | + alert('Please select an MP3 file.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const sampleRate = parseInt(rateInput.value, 10); |
| 31 | + if (!sampleRate || sampleRate <= 0) { |
| 32 | + alert('Please enter a valid sample rate.'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const file = fileInput.files[0]; |
| 37 | + status.textContent = 'Loading FFmpeg…'; |
| 38 | + if (!ffmpeg.isLoaded()) { |
| 39 | + await ffmpeg.load(); |
| 40 | + } |
| 41 | + |
| 42 | + status.textContent = 'Processing…'; |
| 43 | + ffmpeg.FS('writeFile', 'input.mp3', await fetchFile(file)); |
| 44 | + await ffmpeg.run('-i', 'input.mp3', '-ar', sampleRate.toString(), 'output.mp3'); |
| 45 | + |
| 46 | + const data = ffmpeg.FS('readFile', 'output.mp3'); |
| 47 | + const url = URL.createObjectURL(new Blob([data.buffer], { type: 'audio/mp3' })); |
| 48 | + |
| 49 | + const link = document.createElement('a'); |
| 50 | + link.href = url; |
| 51 | + link.download = `resampled_${sampleRate}Hz.mp3`; |
| 52 | + link.click(); |
| 53 | + |
| 54 | + status.textContent = 'Done!'; |
| 55 | + }); |
| 56 | + </script> |
| 57 | +</body> |
| 58 | +</html> |
0 commit comments