Skip to content

Instantly share code, notes, and snippets.

@lyarinet
Forked from csparker247/ffmpeg-progress
Created November 27, 2021 08:44
Show Gist options
  • Save lyarinet/8550a93d2ca3d3d31efa54419e339e03 to your computer and use it in GitHub Desktop.
Save lyarinet/8550a93d2ca3d3d31efa54419e339e03 to your computer and use it in GitHub Desktop.
Outputs ffmpeg progress to console as percentage of work completed.
# Get video duration in frames
duration=$(ffmpeg -i [filename] 2>&1 | sed -n "s/.* Duration: \([^,]*\), start: .*/\1/p")
fps=$(ffmpeg -i [filename] 2>&1 | sed -n "s/.*, \(.*\) tbr.*/\1/p")
hours=$(echo $duration | cut -d":" -f1)
minutes=$(echo $duration | cut -d":" -f2)
seconds=$(echo $duration | cut -d":" -f3)
FRAMES=$(echo "($hours*3600+$minutes*60+$seconds)*$fps" | bc | cut -d"." -f1)
# Start ffmpeg, use awk to flush the buffer and remove carriage returns
ffmpeg -i [filename] [options] [outputfile] 2>&1 | awk '1;{fflush()}' RS='\r\n'>[errorlog] &
# Get ffmpeg Process ID
PID=$( ps -ef | grep "ffmpeg" | grep -v "grep" | awk '{print $2}' )
# While ffmpeg runs, process the log file for the current frame, display percentage progress
while ps -p $PID>/dev/null ; do
currentframe=$(tail -n 1 [errorlog] | awk '/frame=/ { print $2 }')
if [[ -n "$currentframe" ]]; then
PROG=$(echo "scale=3; ($currentframe/$FRAMES)*100.0" | bc)
echo PROGRESS: $PROG
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment