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