防止 ffmpeg 接管标准输出

发布于 2024-10-17 20:09:27 字数 590 浏览 4 评论 0原文

当我执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-10-24 20:09:27

fork/exec 是正确的解决方案。由于分叉进程继承父进程 fopen 文件句柄等,因此您必须关闭(或重定向)您不希望子进程使用的文件句柄。

例如:

# this will print nothing, but yes is running as a forked process
# you'll want to `killall yes` after running this script.
fork do
  [$stdout, $stderr].each { |fh| fh.reopen File.open("/dev/null", "w") }
  exec "yes"
end

好的,对您发布的代码的一些评论。外部的fork毫无意义。只需从主进程中 fork 两个 ffmpeg 进程即可。也许编写一个辅助函数,例如:

def ffmpeg(mp4)
  fork do
    [$stdout, $stderr].each { ... }
    exec "ffmpeg -i #{mp4} ..."
  end
end

ffmpeg("you-know.mp4")
ffmpeg("bangbang.mp4")

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:

# this will print nothing, but yes is running as a forked process
# you'll want to `killall yes` after running this script.
fork do
  [$stdout, $stderr].each { |fh| fh.reopen File.open("/dev/null", "w") }
  exec "yes"
end

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:

def ffmpeg(mp4)
  fork do
    [$stdout, $stderr].each { ... }
    exec "ffmpeg -i #{mp4} ..."
  end
end

ffmpeg("you-know.mp4")
ffmpeg("bangbang.mp4")
撩人痒 2024-10-24 20:09:27

尝试 subprocess gem - 这就是我现在用来处理进程分叉的东西,并且发现它更容易使用。

例如

    work_list.each do |cmd|
        process = Subprocess::Popen.new(cmd)
        process.run
        process.wait
        #puts process.stdout
        #puts process.stderr
        puts process.status
    end

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.

    work_list.each do |cmd|
        process = Subprocess::Popen.new(cmd)
        process.run
        process.wait
        #puts process.stdout
        #puts process.stderr
        puts process.status
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文