extract-thumbnail script improved
[video-handling.git] / extract-thumbnail
1 #!/bin/bash
2 #
3 # Calin-Andrei Burloiu, 2011, calin.burloiu@gmail.com
4 #
5 # This script extracts a thumbnail from a random frame of a video file.
6
7 #
8
9 if test $# -ne 3; then
10     echo "Usage: $0 input-video output-image resolution"
11     exit 1
12 fi
13
14 in="$1"
15 out="$2"
16 resolution="$3"
17 FFMPEG=/usr/bin/ffmpeg
18
19 if test ! -f "$in"; then
20     echo "Error: No such file $in"
21     exit 1
22 fi
23
24 # Choose a random frame
25 str_duration=$(mediainfo --Inform="General;%Duration/String3%" "$in" | cut -d"." -f1)
26 h=$(echo $str_duration | cut -d":" -f1 | sed -r 's/^[0]*//')
27 if [ -z "$h" ]; then h=0; fi
28 m=$(echo $str_duration | cut -d":" -f2 | sed -r 's/^[0]*//')
29 if [ -z "$m" ]; then m=0; fi
30 s=$(echo $str_duration | cut -d":" -f3 | sed -r 's/^[0]*//')
31 if [ -z "$s" ]; then s=0; fi
32 s=$(($h * 3600 + $m * 60 + $s))
33
34 seek_point=$(echo "$s $RANDOM" | awk '{ print int($1 * $2 / 32767) }')
35
36 #"$FFMPEG" -i "$in" -ss "$seek_point" -vframes 1 -s "$resolution" -f image2 "$out"
37 "$FFMPEG" -ss "$seek_point" -i "$in" -vcodec mjpeg -vframes 1 -an -s "$resolution" -f rawvideo "$out"