2

I'm trying to build a service on a linux server to record video a web browser with its sound.

The first step I did with the source code below (using xvfb, puppeteer and ffmpeg) to record successfully.

However when I do a recording with different processes at a same time (different websites) the audio gets mixed up between the processes. I know this is happening because I used the same default audio output for all processes.

The question is: how can I record the browser with sound that doesn't get mixed up between different processes?

My sample code below:

var Xvfb = require('xvfb');
var puppeteer = require('puppeteer');
const { spawn, spawnSync } = require('child_process');

async function record() {

    var xvfb = new Xvfb({
        displayNum: 99,
        reuse: false,
        xvfb_args: [
            "-screen", "0", "1920x1080x24",
            "-ac",
            "-nolisten", "tcp",
            "-dpi", "96",
            "+extension", "RANDR"
        ]
    });

    xvfb.startSync();

    var browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
        ignoreDefaultArgs: [
            "--mute-audio",
            "--enable-automation"
        ],
        args: [
            "--use-fake-ui-for-media-stream",
            "--window-size=1920,1080",
            "--start-fullscreen"
        ]
    });
    const page = await browser.newPage();

    var url = "http://www.noiseaddicts.com/free-samples-mp3/?id=2544";
    await page.goto(url);
    await page.click('span.map_play');

    var time = new Date().getTime();

    var options = [
        "-video_size", "1920x1080",
        "-framerate", "30",
        "-f", "x11grab",
        "-draw_mouse", "0",
        "-i", ":99",

        "-f", "pulse",
        "-ac", "2",
        "-i", "1",
        "./output" + time + ".mkv"
    ];

    var cmd = 'ffmpeg';

    var proc = spawn(cmd, options);

    proc.stdout.on('data', function (data) {
        console.log(data);
    });

    proc.stderr.setEncoding("utf8")
    proc.stderr.on('data', function (data) {
        console.log(data);
    });

    proc.on('close', async function () {
        console.log('finished');
        xvfb.stopSync();
    });
}

record();
ggorlen
  • 44,755
  • 7
  • 76
  • 106
NA GUY
  • 21
  • 2
  • I'm not very familiar with PulseAudio so don't know the exact solution should be but I think you'd probably want to create different virtual audio sinks with `pacmd` and then use the right one as the input `"-i", "[sink]" `. Not sure how to tell chrome which sink to use as the output though. – Petr Bela Oct 07 '21 at 23:59

1 Answers1

1

use puppeteer-stream. and run examples/ffmpeg.js with xfvb-run --auto-servernum node ffmpeg.js you will see the captured output.

Murat Cem YALIN
  • 317
  • 1
  • 6