限制循环中进程的cpu限制

问题描述 投票:0回答:3

我正在尝试在多个文件上循环执行

ffmpeg
。我只想一次运行一个实例,并且只使用 50% 的 CPU。我一直在尝试
cpulimit
,但循环效果不佳。

for i in {1..9}; do cpulimit -l 50 -- ffmpeg <all the options>; done

这会同时产生所有九个作业,并且它们都属于

init
所有,所以我必须打开
htop
才能杀死它们。

for i in {1..9}; do ffmpeg <all the options> & cpulimit -p $! -l 50; done

此挂起,

ctrl+c
继续下一个循环迭代。这些个体只能被
SIGKILL
杀死。

bash loops ubuntu ffmpeg cpu-usage
3个回答
4
投票

使用队列是正确的方法。我使用的一个简单解决方案是Task Spooler。您还可以使用

-threads
限制 ffmpeg 使用的核心数量。这是给您的一些代码:

ts sh -c "ffmpeg -threads 4 -i INPUT.mp4 -threads 4 OUTPUT.mp4"
  • 您可以使用以下方法将最大并发任务数设置为 1:
    ts -S 1
  • 要查看当前队列,只需运行
    ts
  • 编辑:在
    -threads 4
    之前添加
    -i
    以限制解码 CPU 使用量

2
投票

您应该在前台运行它。这样循环就会按预期工作。

$ cpulimit --help
...
-f  --foreground   launch target process in foreground and wait for it to exit

这对我有用。

for file in *.mp4; do
    cpulimit -f -l 100 -- ffmpeg -i "$file" <your options>
done

1
投票

如果您希望 -threads 选项对编码器产生影响,您应该将其放在 -i 参数之后、输出文件名之前 - 您当前的选项仅告诉解码部分使用单个线程。因此,为了使所有这些都使用单个线程,您需要在 -i 选项之前和之后都添加 -threads 1 。所以你可以这样做:

ffmpeg -threads 1 -i INPUT.mp4 -threads 1 OUTPUT.mp4

© www.soinside.com 2019 - 2024. All rights reserved.