17

是否可以从 HTML5 中的 mp4 或 webm 视频元素中获取音频?

我正在尝试使用https://github.com/corbanbrook/dsp.js来计算音频的 FFT,但是,我只有 mp4 和 webm 视频。

4

2 回答 2

23

不确定这是否能回答您的问题,但您可以通过 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

我只是用视频元素替换了音频元素。

于 2013-03-08T04:27:28.703 回答
-1

Webm 用作音频源。

<audio controls="controls" class="full-width" preload="metadata">
  <source src="//rack.international/samples/sample.webm" type="audio/mpeg">
</audio>

<video src="https://rack.international/samples/sample.webm" controls>
  <p>Your browser doesn't support HTML5 video. Here is a <a href="rabbit320.webm">link to the video</a> instead.</p> 
</video>

于 2017-03-21T17:38:15.707 回答