ffmpeg-progressbar.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env bash
  2. # Example implementation of progressbar for ffmpeg written in pure bash.
  3. #
  4. # Works only using bash, no third party special dependencies needed. Progress
  5. # percentage is shown in decimal places (e.g. 45.12%, which is done with awk,
  6. # without anything like bc), so very useful for large files and extremely
  7. # portable.
  8. #
  9. # License: CC0
  10. # Usage:
  11. # Just pass a video file to this script. e.g.
  12. # $ /path/to/ffmpeg-progressbar.sh somefile.mp4
  13. # Input file name
  14. source="$1"
  15. # Get total frames in the source video
  16. # Ref: https://stackoverflow.com/a/28376817
  17. total_frames=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 "$source")
  18. # Use any ffmpeg command here
  19. # But be sure to keep "-progress /dev/stdout -nostats" intact
  20. ffmpeg -progress /dev/stdout -nostats -i "$source" "${source}.ogv" | while IFS='=' read -r key value; do
  21. # This is run for each line of the progress output. It may or may not have
  22. # frame data. 'frame' data is the number of frame that are done processing.
  23. # A line like 'frame=1234' is passed and IFS='=' devides the key and value.
  24. # So $key has 'frame' and $value has '1234'.
  25. # When "frame" data is passed to us, we update the progressbar.
  26. if [ "$key" = 'frame' ]; then
  27. current_frame="$value"
  28. # Calculations
  29. # A clever little trick to calculate float numbers with bash
  30. # Ref: https://stackoverflow.com/a/22406193
  31. progress_percentage=$(awk "BEGIN {printf \"%.2f\",${current_frame}/${total_frames}*100}")
  32. progress_int=$(( ${progress_percentage%.*} / 2 ))
  33. # Progress bar
  34. echo -n '['
  35. for ((i = 0 ; i <= $progress_int; i++)); do echo -n '#'; done
  36. for ((j = i ; j <= 50 ; j++)); do echo -n '-'; done
  37. echo -n '] '
  38. # Progress text
  39. echo -n "total frames: ${total_frames} done: ${current_frame} - ${progress_percentage}%" $'\r'
  40. fi
  41. done