Advertisement
pintcat

ffprogress - runs custom ffmpeg command line & turns it's output into a progress bar.

Sep 4th, 2022 (edited)
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.82 KB | Software | 0 0
  1. #####################################################################################################################
  2. # ffprogress v1.12 - turns ffmpeg output into a simple progress bar                                                 #
  3. # Handshake:                                                                                                        #
  4. # $FF_LOG contains the path & name of the log file (default: "/media/ramdisk/log")                                  #
  5. # $FF_CMD contains the path & name of the script with the ffmpeg command line (default: "/media/ramdisk/cmd")       #
  6. # $FF_INPARAMS contains the input parameters (default: none)                                                        #
  7. # $FF_INPUT contains the input path and file name (default: none)                                                   #
  8. # $FF_OUTPUT contains the output path and file name (default: "/dev/null")                                          #
  9. # $FF_ARGS contains additional arguments passed to ffmpeg (default: none)                                           #
  10. # The input file name is mandatory, output is optional. FF_ARGS must contain the remaining ffmpeg command line.     #
  11. #####################################################################################################################
  12.  
  13. . progress.sh
  14.  
  15. FF_LOG=/media/ramdisk/log_$(date +%y%m%d%H%M%S%3N)
  16. FF_CMD=/media/ramdisk/cmd_$(date +%y%m%d%H%M%S%3N)
  17. FF_OUTPUT=/dev/null
  18. FF_TIME_PAST=0
  19. PROGRESS_R1=" "
  20. PROGRESS_FT=S
  21. PROGRESS_TC=5
  22.  
  23. FF_PROGRESS(){
  24.     if [ -z "$FF_INPUT" -o ! -e "$FF_INPUT" ]; then
  25.         FF_ERROR="wrong input path or file name."
  26.         return 1
  27.     elif [ "$FF_OUTPUT" != /dev/null -a -e "$FF_OUTPUT" ]; then
  28.         read -r -p "Output file already exists. Overwrite (y/N)? " OPT
  29.         case $OPT in
  30.             y|Y)
  31.                 FF_ARGS="$FF_ARGS -y"
  32.                 ;;
  33.             *)
  34.                 FF_ERROR="output flile already exists and won't be replaced."
  35.                 return 1
  36.                 ;;
  37.         esac
  38.     fi
  39.     printf "Starting ffmpeg...\r"
  40.     echo "ffmpeg $FF_INPARAMS -i \"$FF_INPUT\" $FF_ARGS \"$FF_OUTPUT\" 2>> $FF_LOG" > $FF_CMD
  41.     chmod +x $FF_CMD
  42.     $FF_CMD &
  43.  
  44.     while ! grep -qs Duration $FF_LOG; do sleep 0.2s; done
  45.     PROGRESS_MAX1=$(grep "Duration:" $FF_LOG | awk -F '[:.]' '{print ($2*3600)+($3*60)+$4}')
  46.     [ -n "$VIDEO_LIMIT" ] && PROGRESS_MAX1=$(($VIDEO_LIMIT*100/$(grep -m1 " fps," $FF_LOG | awk -F " fps," '{print $1}' | awk -F "[, .]" '{print $(NF-1)$NF}')))
  47.     grep "Duration:" $FF_LOG | awk -F '[, ]' '{printf $3" "$4}'
  48.     printf " Processing $PROGRESS_MAX1 seconds of the video...\n"
  49.     while [ -n "$(ps -e | grep ffmpeg)" ]; do
  50.         FF_TIME_CUR=$(grep -Eo 'time=[0-9]+:[0-9]+:[0-9]+' $FF_LOG | tail -n1 | awk -F '[=:]' '{print ($2*3600)+($3*60)+$4}')
  51.         if [ -n "$FF_TIME_CUR" ] && [ $FF_TIME_CUR -ne $FF_TIME_PAST ]; then
  52.             PROGRESS_BAR $FF_TIME_CUR
  53.             sleep 0.1s
  54.             FF_TIME_PAST=$FF_TIME_CUR
  55.         fi
  56.     done
  57.     PROGRESS_BAR $PROGRESS_MAX1
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement