Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SCRIPT] video-thumbnailer
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Alexandr
n00b
n00b


Joined: 06 Sep 2005
Posts: 11
Location: Ukraine

PostPosted: Wed Feb 28, 2007 8:35 am    Post subject: [SCRIPT] video-thumbnailer Reply with quote

Script makes one picture with thumbnails from video file on it.

Example:
http://img84.imageshack.us/img84/5684/sincity1280x720xvidac3rrh8.jpg

Requires:
- bash :)
- midentify and mplayer (media-video/mplayer package)
- bc (sys-devel/bc package)
- montage and convert (media-gfx/imagemagick package)

Usage:
- copy and paste next code into new file /usr/local/bin/video-thumbnailer
- make this file executable (chmod a+x video-thumbnailer)

Code:
#!/bin/bash
# video-thumbnailer
# Author: Alexander Pogrebny

if [ $# -ne 2 ]
        then
                echo "Usage: `basename $0` <number_of_thumbs> /path/to/file"
                exit 1
        fi

if [ $1 -gt 0 ] && [ $1 -lt 100 ]
        then :
        else
                echo "Number must be in range: 1-99"
                exit 1
        fi

if [ ! -e "$2" ]
        then
                echo "File \"$2\" not found"
                exit 1
        fi

if ls tmp-*.jpg
        then
                echo "Please, delete all tmp-*.jpg files in current directory first"
                exit 1
        fi

LENGTH=`midentify "$2" | grep ID_LENGTH | cut -d = -f 2 | cut -d . -f 1`
FPS=`midentify "$2" | grep ID_VIDEO_FPS | cut -d = -f 2`
FRAMESALL=`echo "$LENGTH * $FPS" | bc | cut -d . -f 1`

FRAMESREQUIRED=`expr $1 + 1`
COUNT=1

OFFSETFRAMES=`expr $FRAMESALL / $FRAMESREQUIRED`
OFFSETSECONDS=`echo "$OFFSETFRAMES / $FPS" | bc`

while [ $COUNT -lt $FRAMESREQUIRED ]
do
        OFFSET=`expr $OFFSETSECONDS \* $COUNT`
        echo "$COUNT - $OFFSET second"
        mplayer -nosound -ss $OFFSET -vo jpeg -frames 2 "$2" > /dev/null
        if [ ${#COUNT} -eq 1 ]
                then
                        mv 00000002.jpg tmp-0$COUNT.jpg
                else
                        mv 00000002.jpg tmp-$COUNT.jpg
        fi
        COUNT=`expr $COUNT + 1`
done

rm 00000001.jpg

MM=`expr $LENGTH / 60`
HH=0
while [ $MM -ge 60 ]
do
        MM=`expr $MM - 60`
        HH=`expr $HH + 1`
done
SS=`echo "$LENGTH - (($HH * 60 + $MM) * 60)" | bc`
if [ ${#SS} -eq 1 ]
        then
                SS="0$SS"
        fi
if [ ${#MM} -eq 1 ]
        then
                MM="0$MM"
        fi
TIME=`echo "$HH:$MM:$SS"`

WIDTH=`midentify "$2" | grep ID_VIDEO_WIDTH | cut -d = -f 2`
HEIGHT=`midentify "$2" | grep ID_VIDEO_HEIGHT | cut -d = -f 2`
if [ $WIDTH -gt 600 ]
        then
                W=`expr $WIDTH / 6`
        else
                W=`expr $WIDTH / 2`
        fi
if [ $HEIGHT -gt 600 ]
        then
                H=`expr $HEIGHT / 6`
        else
                H=`expr $HEIGHT / 2`
        fi

montage -geometry $W\x$H+10+10 -background black tmp-*.jpg tmp-thumb.jpg

convert tmp-thumb.jpg -bordercolor black -border 0x50 -font Comic-Sans-MS -fill white -pointsize 28 -gravity NorthWest -annotate +10+10 "$2" tmp-thumb2.jpg
convert tmp-thumb2.jpg -font Comic-Sans-MS -fill white -pointsize 28 -gravity NorthEast -annotate +10+10 "$TIME" tmp-thumb3.jpg

VIDEOCODEC=`midentify "$2" | grep ID_VIDEO_CODEC | cut -d = -f 2`
AUDIOCODEC=`midentify "$2" | grep ID_AUDIO_CODEC | cut -d = -f 2`
AUDIOBITRATE=`midentify "$2" | grep ID_AUDIO_BITRATE | tail -n 1 | cut -d = -f 2 | cut -d 0 -f 1`
AUDIORATE=`midentify "$2" | grep ID_AUDIO_RATE | tail -n 1 | cut -d = -f 2`
AUDIONCH=`midentify "$2" | grep ID_AUDIO_NCH | tail -n 1 | cut -d = -f 2`

convert tmp-thumb3.jpg -font Comic-Sans-MS -fill white -pointsize 28 -gravity SouthWest -annotate +10+10 "[$VIDEOCODEC]   $WIDTH x $HEIGHT,   $FPS fps" tmp-thumb4.jpg
convert tmp-thumb4.jpg -font Comic-Sans-MS -fill white -pointsize 28 -gravity SouthEast -annotate +10+10 "[$AUDIOCODEC]   $AUDIOBITRATE kbps,   $AUDIORATE Hz,   $AUDIONCH ch" "$2".jpg

rm tmp-*.jpg

echo "[$VIDEOCODEC] $WIDTH x $HEIGHT, $FPS fps"
echo "[$AUDIOCODEC] $AUDIOBITRATE kbps, $AUDIORATE Hz, $AUDIONCH ch"

exit 0
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum