node-fluent-ffmpeg: Question: Output to file works - Output to stream doesn't

Hi there,

this isn’t an issue (I believe & hope), more a question or a behaviour I can’t explain.

I´m trying to convert a mjpeg stream (from a http resource) to a mp4 stream. If I´m using the native ffmpeg output to file method, everything works as expected:

Here is the code:

var ffmpeg = require('fluent-ffmpeg');

ffmpeg()
    .input('http://192.168.178.63/image.jpg')
    .inputOptions('-loop 1')
    .outputOptions('-c:v libx264')
    .outputOptions('-t 3')
    .outputOptions('-pix_fmt yuv420p')
    .outputOptions('-f mp4')
    .output('foo.mp4')
    .on('start', function(commandLine) {
        console.log('Spawned Ffmpeg with command: ' + commandLine);
     })
    .on('error', function(err) {
           console.log('An error occurred: ' + err.message);
    })
    .on('end', function() {
          console.log('Processing finished !');
    })
    .run();

If I´m trying to use a stream as an output, I´m getting the following error:

Spawned Ffmpeg with command: ffmpeg -loop 1 -i http://192.168.178.63/image.jpg -c:v libx264 -t 50 -pix_fmt yuv420p -f mp4 pipe:1
An error occurred: ffmpeg exited with code 1: Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

Here is the code that produces the error:

var fs = require('fs');
var ffmpeg = require('fluent-ffmpeg');

var wms = fs.createWriteStream('1_newtest.mp4');

ffmpeg()
    .input('http://192.168.178.63/image.jpg')
    .inputOptions('-loop 1')
    .outputOptions('-c:v libx264')
    .outputOptions('-t 3')
    .outputOptions('-pix_fmt yuv420p')
    .outputOptions('-f mp4')
    .output(wms, {end: true })
    .on('start', function(commandLine) {
        console.log('Spawned Ffmpeg with command: ' + commandLine);
     })
    .on('error', function(err) {
          console.log('An error occurred: ' + err.message);
    })
    .on('end', function() {
          console.log('Processing finished !');
    })
    .run();

My ffmpeg version & compile flags:

ffmpeg version 2.4.3 Copyright (c) 2000-2014 the FFmpeg developers
  built on Dec 13 2014 16:48:42 with Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.4.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid --enable-libtheora --enable-libvorbis --enable-vda
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100

I´m running this on Mac OSX Yosemite.

If you could give me a hint on what I´m doing wrong, that would be awesome. Thank you.

Cheers Sebastian

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Ah, thanks for the hint. based on that .outputOptions('-movflags frag_keyframe+empty_moov') fixes it.

frag_keyframe allows fragmented output & empty_moov will cause output to be 100% fragmented; without this the first fragment will be muxed as a short movie (using moov) followed by the rest of the media in fragments.

Guys, is there any other alternative than -movflags frag_keyframe+empty_moov ? I can’t use those flags because Twitter media upload rejects video created with it…