Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming a page's audio with puppeteer

Let's assume I go to a page where audio gets played. Is there any way to expose and capture the audio stream coming from this puppeteer page? I don't want to simply save it but instead have access to the stream which I can constantly pipe throughout other stuff (for example to a server using the discord api)

There's already a similar issue on the official GitHub repo: startScreencast feature?

But I simply want to have access to the audio pipeline of the page instance (preferably a stream). Is there any way to accomplish this?

like image 325
Simon Avatar asked Apr 13 '19 11:04

Simon


2 Answers

I had the same problem and therefore developed a npm package puppeteer-stream. Be aware that this will only work in headful mode, because extension aren't supported in headless mode. I also made an example how you can stream to discord.

like image 160
Flam3rboy Avatar answered Oct 17 '22 10:10

Flam3rboy


You can run code in the target page like so:

await page.evaluate(() => {
  var audio = document.getElementById('audio');

  audio.onplay = function() {
    // Set the source of one <audio> element to be a stream from another.
    var stream = audio.captureStream();
    // This is your audio stream object...
  };
});

More info:

page.evaluate() spec

Capture a MediaStream From a Canvas, Video or Audio Element

like image 20
Jonathan Martins Avatar answered Oct 17 '22 11:10

Jonathan Martins