我正在尝试在多个文件上循环执行
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
杀死。
使用队列是正确的方法。我使用的一个简单解决方案是Task Spooler。您还可以使用
-threads
限制 ffmpeg 使用的核心数量。这是给您的一些代码:
ts sh -c "ffmpeg -threads 4 -i INPUT.mp4 -threads 4 OUTPUT.mp4"
ts -S 1
ts
-threads 4
之前添加 -i
以限制解码 CPU 使用量您应该在前台运行它。这样循环就会按预期工作。
$ 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
如果您希望 -threads 选项对编码器产生影响,您应该将其放在 -i 参数之后、输出文件名之前 - 您当前的选项仅告诉解码部分使用单个线程。因此,为了使所有这些都使用单个线程,您需要在 -i 选项之前和之后都添加 -threads 1 。所以你可以这样做:
ffmpeg -threads 1 -i INPUT.mp4 -threads 1 OUTPUT.mp4