Skip to content

Instantly share code, notes, and snippets.

@dmoosocool
Created March 25, 2021 14:59
Show Gist options
  • Save dmoosocool/39556d812887d35f1648405b4bdcfbd1 to your computer and use it in GitHub Desktop.
Save dmoosocool/39556d812887d35f1648405b4bdcfbd1 to your computer and use it in GitHub Desktop.
audio.mp3 concat to video.webm, use ffmpeg.wasm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>audio.mp3 concat to video.webm</title>
</head>
<body>
<script src="https://unpkg.com/@ffmpeg/ffmpeg@0.9.5/dist/ffmpeg.min.js"></script>
<script>
const { createFFmpeg, fetchFile } = FFmpeg;
const ffmpeg = createFFmpeg({log: true});
(async ()=>{
await ffmpeg.load();
console.log('ffmpeg loaded.');
await ffmpeg.FS('writeFile', 'audio.mp3', await fetchFile('./assets/audio.mp3'));
console.log('audio.mp3 is writed.');
// ffmpeg -i pc.wav -ar 48000 -ac 2 -acodec libopus -ab 256k man.opus
await ffmpeg.run('-i', 'audio.mp3', '-ar', '48000', '-ac', '2', '-acodec', 'libopus', '-ab', '256k', 'audio.opus');
console.log('audio.mp3 is coverd to audio.opus');
await ffmpeg.FS('writeFile', 'video.webm', await fetchFile('./assets/video.webm'));
console.log('video.webm is writed.');
// ffmpeg -y -i vFilePath -i aFilePath -map 0:0 -map 1:0 -c copy oFilename
await ffmpeg.run('-y', '-i', 'video.webm', '-i', 'audio.opus', '-map', '0:0', '-map', "1:0", '-c', 'copy', 'output.webm');
console.log('output.web is covered.');
const data = ffmpeg.FS('readFile', 'output.webm');
const video = document.createElement('video');
video.src = URL.createObjectURL(new Blob([data], {type:'video/webm'}));
video.controls = true;
video.play().then(()=>{console.log('video is plat')}).catch(e=>{console.log(e.message)});
document.body.appendChild(video);
console.log(data);
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment