Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spawn in Bun Fails to Properly Handle FFmpeg Custom Pipes (e.g., pipe:3) #17989

Open
pxgo opened this issue Mar 8, 2025 · 0 comments
Open

spawn in Bun Fails to Properly Handle FFmpeg Custom Pipes (e.g., pipe:3) #17989

pxgo opened this issue Mar 8, 2025 · 0 comments
Labels
bug Something isn't working node:child_process

Comments

@pxgo
Copy link

pxgo commented Mar 8, 2025

What version of Bun is running?

1.2.4

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What steps can reproduce the bug?

const { spawn } = require('child_process');

class ExecModule {
  run(app, args, callback) {
    return new Promise((resolve, reject) => {
      const child = spawn(
        app,
        args.map((i) => i.toString())
      );
      const stdoutChunks = [];
      const stderrChunks = [];

      child.stdout?.on('data', (chunk) => {
        stdoutChunks.push(chunk);
        if (callback) callback(chunk);
      });

      child.stderr?.on('data', (chunk) => {
        stderrChunks.push(chunk);
      });

      child.on('close', (code) => {
        const stdout = Buffer.concat(stdoutChunks);
        const stderr = Buffer.concat(stderrChunks).toString('utf8');

        if (code === 0) {
          resolve(stdout);
        } else {
          reject(
            new Error(
              `Process exited with code ${code}\n` + `[STDERR] ${stderr.trim()}`
            )
          );
        }
      });

      child.on('error', (err) => {
        reject(
          new Error(
            `Process failed to start: ${err.message}\n` +
            `[STDERR] ${Buffer.concat(stderrChunks).toString('utf8').trim()}`
          )
        );
      });
    });
  }
}

class AudioModule {
  sampleRate = 44100;
  format = 'mp3';
  bitrate = '128k';
  async getMuteBufferForever(callback) {
    return await execModule.run(
      'ffmpeg',
      [
        '-re',
        '-f',
        'lavfi',
        '-i',
        'anullsrc=cl=stereo:r=44100',
        '-ac',
        '2',
        '-ar',
        this.sampleRate,
        '-b:a',
        this.bitrate,
        '-f',
        this.format,
        'pipe:1',
      ],
      callback
    );
  }
}

const audioModule = new AudioModule();
const execModule = new ExecModule();

async function main() {

  const ffmpeg = spawn(
    'ffmpeg',
    ['-re', '-i', 'pipe:3', '-f', 'mp3', 'pipe:1'],
    {
      stdio: ['pipe', 'pipe', 'pipe', 'pipe'],
    }
  );

  ffmpeg.stdout.on('data', (data) => {
    console.log('stdout', data);
  });
  ffmpeg.stderr.on('data', (data) => {
    console.error(`FFmpeg Error:`);
    console.log(data.toString());
  });
  ffmpeg.on('close', (code) => {
    console.log('close', code);
  });
  ffmpeg.on('error', (err) => {
    console.log('error', err);
  });

  audioModule.getMuteBufferForever((buffer) => {
    ffmpeg.stdio[3].write(buffer);
  });
}
main().catch(console.error);

What is the expected behavior?

The following code will be triggered repeatedly:

ffmpeg.stdout.on('data', (data) => {
  console.log('stdout', data);
});

Console output:

Image

What do you see instead?

The following code is not triggered:

ffmpeg.stdout.on('data', (data) => {
  console.log('stdout', data);
});

Additional information

Problem Description

When using Bun's child_process.spawn to invoke FFmpeg, I observed that when FFmpeg parameters specify a custom input pipe (e.g., -i pipe:3) and data is written to the corresponding stdio[3], FFmpeg does not receive the data, resulting in no output and potential process blocking. However, the same code works as expected in the Node.js environment. This suggests that Bun's spawn implementation may have an issue handling additional pipes beyond the standard stdin, stdout, and stderr.

Bun vs NodeJS(v20.14.0)

Bun:

Image

NodeJS:

Image

@pxgo pxgo added bug Something isn't working needs triage labels Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node:child_process
Projects
None yet
Development

No branches or pull requests

2 participants