Home > Net >  How do I access ffmpeg stdout as a buffer stream in node js
How do I access ffmpeg stdout as a buffer stream in node js

Time:10-15

I'm trying to access the output of an ffmpeg command (run with spawn in nodeJS) so that I can store the processed video in a buffer.

I've tried the following code:

(videoBuffer = [Unit8Array])

let arr = [];

let ffmpegSpawn = spawn(ffmpegPath, [
    "-i",
    "-",
    "-filter:v",
    "crop=500:500:50:50",
    "-preset",
    "fast",
    "-f",
    "mp4",
    "-",
  ]);

  ffmpegSpawn.stdin.write(videoBuffer);
  ffmpegSpawn.stdin.end();

  ffmpegSpawn.stdout.on("data", (chunk) => {
    console.log(chunk.toString());
    arr.push(chunk)
  });

  ffmpegSpawn.stderr.on("error", (err) => {
    console.log(err);
  });

  ffmpegSpawn.stdout.on("close", () => {
    console.log(arr)
  });

No error is being produced but I can't see any output in the console other than an empty array.

The ffmpeg command is successfully receiving the input via child_process.stdin and the [ "-i", "-" ...] args because I'm able to successfully create a test .mp4 file by passing the following args into ffmpeg (using spawn) :

[
    "-i",
    "-",
    "-filter:v",
    "crop=500:500:50:50",
    "-preset",
    "fast",
    "./desktop/testVideo.mp4"
  ]

I've tried accessing the child_process directly with:

ffmpegSpawn.on("data", (chunk) => { arr.push(chunk); console.log(chunk.toString()})

but nothing is seen on the console.

Q: How do I access ffmpegSpawn.stdout ? As no error is being produced, I am assuming that the arguments to ffmpeg are valid.

Why is the array empty ?

Any help would be much appreciated.

CodePudding user response:

It's stuck on ffmpegSpawn.stdin.write(videoBuffer); Move on.... lines above the write. You need to set up the stdout events before sending in the first byte to stdin. Currently, there is a deadlock after the first few frames are written to stdin. Node.js is trying to send more frames but FFmpeg won't take them until its output data is read from stdout.

Another thing that'll bite you. You cannot use mp4 container for piping the media data. MP4 requires the muxer (writer) to be able to seek the file, which you cannot do with pipe. Use alternate format. I know -f matroska would work.

  • Related