How to fix 'Error: Write EOF' when running a nodejs file which spawns a child process running ffmpeg?

I am new to nodejs scripting and I am trying to create a program which receives a video file as a buffer and creates a thumbnail from it. This is my code:

create_thumbnail.js:

const { Readable } = require('stream');
const child_process = require('node:child_process');

async function generate_thumbnail(videoFile, callback) {

    const readable = new Readable();
    readable._read = () => {}
    readable.push(videoFile.data)
    readable.push(null)
    const ffmpeg = child_process.spawn('C:\\FFmpeg\\bin\\ffmpeg', ['-i', 'pipe:0', '-vframes', '1', '-vf', 'scale=iw*.5:ih*0.5', '-f', 'mjpeg', 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] })
    readable.pipe(ffmpeg.stdin);

    const buffer = [];

    ffmpeg.stdout.on('data', (data) => {
        buffer.push(data);
    });

    const thumbnail = ffmpeg.stdout.on('end', () => {
        console.log('There will be no more data.');
        var buf = Buffer.concat(buffer);
        callback (buf);
    });
}

module.exports = { generate_thumbnail };

The program is creating a thumbnail successfully for a video which has a few seconds length. It is failing for larger videos with the following error:

node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: write EOF
    at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:98:16)
Emitted 'error' event on Socket instance at:
    at Socket.onerror (node:internal/streams/readable:773:14)
    at Socket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -4095,
  code: 'EOF',
  syscall: 'write'

Can someone please let me know the reason for getting this error. How can I fix it?

Upvotes: 2

Views: 1479

Answers (0)

Related Questions