extract-thumbnail script improved master
authorCalin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 17 Nov 2011 14:49:26 +0000 (16:49 +0200)
committerCalin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 17 Nov 2011 14:49:26 +0000 (16:49 +0200)
extract-thumbnail

index 92e4a1d..c2bcd46 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Calin-Andrei Burloiu, 2010, calin.burloiu@gmail.com
+# Calin-Andrei Burloiu, 2011, calin.burloiu@gmail.com
 #
 # This script extracts a thumbnail from a random frame of a video file.
 # 
@@ -11,18 +11,27 @@ if test $# -ne 3; then
     exit 1
 fi
 
-IN="$1"
-OUT="$2"
-RESOLUTION="$3"
+in="$1"
+out="$2"
+resolution="$3"
 FFMPEG=/usr/bin/ffmpeg
 
-if test ! -f "$IN"; then
-    echo "Error: No such file $IN"
+if test ! -f "$in"; then
+    echo "Error: No such file $in"
     exit 1
 fi
 
 # Choose a random frame
-DURATION=$(mediainfo --Inform="General;%Duration%" "$IN" | cut -d"." -f1)
-SEEK_POINT=$(($DURATION * $RANDOM / 32767 / 1000))
+str_duration=$(mediainfo --Inform="General;%Duration/String3%" "$in" | cut -d"." -f1)
+h=$(echo $str_duration | cut -d":" -f1 | sed -r 's/^[0]*//')
+if [ -z "$h" ]; then h=0; fi
+m=$(echo $str_duration | cut -d":" -f2 | sed -r 's/^[0]*//')
+if [ -z "$m" ]; then m=0; fi
+s=$(echo $str_duration | cut -d":" -f3 | sed -r 's/^[0]*//')
+if [ -z "$s" ]; then s=0; fi
+s=$(($h * 3600 + $m * 60 + $s))
 
-"$FFMPEG" -i "$IN" -ss "$SEEK_POINT" -vframes 1 -s "$RESOLUTION" -f image2 "$OUT"
+seek_point=$(echo "$s $RANDOM" | awk '{ print int($1 * $2 / 32767) }')
+
+#"$FFMPEG" -i "$in" -ss "$seek_point" -vframes 1 -s "$resolution" -f image2 "$out"
+"$FFMPEG" -ss "$seek_point" -i "$in" -vcodec mjpeg -vframes 1 -an -s "$resolution" -f rawvideo "$out"