我正在尝试使用以下方法创建视频可视化(最小代码只是为了查看可能性):
HTML:
<canvas id=canvas width=427 height=240></canvas>
JS:
var context = new webkitAudioContext()
, analyser = context.createAnalyser()
, video = document.createElement('video')
, source = context.createMediaElementSource(video)
, canvas = document.getElementById('canvas')
, ctx = canvas.getContext('2d')
;
var loadVideos = function() {
video.src = 'my video.ogg';
source.connect(analyser);
analyser.connect(context.destination);
video.play();
videoVisualization();
}
loadVideos();
function videoVisualization() {
window.webkitRequestAnimationFrame(videoVisualization);
ctx.drawImage(this.video, 0, 0, canvas.width, canvas.height);
var freqByteData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(freqByteData);
for (i = 0; i < freqByteData.length; ++i) {
console.log(freqByteData[5]);
}
}
如果我将其添加到:
,这可以轻松完成<video id=video controls width=427 height=240>
<source src="my video.ogg" type="audio/ogg" />
</video>
特别是当我使用scriptProcessor Node获取音频数据时,我想动态更改视频源并且视频标签不支持它。
提前致谢,