防止 ffmpeg 接管标准输出
当我执行 system "ffmpeg -i just-do-it.mp4 -ab 96k -ar 22050 -qscale 6 output.flv"
ffmpeg 接管 ruby 进程直到工作完成,有时需要很长一段时间。我尝试在 Ruby 中使用线程 amd fork 但无济于事,还有 system
等效命令,如 exec
%x[]
我也尝试了最新的ruby 1.9.2 中的纤维,但我认为我没有正确使用它。
我的问题是如何同时从 ruby 运行两个 ffmpeg 进程?
编辑:
fork do
fork do
system "ffmpeg -i you-know.mp4 -ab 96k -ar 22050 -qscale 6 #{Time.now.sec}.flv"
end
fork do
system "ffmpeg -i bangbang.mp4 -ab 96k -ar 22050 -qscale 6 #{Time.now.sec}.flv"
end
end
When I do system "ffmpeg -i just-do-it.mp4 -ab 96k -ar 22050 -qscale 6 output.flv"
ffmpeg takes over the ruby process till the job is done, which sometimes take a long time. I've tried using threads amd fork in Ruby to no avail, also system
equivalent commands like exec
%x[]
I also tried the latest Fibers in ruby 1.9.2, but I don't think I'm using it properly.
My question is how to run two ffmpeg processes from ruby concurrently?
EDIT:
fork do
fork do
system "ffmpeg -i you-know.mp4 -ab 96k -ar 22050 -qscale 6 #{Time.now.sec}.flv"
end
fork do
system "ffmpeg -i bangbang.mp4 -ab 96k -ar 22050 -qscale 6 #{Time.now.sec}.flv"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fork/exec
是正确的解决方案。由于分叉进程继承父进程 fopen 文件句柄等,因此您必须关闭(或重定向)您不希望子进程使用的文件句柄。例如:
好的,对您发布的代码的一些评论。外部的
fork
毫无意义。只需从主进程中 fork 两个 ffmpeg 进程即可。也许编写一个辅助函数,例如:fork/exec
is the right solution. Since forked processes inherit the parent processes fopen file handles/etc., you'll have to close (or redirect) the file handles you don't want children processes to use.For example:
Ok, some comments on the code you posted. The outer
fork
is pointless. Just fork the two ffmpeg process from the main process. Maybe write a helper function like:尝试 subprocess gem - 这就是我现在用来处理进程分叉的东西,并且发现它更容易使用。
例如
Try the subprocess gem - that's what I'm using now for dealing with process forking and finding it much easier to use.
E.g.