How to stream with ffmpeg in a separate process

16,451

Solution 1

Probably it's trying to read stdin (ffmpeg actually takes interactive commands...). To disable that, ffmpeg has the -nostdin option—that should make it run in the background.

You could also consider -loglevel fatal or -loglevel error as well as a few other options to make it quieter (-nostats, etc.).

Solution 2

Redirecting stdin does the trick.

Use a command like: ffmpeg ... > output.log 2>&1 < /dev/null &.

  • > output.log Redirect stdout to output.log.
  • 2>&1 Redirect stderr to stdout (so it ends up in the log file and not in the terminal).
  • < /dev/null Redirect stdin (which ffmpeg thinks it needs) to /dev/null (which gives an empty stream).
  • & has the process run in the background.

Solution 3

As per PHP example on ffmpeg site, one should redirect input and both outputs to somewhere for ffmpeg to effectively run in the background:

ffmpeg -s 640x480 -f video4linux2 -i $CAM_PATH -f mpeg1video -b:v 800k -r 30 http://127.0.0.1:$FFMPEG_PORT/$VIDEO_PASSWORD/640/480/ -nostdin -nostats </dev/null >/dev/null 2>&1 &

The </dev/null >/dev/null 2>&1 bits do the trick.

Share:
16,451

Related videos on Youtube

Maxim V. Pavlov
Author by

Maxim V. Pavlov

Updated on September 18, 2022

Comments

  • Maxim V. Pavlov
    Maxim V. Pavlov over 1 year

    I need to stream with ffmpeg on Ubuntu. The streaming should be started from a bash script.

    Currently, I call ffmpeg like this, as a part of subroutine in a bash script:

    function startFFMpegEncoder () {
        echo "Starting Encoder..."
    
        killall -9 ffmpeg   
    
        ffmpeg -s 640x480 -f video4linux2 -i $CAM_PATH -f mpeg1video -b:v 800k -r 30 http://127.0.0.1:$FFMPEG_PORT/$VIDEO_PASSWORD/640/480/ &
    
        echo "Encoder started."
    }
    

    Here is an output that is produced by this part of the script's execution:

    Starting Encoder...
    ffmpeg: no process found
    ffmpeg version N-73895-g323ec6b Copyright (c) 2000-2015 the FFmpeg developers
      built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
      configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
      libavutil      54. 28.100 / 54. 28.100
      libavcodec     56. 50.101 / 56. 50.101
      libavformat    56. 40.101 / 56. 40.101
      libavdevice    56.  4.100 / 56.  4.100
      libavfilter     5. 25.100 /  5. 25.100
      libavresample   2.  1.  0 /  2.  1.  0
      libswscale      3.  1.101 /  3.  1.101
      libswresample   1.  2.101 /  1.  2.101
      libpostproc    53.  3.100 / 53.  3.100
    
    [1]+  Stopped                 ffmpeg -s 640x480 -f video4linux2 -i $CAM_PATH -f mpeg1video -b:v 800k -r 30 http://127.0.0.1:$FFMPEG_PORT/$VIDEO_PASSWORD/640/480/
    Encoder started.
    

    Why does the ffmpeg immediately gets stopped? Running ffmpeg in a separate terminal as a single command starts the broadcast for as long as there is an output trace in the terminal window.

  • Maxim V. Pavlov
    Maxim V. Pavlov almost 9 years
    Even though these were an "on the point assumptions", actual solution is a bit different. Thank you very much though.
  • derobert
    derobert almost 9 years
    @MaximV.Pavlov you can post your own answer explaining the solution you went with, to help future visitors to the site.
  • Maxim V. Pavlov
    Maxim V. Pavlov almost 9 years
    Sure, just did that, you are just too quick)
  • derobert
    derobert almost 9 years
    Might want to send the output to a log file (or syslog) instead... Much easier to fix problems when the error messages aren't sent to /dev/null.