Automatically take 8 screenshots of a video using mplayer

# Description: Automatically take 8 screenshots of a video using mplayer
#              Screenshots are saved in 01.png, 02.png, ..., 08.png
# Author: Xuan Ngo
# Usage: ./ VideoFilename
# Requirements: mplayer, grep, sed
# For some unknown reason, the last screenshot is not generated by mplayer.
# Therefore, always add 1 more to the total number of screenshots that you want.
# Get the total length of the video in seconds.
#  Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null ${filename} | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length-=4
# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}
# time_at: When should mplayer take screenshots.
# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
  # Create unique filename.
  padding=$(printf %03d ${i})
  # Take the screenshot.
  mplayer -nosound -ss ${time_at} -vf screenshot -frames 1 -vo png:z=9 ${filename}
  # Increment to the next time slice.
  let time_at+=${time_slice}
  # Move the screenshot 00000001.png to 0X.png
  mv 00000001.png ${padding}.png