Skip to content

Instantly share code, notes, and snippets.

@damoxc
Created September 22, 2011 13:08

Revisions

  1. damoxc created this gist Sep 22, 2011.
    75 changes: 75 additions & 0 deletions create-thumbnails
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/bin/bash

    parse_time() {
    IFS=' ' read -ra PART <<< "$1"
    seconds=0
    for i in "${PART[@]}"; do
    number=$(expr match $i '\([0-9]\+\)')
    unit=$(expr match $i '[0-9]*\([a-z]\+\)')
    case $unit in
    h)
    seconds=$(($seconds+$(($number*3600))))
    ;;
    mn)
    seconds=$(($seconds+$(($number*60))))
    ;;
    *)
    seconds=$(($seconds+$number))
    ;;
    esac
    done
    echo $seconds
    }

    pretty_time() {
    seconds=$1
    hours=$(($seconds / 3600))
    seconds=$(($seconds % 3600))
    minutes=$(($seconds / 60))
    seconds=$(($seconds % 60))

    printf "%02d:%02d:%02d" $hours $minutes $seconds
    }

    genthumb() {
    VIDEO="$1"
    NAME=${VIDEO%.*}

    # Generate the media info
    mediainfo "$VIDEO" > $NAME.txt

    SECS="$(parse_time "$(grep -m 1 Duration $NAME.txt | cut -d: -f2)")"
    STEP=$(echo "($SECS / 4) - ($SECS / 4 * 0.1 / 1)" | bc)

    # Generate the thumbnails
    mplayer -quiet -vo jpeg -ss $STEP -sstep $STEP -frames 4 "$VIDEO"

    # Find out the width of the thumbnails
    WIDTH=$(identify -format %w 00000001.jpg)
    HEIGHT=$(identify -format %h 00000001.jpg)

    # Annotate the images
    for i in `seq 1 4`; do
    TIME=$(pretty_time $(($STEP*$i)))
    convert -background '#0008' -fill white -gravity south-east \
    -font /usr/share/fonts/TTF/DejaVuSans.ttf \
    -size "55x14" \
    caption:"$TIME" "0000000$i.jpg" \
    +swap -gravity south-east -composite "0000000$i.jpg.tmp"
    mv "0000000$i.jpg.tmp" "0000000$i.jpg"
    done

    # Generate the tiled image
    montage -font /usr/share/fonts/TTF/DejaVuSans.ttf \
    -border 0 -geometry ${WIDTH}x -tile 2x2 \
    0000000{1,2,3,4}.jpg ${NAME}.jpg

    # Generate the header

    # Remove the tiles
    rm 0000000{1,2,3,4}.jpg
    }

    for ARG in "$@"; do
    genthumb "$ARG"
    done;