ffmpeg在后台运行时挂起 [英] ffmpeg hangs when run in background

查看:836
本文介绍了ffmpeg在后台运行时挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我按以下方式运行ffmpeg:

If I run ffmpeg as follows:

ffmpeg -i H264-media-4.264 4.avi

它工作正常(即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 &

挂起! (和4.avi从来没有创建)
任何想法?

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

注意:问题是隔离类似的问题在python当试图运行它作为子进程和它挂起以及:ff.py include

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)

$ c> ./ ff.py 运行正常, ./ ff.py& 挂起。

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


  • 系统:CentOS 6.6

  • ffmpeg:0.10.2

成功运行的结果:

 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%

停止运行的结果:

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


推荐答案

挂起是因为在某一点之后,它不能再写入它的输出管道了。

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

当你运行一个进程时,它有3个打开的管道:stdin,stdout和stderr。管道具有内存缓冲区(Linux上为4KB),可以容纳一定量的数据,下一个写操作将暂停,直到从管道的另一侧读取一些数据。

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.

因为你从未从stdout和stderr读取你的子进程和FFMpeg输出相当多,它会挂起。

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

如上面的注释中所述,您可以简单地将您的ffmpeg输出重定向到/ dev / null:

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

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

在这种情况下,ffmpeg永远不会输出足够的数据来管道'挂起'。

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

另一种选择是在你的子进程启动后立即关闭它的stdin,stdout和stderr。

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

还有一个选择是读取(可选择丢弃)子进程的stdout和stderr上的所有内容。

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

这篇关于ffmpeg在后台运行时挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆