
是否有一個進度條可以根據在 for 循環中找到並完成的檢測到的文件數量來顯示視覺上完成的進度,如下所示?
mkdir -p hflip; for i in *.mp4; do ffmpeg -n -i "$i" -vf hflip -c:a copy hflip/"${i%.*}.mp4"; done
答案1
我建議為進度條保留一個字串,為每個文件填充一些字符,並在循環期間將它們替換為另一個字符:
bar=""; for i in *.EXT; do bar=$bar-; done; for i in *.EXT; do PROGRAM OPTION1 OPTION2 "$i"; bar=${bar/-/=}; printf "%s\r" $bar; done
但由於你ffmpeg
給了輸出,它會幹擾進度條的列印。您可以將輸出重定向到/dev/null
根本看不到它,但最好知道是否出了問題,因此我建議將其重定向到 和 的日誌文件中stdout
,stderr
這次打印為多行腳本以使其更具可讀性:
mkdir -p hflip
bar=""
for i in *.mp4; do
bar=$bar-
done
for i in *.mp4; do
ffmpeg -n -i "$i" -vf hflip -c:a copy hflip/"${i%.*}.mp4" > /tmp/log.out 2> /tmp/log.err
bar=${bar/-/=}
printf "%s\r" $bar
done
more /tmp/log.err
這將在處理文件後顯示包含所有錯誤的日誌。您也可以顯示log.out
,但因為這是關於 的ffmpeg
,所以牠喜歡輸出很多大多數人不想閱讀的內容。 (-;
答案2
嘗試這樣的簡單解決方案(您需要全面品質管理包裹):
for i in *.EXT; do PROGRAM OPTION1 OPTION2 "$(echo $i|tqdm)"; done
假設您的檔案名稱中沒有“有趣”的字元。
答案3
我建議兩個選擇
1. bash
shellscript用來pv
連續顯示進度
安裝pv
sudo apt install pv # in Debian and Ubuntu, other commands in other distros
帶有演示程式的 Shellscript
#!/bin/bash
# if only files (if directories, you may need another command)
cnt=0
for i in dir/*
do
cnt=$((cnt+1))
done
files="$cnt"
> log
> err
for i in dir/*
do
ls "$i" >> log 2>> err # simulating the actual process
sleep 2 # simulating the actual process
echo "$i"
done | pv -l -s "$files" > /dev/null # progress view using lines with $i
示範
過程中
$ ./pver
2.00 0:00:06 [0,00 /s] [===============> ] 40% ETA 0:00:09
完成後
$ ./pver
5.00 0:00:10 [ 499m/s] [======================================>] 100%
2. bash
shellscript按需顯示當前進度狀態
for
在後台循環,運行program
和一個計數器cnt
while
循環尋找字元輸入(如果c
,則告訴我們進度)
沒有進度條,但只要您願意,您就可以獲得有關進度的狀態更新。
帶有演示程式的 Shellscript
#!/bin/bash
cnt=0
echo "0" > clog
program () {
ls "$1"
sleep 5
}
# main
files=$(ls -1 dir|wc -l)
for i in dir/*
do
program "$i"
cnt=$((cnt+1))
echo "$cnt" > clog
done > log &
while [ "$cnt" != "$files" ]
do
cnt=$(cat clog)
read -sn1 -t1 chr
if [ "$chr" == "c" ]
then
echo "$cnt of $files files processed; running ..."
fi
done
echo "$cnt of $files files processed; finished :-)"
示範
$ ./loop
0 of 5 files processed; running ...
3 of 5 files processed; running ...
5 of 5 files processed; finished :-)
$ cat log
dir/file1
dir/file2
dir/file3
dir/file4
dir/file w space