且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ffmpeg 在后台运行时挂起

更新时间:2022-12-27 10:47:03

它挂起是因为在某个点之后它无法再写入它的输出管道.

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.