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

时间:2022-03-29 19:00:36

How could an audio track be extracted from a video in HTML5 Javascript as the raw audio data? I.e. an array of samples?

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

I am completely new to the HTML5 Video API so an example would be great.

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

1 个解决方案

#1


7  

The Web Audio API is exactly what you want. In particular, you want to feed a MediaElementAudioSourceNode into an AnalyserNode. Unfortunately, the Web Audio API is only implemented in Chrome (somewhat implemented in FF), and even Chrome doesn't have full support for MediaElementAudioSourceNode yet.

Web Audio API正是您想要的。特别是,您希望将MediaElementAudioSourceNode提供给AnalyserNode。遗憾的是,Web Audio API仅在Chrome中实现(有些在FF中实现),甚至Chrome也没有完全支持MediaElementAudioSourceNode。

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);

#1


7  

The Web Audio API is exactly what you want. In particular, you want to feed a MediaElementAudioSourceNode into an AnalyserNode. Unfortunately, the Web Audio API is only implemented in Chrome (somewhat implemented in FF), and even Chrome doesn't have full support for MediaElementAudioSourceNode yet.

Web Audio API正是您想要的。特别是,您希望将MediaElementAudioSourceNode提供给AnalyserNode。遗憾的是,Web Audio API仅在Chrome中实现(有些在FF中实现),甚至Chrome也没有完全支持MediaElementAudioSourceNode。

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);