1

Is there a progress bar that can show visually completed progress based on the number of detected files found and completed in a for loop like the one below?

mkdir -p hflip; for i in *.mp4; do ffmpeg -n -i "$i" -vf hflip -c:a copy hflip/"${i%.*}.mp4"; done
  • 1
    Did one of the answers solve your problem? Then please close the question by marking the answer – Philippos Jun 3 '19 at 5:45
2

I suggest to keep a string for the progress bar, fill it with some character for each file and replace them with another during your loop:

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

But since your ffmpeg gives output, it will interfere with the printing of the progress bar. You could redirect the output to /dev/null not to see it at all, but it may be good to know if something went wrong, so I suggest to redirect it into log files for stdout and stderr, this time printed as multi-line script to make it more readable:

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

This will display the log with all errors after processing the files. You can also display the log.out, but because this is about ffmpeg, it likes to output a lot of stuff most people don't want to read. (-;

| improve this answer | |
  • This code is making my ffmpeg commands give me No such file or directory errors, also I could not see a progress bar anywhere. – Anonymous May 27 '19 at 6:54
  • 1
    1) Try with a dummy command like sleep 1 if this progress bar is what you are looking for. 2) Your command is not changed; if it worked before, it still works; if it fails, it did fail before. 3) Of course, if your command produces output, it will interfere with the progress bar. ffmpeg is extremely chatty, so you need to redirect its output (to a logfile or /dev/null) to avoid it. If you want a progress bar and to see the program output, you need to define it, because obviously, there can't be a general solution. Name your command and expected way to deal with the output and bar. – Philippos May 27 '19 at 8:55
  • Ohhh I see! Thank you! So how can I redirect output of a script.sh and see a progress bar instead? – Anonymous May 28 '19 at 15:00
  • I edited my answer to demonstrate the redirection. Good luck. – Philippos May 28 '19 at 15:39
  • Sorry, would you mind editing your answer once more and use this instead? mkdir -p hflip; for i in *.mp4; do ffmpeg -n -i "$i" -vf hflip -c:a copy hflip/"${i%.*}.mp4"; done I updated my question, very sorry for the change. Hope this is okay. – Anonymous Jun 7 '19 at 19:23
1

Try something like this for a simple solution (you need the tqdm package):

for i in *.EXT; do PROGRAM OPTION1 OPTION2 "$(echo $i|tqdm)"; done

assuming there are no "funny" characters in your filenames.

| improve this answer | |
  • How to make it work with this command? I have tried many ways but keep failing. mkdir -p hflip; for i in *.mp4; do ffmpeg -n -i "$i" -vf hflip -c:a copy hflip/"${i%.*}.mp4"; done I also updated my question, very sorry about the change. – Anonymous Jun 7 '19 at 19:21
1

I suggest two alternatives

1. bash shellscript using pv to show progress continuously

Install pv

sudo apt install pv  # in Debian and Ubuntu, other commands in other distros

Shellscript with a demo program

#!/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

Demo

During the process

$ ./pver
2.00  0:00:06 [0,00 /s] [===============>                        ] 40% ETA 0:00:09

After finish

$ ./pver
5.00  0:00:10 [ 499m/s] [======================================>] 100%

2. bash shellscript to show current progress status on demand

  • for loop in the background, run the program and a counter cnt
  • while loop looking for a character input (if c, then tell us about the progress)

There is no progress bar, but whenever you want, you can get a status update about the progress.

Shellscript with a demo program

#!/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 :-)"

Demo

$ ./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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.