且构网

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

ffmpeg在后台运行时挂起

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

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

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.