#!/bin/bash
videofile=$(echo "$1" | sed -e 's/%20/ /g' | sed -e 's/%26/\&/g' | sed -e 's/%5B/\[/g' | sed -e 's/%5D/\]/g' | sed -e 's/%40/\@/g'  )

# If we only have a chunk, then file will return "data", so we must exit to prevent mplayer from hanging
[ "$(/usr/bin/file --brief "$videofile" | grep -e "^data$")" ] && exit

thumbfile="$2"
LENGTH=$(mplayer -nocache  -identify -vo null -ao null -frames 0 "$videofile"  | awk -F= '/ID_LENGTH/ {print $2}'| awk -F. '{print $1}')
RANDOM=$(date %+s)
START=$(((LENGTH / 100) * 15)) ; END=$(((LENGTH / 100) * 70)); LENGTH=$(($END - $START)) ; SHOT=$((($RANDOM % $LENGTH) + $START))
/usr/bin/mplayer -nojoystick -nolirc -nocache -noautosub -nosound -vf scale -vo jpeg:outdir=/tmp:quality=100 -ao null -ss "$SHOT" -frames 3 -zoom -xy 128 -slave "$videofile"
mv /tmp/00000002.jpg "$thumbfile" || rm -f /tmp/00000002.jpg 
rm -f /tmp/00000001.jpg /tmp/00000003.jpg 
