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

Created on 13 Dec 2014  路  12Comments  路  Source: fluent-ffmpeg/node-fluent-ffmpeg

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

Most helpful comment

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.

All 12 comments

Could you please show what ffmpeg outputs when the error happens ?
You can do so by adding 2 more arguments to the 'error' event handler and outputting them to the console.

@njoyard

Sure, thanks for taking a look at it:

2nd argument: null
3rd argument: 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
Input #0, image2, from 'http://192.168.178.63/image.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 9309 kb/s
    Stream #0:0: Video: mjpeg, yuvj422p(pc, bt470bg), 640x480, 25 fps, 25 tbr, 25 tbn, 25 tbc
[swscaler @ 0x7fc8fc006200] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x7fc8fb809800] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fc8fb809800] profile High, level 3.0
[libx264 @ 0x7fc8fb809800] 264 - core 142 r2455 021c0dc - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[mp4 @ 0x7fc8fb808c00] muxer does not support non seekable output
Output #0, mp4, to 'pipe:1':
  Metadata:
    encoder         : Lavf56.4.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x480, q=-1--1, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.1.100 libx264
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

Here is your issue:

[mp4 @ 0x7fc8fb808c00] muxer does not support non seekable output

I thought about that in the first place but wasn't sure. mp4 cannot write to streams/pipe, it needs to be able to seek back to the beginning of the output to write headers after encoding is finished. You may want to either use a different format or add some options for the muxer to be able to stream its output.

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.

Thanks Asciidisco and Njoyard for your questions and answers, I got the same issues and they are very helpful

Thank you @asciidisco for your solution! It fixed it for me.

:+1:

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...

@alekbarszczewski you only need those when you intend to stream the media. Just don't use the option when the goal is to read the file as a whole.

@njoyard yes but I don't want to save mp4 to a file - I need to get stream and it does not work for mp4 without this flags

If twitter does not support initial moov you won't be able to stream the same file you send them.

@asciidisco me too face the same problem using to merge audio and video with this https://github.com/WritingMinds/ffmpeg-android-java library for android after merging .Video is not viewed in default video player in device but it plays in vlc player, think so video codec issue,can you please help me to solve this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomKaltz picture TomKaltz  路  5Comments

joergbirkhold picture joergbirkhold  路  5Comments

bsonntag picture bsonntag  路  5Comments

geraldoramos picture geraldoramos  路  3Comments

danielbanfield picture danielbanfield  路  5Comments