是否可以从 HTML5 中的 mp4 或 webm 视频元素获取音频?
我正在尝试使用 https://github.com/corbanbrook/dsp.js计算音频的 FFT,但是,我只有 mp4 和 webm 视频。
最佳答案
不确定这是否回答了您的问题,但您可以通过 Web Audio API 节点图运行视频的音频。下面的代码可以通过改变增益和滤波器参数来演示。我只在 Chrome 上测试过。
<!DOCTYPE html>
<meta charset="UTF-8">
<title></title>
<body>
</body>
<script>
var video = document.createElement("video");
video.setAttribute("src", "ourMovie.mov");
video.controls = true;
video.autoplay = true;
document.body.appendChild(video);
var context = new webkitAudioContext();
var gainNode = context.createGain();
gainNode.gain.value = 1; // Change Gain Value to test
filter = context.createBiquadFilter();
filter.type = 2; // Change Filter type to test
filter.frequency.value = 5040; // Change frequency to test
// Wait for window.onload to fire. See crbug.com/112368
window.addEventListener('load', function(e) {
// Our <video> element will be the audio source.
var source = context.createMediaElementSource(video);
source.connect(gainNode);
gainNode.connect(filter);
filter.connect(context.destination);
}, false);
</script>
上面的代码是这个的修改版本: http://updates.html5rocks.com/2012/02/HTML5-audio-and-the-Web-Audio-API-are-BFFs
我只是用视频元素替换了音频元素。
关于javascript - 从 html5 视频中获取音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118524/