无论如何,是否可以使用网络音频API可视化来自iframe的youtube音频?

wil*_*137 5 javascript youtube api audio iframe

是否可以收听iframe中的youtube视频的音频,然后对其进行分析以用于基于Web音频api的可视化器?

从制作网站的方式来看,我只能从iframe获取源网址。这是我的一个iframe的示例:

<iframe id="youtube-player" type="text/html"  width="500" height="281" src="https://www.youtube.com/embed/JlhTiynRiXA?feature=oembed" frameborder="0" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)

kan*_*ano 9

希望这对将来的Google员工有所帮助。

我发现做到这一点的唯一方法是使用音频流库(如Node的youtube-audio-stream)并从服务器端缓冲/管道音频。

var express = require('express');
var router = express.Router();

var youtubeStream = require('youtube-audio-stream');

router.get('/stream/:videoId', function (req, res) {
    try {
        youtubeStream(req.params.videoId).pipe(res);
    } catch (exception) {
        res.status(500).send(exception)
    }
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据它创建audioContext。AudioContext是使用WebGL或canvas(例如pixi.js)进行可视化所需的魔术关键字。

因此在前端:

function loadSound() {
  var request = new XMLHttpRequest();
  request.open("GET", "http://localhost:8000/stream/nl6OW07A5q4", true); 
  request.responseType = "arraybuffer"; 

  request.onload = function() {
      var Data = request.response;
      process(Data);
  };

  request.send();
}

function process(Data) {
  source = context.createBufferSource(); // Create Sound Source
  context.decodeAudioData(Data, function(buffer){
    source.buffer = buffer;
    source.connect(context.destination); 
    source.start(context.currentTime);
}) 
Run Code Online (Sandbox Code Playgroud)

从那里开始,应该很容易地将多个音频可视化库中的任何一个应用于上下文。

来自http://www.willvillanueva.com/the-web-audio-api-from-nodeexpress-to-your-browser/的基本前端示例

编辑:存档链接 https://web.archive.org/web/20170715041035/http://www.willvillanueva.com/the-web-audio-api-from-nodeexpress-to-your-browser/