node.js fluent-ffmepg with createWriteStream fails

2.6k views Asked by At

I am trying to use fluent-ffmpeg node package with createReadStream and createWriteStream.

However, if I replace this line:

  .output(fs.createWriteStream('assets/transcode-video.mp4'), {end: true})

I write this

  .output('assets/transcode-video.mp4', {end: true})

it works. But I literally followed the documentation. I appreciate any help or hint.

const fs = require('fs');
const ffmpeg = require("fluent-ffmpeg");
const logger = console;

const options = {
  timeout: 60, // seconds
  logger
};

ffmpeg(fs.createReadStream('assets/video.webm'), options)
  .output(fs.createWriteStream('assets/transcode-video.mp4'), {end: true})
  .on('end', () => console.log('Finished processing'))
  .run();

Error:

> node src/index.js

events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: ffmpeg exited with code 1: pipe:1: Invalid argument

My repository for complete code

2

There are 2 answers

2
Josias da Paixao junior On

In the .output(fs.createWriteStream('assets/transcode-video.mp4'), {end: true}) remove fs.write(), as it is unnecessary, ffmpeg will create the output file without the need you do it manually.

Your code will look like this:

const fs = require ('fs');
const ffmpeg = require ("fluent-ffmpeg");
const logger = console;

const options = {
  timeout: 60, // seconds
  logger
};

ffmpeg (fs.createReadStream ('out.mp3'), options)
  .output ('transcode-video.mp4', {end: true})
  .on ('start', (a) => console.log (a))
  .on ('end', () => console.log ('Processing completed'))
  .run();
0
Josias da Paixao junior On

It does not work with createWriteStream, as fluent-ffmpeg is "converting" everything to ffmpeg syntax. This syntax requires that the output be a string