14

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?

Simon
  • 2,686
  • 2
  • 31
  • 43
  • As for now, node.js only accept the following data type for Readable Stream https://nodejs.org/api/stream.html#stream_readable_streams . Or maybe you can read the audio/video file using `fs` – Edi Imanto Nov 03 '19 at 15:53
  • @Edilmanto I don't want to read any _file_ as I said. I'd like to stream it directly without any buffer :) – Simon Nov 04 '19 at 13:07

2 Answers2

4

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

Jonathan Martins
  • 734
  • 7
  • 24
  • That's not what I asked for, I want to record the sound of the entire _page_ - independent of how or what kind of sound is being played. And I want to do it without having access to where the sound comes from (e.g. audio elements, video elements etc...). Certain things are impossible to capture such as blobs etc... that's were my question comes in :) – Simon Sep 01 '20 at 08:02
  • I don't think it's possible with the current version of Puppeteer, but like you said startScreencast feature is on its way. Perhaps this will be possible in the near future! – Jonathan Martins Sep 02 '20 at 00:49
3

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.

Flam3rboy
  • 95
  • 2
  • 7
  • Thank you for the npm package. Could you elaborate in more details, why is audio capturing impossible in headless mode? Is it a limitation of the package? – yaskovdev Mar 03 '22 at 19:11
  • 1
    @yaskovdev https://github.com/Flam3rboy/puppeteer-stream/issues/4 – Flam3rboy Mar 04 '22 at 22:15