New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Output to file works - Output to stream doesn't #346

Closed
asciidisco opened this Issue Dec 13, 2014 · 12 comments

Comments

Projects
None yet
6 participants
@asciidisco

asciidisco commented Dec 13, 2014

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

@njoyard

This comment has been minimized.

Show comment
Hide comment
@njoyard

njoyard Dec 14, 2014

Member

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.

Member

njoyard commented Dec 14, 2014

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.

@asciidisco

This comment has been minimized.

Show comment
Hide comment
@asciidisco

asciidisco Dec 14, 2014

@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

asciidisco commented Dec 14, 2014

@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
@njoyard

This comment has been minimized.

Show comment
Hide comment
@njoyard

njoyard Dec 14, 2014

Member

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.

Member

njoyard commented Dec 14, 2014

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.

@asciidisco

This comment has been minimized.

Show comment
Hide comment
@asciidisco

asciidisco Dec 17, 2014

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.

asciidisco commented Dec 17, 2014

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.

@asciidisco asciidisco closed this Dec 17, 2014

@trangtungn

This comment has been minimized.

Show comment
Hide comment
@trangtungn

trangtungn Mar 24, 2015

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

trangtungn commented Mar 24, 2015

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

@Glavin001

This comment has been minimized.

Show comment
Hide comment
@Glavin001

Glavin001 May 30, 2015

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

Glavin001 commented May 30, 2015

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

@alekbarszczewski

This comment has been minimized.

Show comment
Hide comment
@alekbarszczewski

alekbarszczewski commented Sep 22, 2015

👍

@alekbarszczewski

This comment has been minimized.

Show comment
Hide comment
@alekbarszczewski

alekbarszczewski Sep 30, 2015

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 commented Sep 30, 2015

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

@njoyard

This comment has been minimized.

Show comment
Hide comment
@njoyard

njoyard Oct 11, 2015

Member

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

Member

njoyard commented Oct 11, 2015

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

@alekbarszczewski

This comment has been minimized.

Show comment
Hide comment
@alekbarszczewski

alekbarszczewski Oct 11, 2015

@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

alekbarszczewski commented Oct 11, 2015

@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

@njoyard

This comment has been minimized.

Show comment
Hide comment
@njoyard

njoyard Oct 11, 2015

Member

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

Member

njoyard commented Oct 11, 2015

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

@karthikeyan-kandasamy

This comment has been minimized.

Show comment
Hide comment
@karthikeyan-kandasamy

karthikeyan-kandasamy Aug 3, 2016

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

karthikeyan-kandasamy commented Aug 3, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment