[英]script to execute program in loop and kill current process if time limit is exceeded
[英]Limit cpu limit of process in a loop
我正在嘗試在多個文件的循環中執行ffmpeg
。 我只想一次運行一個實例,並且只使用 50% 的 CPU。 我一直在嘗試cpulimit
但它在循環中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 。 您也可以限制 ffmpeg 與-threads
使用的內核數。 這里有一些代碼給你:
ts sh -c "ffmpeg -i INPUT.mp4 -threads 4 OUTPUT.mp4"
ts -S 1
ts
如果您希望 -threads 選項對編碼器產生影響,則應將其放在 -i 參數之后,輸出文件名之前 - 您當前的選項僅告訴解碼部分使用單個線程。 因此,為了使用單個線程保留所有內容,您需要在 -i 選項之前和之后使用 -threads 1。 所以你可以這樣做:
ffmpeg -threads 1 -i INPUT.mp4 -threads 1 OUTPUT.mp4
你應該在前台運行它。 通過這種方式,循環將按預期工作。
$ 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.