如何从HTML5中的视频中提取音频?

时间:2013-07-08 16:35:49

标签: javascript html5 html5-video html5-audio

如何从HTML5 Javascript中的视频中提取音轨作为原始音频数据?即一系列样本?

我是HTML5视频API的新手,所以一个例子会很棒。

1 个答案:

答案 0 :(得分:7)

Web Audio API正是您想要的。特别是,您希望将MediaElementAudioSourceNode提供给AnalyserNode。不幸的是,Web Audio API仅在Chrome中实现(有些在FF中实现),甚至Chrome doesn't have full support for MediaElementAudioSourceNode yet

var context = new webkitAudioContext();

// feed video into a MediaElementSourceNode, and feed that into AnalyserNode
// due to a bug in Chrome, this must run after onload
var videoElement = document.querySelector('myVideo');
var mediaSourceNode = context.createMediaElementSource(videoElement);
var analyserNode = context.createAnalyser();
mediaSourceNode.connect(analyserNode);
analyserNode.connect(context.destination);

videoElement.play();

// run this part on loop to sample the current audio position
sample = new Float32Array(analyser.frequencyBinCount);
analyser.getFloatFrequencyData(sample);