是否可以直接向 Puppeteer 获取 Puppeteer 音频馈送和/或输入音频? [英] Possible to Get Puppeteer Audio Feed and/or Input Audio Directly to Puppeteer?

查看:103
本文介绍了是否可以直接向 Puppeteer 获取 Puppeteer 音频馈送和/或输入音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 WAV 或 MP3 作为麦克风输入 puppeteer,但是在无头模式下,应用程序被静音,所以我想知道是否有办法直接将输入输入到浏览器中.

I want to input WAV or MP3 into puppeteer as a microphone, however while in headless the application is muted, so I was wondering if there was a way to get input directly into the browser.

我还想知道是否可以在无头状态下从浏览器获取音频源,和/或录制音频并将其放在文件夹中.

I am also wondering if it's possible to get a feed of audio from the browser while in headless, and/or record the audio and place it in a folder.

推荐答案

我最终使用了这个解决方案.首先,我为 Chromium 启用了一些选项:

I ended up using this solution. First, I enabled some options for Chromium:

const browser = await puppeteer.launch({
  args: [
    '--use-fake-ui-for-media-stream',
  ],
  ignoreDefaultArgs: ['--mute-audio'],
});

如果 Chromium 已经打开,请记住关闭它.

Remember to close Chromium if it is open already.

我创建了一个 元素,其中 src 设置为我想要输入的文件.我还覆盖了 navigator.mediaDevices.getUserMedia,以便它返回一个 Promise 与音频文件相同的流,而不是来自麦克风.

I created an <audio> element with the src set to the file that I wanted to input. I also overrode navigator.mediaDevices.getUserMedia, so that it returns a Promise with the same stream as the audio file, rather than from the microphone.

const page = await browser.newPage();
await page.goto('http://example.com', {waitUntil: 'load'});
await page.evaluate(() => {
  var audio = document.createElement("audio");
  audio.setAttribute("src", "http://example.com/example.mp3");
  audio.setAttribute("crossorigin", "anonymous");
  audio.setAttribute("controls", "");
  audio.onplay = function() {
    var stream = audio.captureStream();
    navigator.mediaDevices.getUserMedia = async function() {
       return stream;
    };
  });
  document.querySelector("body").appendChild(audio);
});

现在每当网站的代码调用 navigator.mediaDevices.getUserMedia 时,它都会从文件中获取音频流.(您需要先确保音频正在播放.)

Now whenever the website's code calls navigator.mediaDevices.getUserMedia, it will get the audio stream from the file. (You need to make sure the audio is playing first.)

这篇关于是否可以直接向 Puppeteer 获取 Puppeteer 音频馈送和/或输入音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆