oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.18k stars 2.77k forks source link

Ffmpeg Not Downloading File in Root Directory #5523

Open Jakisundays opened 1 year ago

Jakisundays commented 1 year ago

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Darwin 21.4.0 x86_64 i386

What steps can reproduce the bug?

The processChunk function, which takes an audio chunk in the form of a Blob and an index i. This function processes the audio chunk and is expected to save it as an MP3 file in a specified public directory.

export const processChunk = async (
  chunkBlob: Blob,
  i: number
): Promise<string> => {
  // Convert the audio chunk Blob to an ArrayBuffer
  const audioArrayBuffer = await chunkBlob.arrayBuffer();

  // Generate a unique output file name with the index
  const outputFileName = `audio_${uniqid()}_${i}.mp3`;

  // Create a Promise to handle the Ffmpeg processing
  return new Promise((resolve, reject) => {
    // Create a readable stream from the ArrayBuffer
    const audioStream = new Readable({
      read() {
        const buffer = Buffer.from(audioArrayBuffer);
        this.push(buffer);
        this.push(null);
      },
    });

    // Use Ffmpeg to process the audio
    Ffmpeg(audioStream)
      .toFormat("mp3")
      .on("start", (commandLine: any) => {
        console.log("FFmpeg started with command: " + commandLine);
      })
      .on("error", (err: any) => {
        console.log("An error occurred: " + err.message);
        reject(err);
      })
      .on("end", () => {
        console.log(`Processing completed for ${outputFileName}`);
        resolve(outputFileName);
      })
      .save(outputFileName); // Save the processed audio as an MP3 file
  });
};

What is the expected behavior?

When executed within the Bun runtime environment, the Ffmpeg process should effectively process the audio chunk and seamlessly save it as an MP3 file in the root directory, mirroring its functionality in a Node.js environment.

What do you see instead?

In the Bun runtime environment, the Ffmpeg process starts without errors, but it doesn't complete. It neither downloads nor saves the audio file in the root directory. The process hangs and never triggers the expected end event. This is in contrast to the Node.js environment, where the code functions correctly, processing and saving the audio file as expected.

Additional information

The code was tested with a clean installation of Ffmpeg via Brew. The "fluent-ffmpeg" package is updated to the newest version ("^2.1.2").

haibuiorg commented 1 year ago

@Jakisundays I faced the same issue. I think it is related to the audioStream as Readable. I fixed it by write it to a temp file using await Bun.write("temp.mp4", data) then pass the temp.mp4 to ffmpeg as Input