38

If I run ffmpeg as follows:

ffmpeg -i H264-media-4.264 4.avi

It works OK (i.e. 4.avi created OK). However, if I try to run it in background:

ffmpeg -i H264-media-4.264 4.avi &

it hangs! (and 4.avi never created) Any Idea?


Note: The problem is an isolation of of similar problem in python when trying to run it as subprocess and there it hanged as well: ff.py includes

ps = subprocess.Popen(ffmpeg_list, stderr=subprocess.STDOUT,stdout = subprocess.PIPE)

and running ./ff.py runs OK, ./ff.py & hangs too.

  • System: CentOS 6.6
  • ffmpeg: 0.10.2

Results from successful run:

 ffmpeg version 0.10.2 Copyright (c) 2000-2012 the FFmpeg developers
 built on Mar 20 2012 04:34:50 with gcc 4.4.6 20110731 (Red Hat 4.4.6-3)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --enable-runtime-cpudetect --enable-gpl --enable-version3 --enable-postproc --enable-avfilter --enable-pthreads --enable-x11grab --enable-vdpau --disable-avisynth --enable-frei0r --enable-libopencv --enable-libdc1394 --enable-libdirac --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --disable-stripping
  libavutil      51. 35.100 / 51. 35.100
  libavcodec     53. 61.100 / 53. 61.100
  libavformat    53. 32.100 / 53. 32.100
  libavdevice    53.  4.100 / 53.  4.100
  libavfilter     2. 61.100 /  2. 61.100
  libswscale      2.  1.100 /  2.  1.100
  libswresample   0.  6.100 /  0.  6.100
  libpostproc    52.  0.100 / 52.  0.100
[h264 @ 0x24eac00] max_analyze_duration 5000000 reached at 5000000
[h264 @ 0x24eac00] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from 'H264-media-4.264':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1200k tbn, 50 tbc
[buffer @ 0x24efa60] w:640 h:480 pixfmt:yuv420p tb:1/1000000 sar:1/1 sws_param:
[mpeg4 @ 0x24eb540] removing common factors from framerate
Output #0, avi, to '4.avi':
  Metadata:
    ISFT            : Lavf53.32.100
    Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 640x480 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> mpeg4)
Press [q] to stop, [?] for help
frame= 2324 fps=477 q=31.0 Lsize=    2603kB time=00:01:32.96 bitrate= 229.4kbits/s    
video:2542kB audio:0kB global headers:0kB muxing overhead 2.409572%

Results from hanged run:

ffmpeg version 0.10.2 Copyright (c) 2000-2012 the FFmpeg developers
  built on Mar 20 2012 04:34:50 with gcc 4.4.6 20110731 (Red Hat 4.4.6-3)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --enable-runtime-cpudetect --enable-gpl --enable-version3 --enable-postproc --enable-avfilter --enable-pthreads --enable-x11grab --enable-vdpau --disable-avisynth --enable-frei0r --enable-libopencv --enable-libdc1394 --enable-libdirac --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --disable-stripping
  libavutil      51. 35.100 / 51. 35.100
  libavcodec     53. 61.100 / 53. 61.100
  libavformat    53. 32.100 / 53. 32.100
  libavdevice    53.  4.100 / 53.  4.100
  libavfilter     2. 61.100 /  2. 61.100
  libswscale      2.  1.100 /  2.  1.100
  libswresample   0.  6.100 /  0.  6.100
  libpostproc    52.  0.100 / 52.  0.100


[5]+  Stopped                 ffmpeg -i H264-media-4.264 4.avi
slhck
  • 36,575
  • 28
  • 148
  • 201
yoosha
  • 681
  • 2
  • 7
  • 12
  • 2
    Problem solved: need to add "/dev/null 2>/var/log/ffmpeg.log & [slhck - sorry, I'm new here...] – yoosha May 13 '13 at 14:19
  • You should post your answer as an answer below, once you can. You should be able to do that in about 8 hours. I edited your question to correct the formatting—please try and stick to code formatting in the future. – slhck May 13 '13 at 14:22

3 Answers3

62

It hangs because after a certain point it can not write to it's output pipe any longer.

When you run a process it has 3 opened pipes for: stdin, stdout and stderr. A pipe has a memory buffer ( 4KB on Linux ) that can hold up to a certain amount of data and the next write operation will pause until some data is read from the other side of the pipe.

Since you never read from stdout and stderr of your child process and FFMpeg outputs quite a lot it will hang at some point.

As explained in the comment above you can simply redirect your ffmpeg output to /dev/null using:

ffmpeg .... > /dev/null 2>&1 < /dev/null

In this case ffmpeg will never output enough data to have the pipe 'hang'.

Another option would be to just close stdin, stdout and stderr of your child process as soon as you launch it.

And yet another option would be to actually read ( and optionally discard ) everything on stdout and stderr of your child process.

Stanislav Vitvitskyy
  • 2,297
  • 20
  • 17
  • 7
    WOW.... Amazing... I use both Windows and Linux (for easiness in compatibility), and in Windows THIS EXACT METHOD WORKED, and one must change /dev/null by NUL, that is: ffmpeg .... > NUL 2>&1 < NUL (with only one "L" at the end, not "NULL" but "NUL", which is the equivalent to /dev/null in windows, according to [1])... But @Stanislav Vitvitskyy, HOW DID YOU KNOW that this would be the explanation?? How did you think of it? Could you guide us to the source of your idea behind this? Which concept am I missing? Thanks. [1] http://stackoverflow.com/questions/313111/dev-null-in-windows – David L Aug 25 '14 at 20:48
  • I have hit this issue on Windows 10. How do I clear it? Anytime I try to run ffmpeg with any argument, it hangs. I can run it from the console of a different user, but not my own. How to unclog this? Is there a file somewhere in %appdata%? – pcunite Mar 23 '21 at 18:23
  • Most of the time my experience is that the output by `ffmpeg -nostats -loglevel fatal ...` is small enough to fit into buffers which aren't read from while the process is running. And ... you haven't discarded the entire output in case of fatal errors. – Rainer Rillke Jul 02 '21 at 15:42
41

ffmpeg enables interaction with stdin by default. On Mac OS X and Linux systems, this causes an ffmpeg job running in the background to suspend. Adding option -nostdin to the invocation causes ffmpeg to not enable stdin interaction, and so avoids suspending the background process.

In your example, try:

ffmpeg -nostdin -i H264-media-4.264 4.avi &

Note: the ffmpeg documentation, 5.4 Main options says that the input redirection mentioned in the other answers:

ffmpeg ... < /dev/null

achieves "roughly the same result" as the use of -nostdin, but requires a shell.

When might you want to run ffmpeg with no shell? When invoking it as a subprocess, for example with Python's subprocess.run().

Jim DeLaHunt
  • 10,960
  • 3
  • 45
  • 74
  • 3
    I had a similar issue with calling `ffmpeg` from `Go`, which has been giving me headaches for weeks now. Your suggestion of `-nostdin` seems to have solved it. – Bazzz Sep 12 '20 at 15:15
  • 1
    I needed both this flag, as well as the -loglevel error from @ErJab, but it worked! Thanks, it was driving me crazy! – Eliot Gillum Dec 22 '20 at 12:40
  • This is the actual correct answer and should be upvoted over the more popular but inaccurate answer. That other answer just happens to also work because it redirects STDIN from /dev/null, thereby disabling the input, but it doesn't know why it works and attributes the problem to a buffer overrun, which is inaccurate since this will happen even when carefully piping all output to log files or /dev/null. – Dodger Jun 08 '22 at 09:27
7

As others have pointed out, the issue is caused by ffmpeg saturating stdout and/or stderr with verbose messages. So another option could be to turn down the log level for ffmpeg:

ffmpeg -loglevel error -i H264-media-4.264 4.avi &

See this question for more information on log levels in ffmpeg.

ErJab
  • 6,056
  • 10
  • 42
  • 54
  • This is a way to avoid the need to redirect output, but people will encounter the original problem when redirecting all output to /dev/null or piping it into a file, because a buffer overrun is NOT the reason this happens. It happens because ffmpeg is listening for input and is not connected to a shell that can provide any, so it stops to wait for STDIN to be available "again". – Dodger Jun 08 '22 at 09:30