Skip to content

Instantly share code, notes, and snippets.

@wilwade
Created January 21, 2014 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilwade/8534145 to your computer and use it in GitHub Desktop.
Save wilwade/8534145 to your computer and use it in GitHub Desktop.
This will generate a png with frames from your video. Originally created for blog to book printing for videos in blog.
#!/bin/bash
#
# This will generate a png with frames from your video. Originally created for blog to book printing for videos in blog.
# Watchout in this current version it requires a large temp space as each frame is output into a separate file first.
#
#
# GPL'd. If you do not know what that means visit http://www.gnu.org/licenses/gpl.html
# Required programs. mplayer, ImageMagick, BASH, GNU Core Tools (Like sed, grep, etc...)
#
# Author: Wil Wade
# Contact: wil@WilWade.com
if [ -z $1 ]
then
echo "Create a film strip from a video pulling frames from video at needed increments"
echo "At least one argument is required. First is the video file. Others are optional."
echo "Usage: framesGenerator.sh videofile columns rows frameWidth frameHeight"
echo "Will output videofile.png in the same folder as the video"
exit
fi
#Set Defaults
if [ -z $2 ]
then
WIDE=5
else
WIDE=$2
fi
if [ -z $3 ]
then
TALL=5
else
TALL=$3
fi
if [ -z $4 ]
then
FRAMEW=400
else
FRAMEW=$4
fi
if [ -z $5 ]
then
FRAMEH=400
else
FRAMEH=$5
fi
#How many seconds long to stop mplayer at the end
LENGTH=$(midentify $1 | grep ID_LENGTH | sed 's/ID_LENGTH=//')
echo "Length=$LENGTH"
mplayer -nosound $1 -vo png -endpos $LENGTH
# Here we hope that there are no other files that are pngs which have filenames of just 8 numbers.
# If so they will be deleted at the end of the script.
ls [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].png > framelist.txt
# Here we get the count of the number of frames, by just counting the number of lines in the file
PNGCOUNT=$(wc -l framelist.txt | sed 's/ framelist.txt//')
# How many frames do we need to make sure that each spot on the determined grid is filled?
NEEDEDFRAMES=$(($WIDE*$TALL))
# Note that we get rid of the last 10 frames,
# because often they are just a fadeout,
# and if the number of frames is directly divisable
# by the number of needed frames, then we would pull the very last frame.
FREQ=$(($(($PNGCOUNT-10))/$NEEDEDFRAMES))
echo "Frequency=$FREQ"
echo "Frames for Grid=$NEEDEDFRAMES"
echo "Grid Dimentions="$WIDE"x"$TALL""
# Here in the for loop we use an array to store the file names,
# if your version of bash does not support arrays (I think it is version 2 and below)
# then sorry.
for ((i=0; i<=$NEEDEDFRAMES; i++))
do
LINE=$(($i*$FREQ))
FILES[$i]=$(head -$LINE framelist.txt | tail -1)
done
montage ${FILES[*]} -geometry "$FRAMEW"x"$FRAMEH"+"$WIDE"+"$TALL" $1.png
#Cleanup
rm $(cat framelist.txt)
rm framelist.txt
echo "done. View file $1.png to see the result!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment