fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.63k stars 873 forks source link

Multer buffer to fluent-ffmpeg and output as a buffer #1189

Open NickBrinsmade opened 1 year ago

NickBrinsmade commented 1 year ago

Version information

Code to reproduce

const stream = require('stream');
const bufferStream = new stream.PassThrough()
    const buffers = []
    bufferStream
      .on('start', () => {
        console.log('Pass-through stream has started')
      })
      .on('data', function (buf) {
        console.log('bufferStream data', buf)
        buffers.push(buf)
      })
      .on('end', async function () {
        const outputBuffer = Buffer.concat(buffers)
        console.log(outputBuffer)
        const result = await uploadFile(outputBuffer, 'insane', bucketNameImages);
        console.log('Done preparing bufferStream')
      })

    const inputStream = stream.Readable.from(request.file.buffer.toString()); // request.file.buffer is a multer buffer
    ffmpeg(inputStream)
      .fps(25)
      .size('720x?')
      .on('start', (commandLine) => {
        console.log('ffmpeg started conversion', commandLine)
      })
      .on('stderr', function (stderrLine) {
        console.error('Stderr output: ' + stderrLine)
      })
      .on('error', function (err) {
        console.error('ffmpeg-error', err)
      })
      .on('progress', function (progress) {
        console.log('ffmpeg-output', Math.round(progress.percent))
      })
      .on('end', function () {
        console.log('Formating finished!')
      })
      .writeToStream(bufferStream)

Expected results

Take in the multer video file buffer, convert it to a stream, apply the changes to the video, and convert it back to a buffer to pass into my S3 upload function.

Observed results

ffmpeg started conversion ffmpeg -i pipe:0 -r 25 -filter:v scale=w=720:h=trunc(ow/a/2)*2 pipe:1 2022-09-02T00:33:02.454407+00:00 app[web.1]: Stderr output: ffmpeg version N-62475-g130d19bf20-static https://johnvansickle.com/ffmpeg/ Copyright (c) 2000-2022 the FFmpeg developers 2022-09-02T00:33:02.454672+00:00 app[web.1]: Stderr output: built with gcc 8 (Debian 8.3.0-6) 2022-09-02T00:33:02.454815+00:00 app[web.1]: Stderr output: configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg 2022-09-02T00:33:02.454858+00:00 app[web.1]: Stderr output: libavutil 57. 30.100 / 57. 30.100 2022-09-02T00:33:02.454883+00:00 app[web.1]: Stderr output: libavcodec 59. 39.100 / 59. 39.100 2022-09-02T00:33:02.454903+00:00 app[web.1]: Stderr output: libavformat 59. 29.100 / 59. 29.100 2022-09-02T00:33:02.454925+00:00 app[web.1]: Stderr output: libavdevice 59. 8.101 / 59. 8.101 2022-09-02T00:33:02.454944+00:00 app[web.1]: Stderr output: libavfilter 8. 46.100 / 8. 46.100 2022-09-02T00:33:02.454967+00:00 app[web.1]: Stderr output: libswscale 6. 8.101 / 6. 8.101 2022-09-02T00:33:02.454993+00:00 app[web.1]: Stderr output: libswresample 4. 8.100 / 4. 8.100 2022-09-02T00:33:02.455019+00:00 app[web.1]: Stderr output: libpostproc 56. 7.100 / 56. 7.100 2022-09-02T00:33:02.460228+00:00 app[web.1]: Stderr output: [mov,mp4,m4a,3gp,3g2,mj2 @ 0x5ad0b00] moov atom not found 2022-09-02T00:33:02.460281+00:00 app[web.1]: Stderr output: pipe:0: Invalid data found when processing input 2022-09-02T00:33:02.460674+00:00 app[web.1]: Stderr output: 2022-09-02T00:33:02.461375+00:00 app[web.1]: 2022-09-02T00:33:02.485567+00:00 app[web.1]: ffmpeg-error Error: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input 2022-09-02T00:33:02.485569+00:00 app[web.1]: 2022-09-02T00:33:02.485569+00:00 app[web.1]: at ChildProcess. (/app/node_modules/fluent-ffmpeg/lib/processor.js:182:22) 2022-09-02T00:33:02.485570+00:00 app[web.1]: at ChildProcess.emit (events.js:400:28) 2022-09-02T00:33:02.485570+00:00 app[web.1]: at ChildProcess.emit (domain.js:470:12) 2022-09-02T00:33:02.485571+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12) 2022-09-02T00:33:02.532553+00:00 app[web.1]: Done preparing bufferStream

NickBrinsmade commented 1 year ago

I also tried this even simpler implementation, but get 'Error: Error: ffmpeg exited with code 1: pipe:1: Invalid argument'. The 'on' events are never hit in any of these examples, not sure why....

app.post('/create', upload.single('video'), async function (request, response, next) { let data = request.body console.log(data) // prints [Object: null prototype] { param1: '' } let bufferStream = new stream.PassThrough(); console.log(request.file.buffer) // prints '<Buffer 00 00 00 14 66 74 79 70 71 74 20 20 00 00 00 00 71 74 20 20 00 00 00 08 77 69 64 65 01 0e 28 55 6d 64 61 74 21 20 03 40 68 1c 21 4e ff 3c 59 96 7c 82 ... 17718642 more bytes>'

new ffmpeg({ source: stream.Readable.from(request.file.buffer, { objectMode: false }) }) .on('error', function (err) { console.log(Error: ${err}) }) .on('progress', function (progress) { console.log("progress") }) .on('end', function () { console.log('Formatting finished!'); console.log("after"); }) .writeToStream(bufferStream);

// Read the passthrough stream const buffers = []; bufferStream.on('data', function (buf) { buffers.push(buf); }); bufferStream.on('end', function () { const outputBuffer = Buffer.concat(buffers); // use outputBuffer }); console.log("Added.") response.send("Success") });

piercus commented 1 year ago

@NickBrinsmade did you try with beamcoder ?

lishenglll commented 10 months ago

I have also encountered a similar problem. I want to output the video file as binary data, write it to the stream, and then send it to the main process. This is an Electron project, but the output has never been successful