Created
February 12, 2012 14:31
-
-
Save Cifro/1808833 to your computer and use it in GitHub Desktop.
Shell scripts for creating thumbnails from video files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Cifro Nix, http://about.me/Cifro | |
# | |
# usage: | |
# ./create-thumbnails.sh | |
# will produce thumbnails with name "<id>.jpg" with size 220x122 | |
# | |
# different size: | |
# ./create-thumbnails.sh 640x360 -big | |
# will produce thumbnails with name "<id>-big.jpg" with size 640x360 | |
videoDir=/var/www/data/videos | |
tnDir=/var/www/data/videos/.tn | |
tnSize=${1-"220x122"} | |
tnSuffix=${2-""} | |
for i in $videoDir/*.flv $videoDir/*.mp4 $videoDir/*.f4v; do | |
# filename: <videoDir>/<id>_<yyyy-mm-dd>_<video-title>.<flv|mp4|f4v> | |
vid=`echo "$i" 2>&1 | cut -d '_' -f 1 | cut -d '/' -f 6`; | |
if [ -f "$i" ] && [ ! -f "$tnDir/$vid$tnSuffix.jpg" ]; then | |
# get video duration, v1 | |
#ff=`$(ffmpeg -i "$i" 2>&1)`; | |
#d="${ff#*Duration: }" | |
#echo "${d%%,*}" | |
# get video duration, v2 | |
fulltime=`ffmpeg -i "$i" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`; | |
hour=`echo $fulltime | cut -d ':' -f 1`; | |
minute=`echo $fulltime | cut -d ':' -f 2`; | |
second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`; | |
seconds=`expr 3600 \* $hour + 60 \* $minute + $second`; | |
ss=`expr $seconds / 2`; # from the middle of video | |
#echo "$vid: $ss / $seconds"; | |
# create thumbnail from middle of video | |
output=`ffmpeg -ss $ss -i "$i" -f image2 -vframes 1 -s $tnSize "$tnDir/$vid$tnSuffix.jpg" 2>&1`; | |
if [ -f "$tnDir/$vid$tnSuffix.jpg" ]; then | |
echo "$(tput setaf 2)Thumbnail $tnDir/$vid$tnSuffix.jpg [$tnSize] saved$(tput sgr0)"; | |
else | |
echo "$(tput setaf 1)Error: Thumbnail $vid$tnSuffix.jpg [$tnSize] was not saved$(tput sgr0)"; | |
fi | |
else | |
echo "$(tput setaf 6)Notice: Thumbnail $tnDir/$vid$tnSuffix.jpg already exists.$(tput sgr0)"; | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Cifro Nix, http://about.me/Cifro | |
# | |
# usage: | |
# ./create-tn.sh 232_2011_12_03-some-video-title.mp4 | |
# will produce thumbnail "232.jpg" with size 220x122 | |
# | |
# different size: | |
# ./create-tn.sh 232_2011_12_03-some-video-title.mp4 640x360 -big | |
# will produce thumbnail "232-big.jpg" with size 640x360 | |
# fourth parameter is custom time in seconds, when will be thumbnail taken, default time is at half of video | |
videoDir=/var/www/data/videos | |
tnDir=/var/www/data/videos/.tn | |
tnSize=${2-"220x122"} | |
tnSuffix=${3-""} | |
# filename: <videoDir>/<id>_<yyyy-mm-dd>_<video-title>.<flv|mp4|f4v> | |
vid=`echo "$videoDir/$1" 2>&1 | cut -d '_' -f 1 | cut -d '/' -f 6`; | |
if [ -f "$videoDir/$1" ] && [ ! -f "$tnDir/$vid$tnSuffix.jpg" ]; then | |
fulltime=`ffmpeg -i "$videoDir/$1" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`; | |
hour=`echo $fulltime | cut -d ':' -f 1`; | |
minute=`echo $fulltime | cut -d ':' -f 2`; | |
second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`; | |
seconds=`expr 3600 \* $hour + 60 \* $minute + $second`; | |
ss=`expr $seconds / 2`; # from the middle of video | |
#echo "$vid: $ss / $seconds"; | |
#custom time from command line | |
if [ "$#" -eq 4 ]; then | |
ss=$4; | |
fi | |
# create thumbnail from middle of video | |
output=`ffmpeg -ss $ss -i "$videoDir/$1" -f image2 -vframes 1 -s $tnSize "$tnDir/$vid$tnSuffix.jpg" 2>&1`; | |
if [ -f "$tnDir/$vid$tnSuffix.jpg" ]; then | |
echo "$(tput setaf 2)Thumbnail $tnDir/$vid$tnSuffix.jpg [$tnSize] saved$(tput sgr0)"; | |
else | |
echo "$(tput setaf 1)Error: Thumbnail $vid$tnSuffix.jpg [$tnSize] was not saved$(tput sgr0)"; | |
fi | |
else | |
echo "$(tput setaf 6)Notice: Thumbnail $tnDir/$vid$tnSuffix.jpg already exists.$(tput sgr0)"; | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Cifro Nix, http://about.me/Cifro | |
# | |
# Script for generating thumbnails, called from PHP | |
# | |
# usage in PHP: | |
# $screenshot = trim(shell_exec("/var/www/bin/tn.sh $videofile &")); | |
videoDir=/var/www/data/videos | |
tnDir=/var/www/data/videos/.tn | |
preview="220x122" | |
standard="640x360" | |
hd="1280x720" | |
if [ -f "$videoDir/$1" ]; then | |
# filename: <videoDir>/<id>_<yyyy-mm-dd>_<video-title>.<flv|mp4|f4v> | |
vid=`echo "$videoDir/$1" 2>&1 | cut -d '_' -f 1 | cut -d '/' -f 6`; | |
size=`ffmpeg -i "$videoDir/$1" 2>&1 | grep 'Video: ' | cut -d ',' -f 3 | cut -d '[' -f 1 | sed 's/^ *\(.*\) *$/\1/'`; | |
fulltime=`ffmpeg -i "$videoDir/$1" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`; | |
hour=`echo $fulltime | cut -d ':' -f 1`; | |
minute=`echo $fulltime | cut -d ':' -f 2`; | |
second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`; | |
seconds=`expr 3600 \* $hour + 60 \* $minute + $second`; | |
ss=`expr $seconds / 2`; # from the middle of video | |
#echo "$vid: $ss / $seconds"; | |
#custom time from command line | |
if [ "$#" -eq 2 ]; then | |
ss=$2; | |
fi | |
# create thumbnail from middle of video | |
# thumbnails will be allways overwrited | |
output=`ffmpeg -ss $ss -i "$videoDir/$1" -f image2 -vframes 1 -s $preview "$tnDir/$vid.jpg" 2>&1`; | |
output=`ffmpeg -ss $ss -i "$videoDir/$1" -f image2 -vframes 1 -s $standard "$tnDir/$vid-v.jpg" 2>&1`; | |
# needs space, "trim" with sed regexp doesnt work well | |
if [ "$size" == "$hd " ]; then | |
output=`ffmpeg -ss $ss -i "$videoDir/$1" -f image2 -vframes 1 -s $hd "$tnDir/$vid-hd.jpg" 2>&1`; | |
fi | |
if [ -f "$tnDir/$vid.jpg" ] && [ -f "$tnDir/$vid-v.jpg" ] && [ -f "$tnDir/$vid-hd.jpg" ] && [ "$size" == "$hd " ]; then | |
echo "ok"; | |
elif [ -f "$tnDir/$vid.jpg" ] && [ -f "$tnDir/$vid-v.jpg" ]; then | |
echo "ok"; | |
else | |
echo "failed"; | |
fi | |
else | |
echo "nonexists"; | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Cifro Nix, http://about.me/Cifro | |
# | |
# Script for batch generating thumbnails. | |
# | |
# usage in PHP: | |
# ./tns.sh | |
videoDir=/var/www/data/videos | |
tnDir=/var/www/data/videos/.tn | |
preview="220x122" | |
standard="640x360" | |
hd="1280x720" | |
for i in $videoDir/*.flv $videoDir/*.mp4 $videoDir/*.f4v; do | |
# filename: <videoDir>/<id>_<yyyy-mm-dd>_<video-title>.<flv|mp4|f4v> | |
vid=`echo "$i" 2>&1 | cut -d '_' -f 1 | cut -d '/' -f 6`; | |
if [ -f "$i" ] && [ ! -f "$tnDir/$vid.jpg" ] && [ ! -f "$tnDir/$vid-v.jpg" ]; then | |
size=`ffmpeg -i "$i" 2>&1 | grep 'Video: ' | cut -d ',' -f 3 | cut -d '[' -f 1 | sed 's/^ *\(.*\) *$/\1/'`; | |
# get video duration, v2 | |
fulltime=`ffmpeg -i "$i" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`; | |
hour=`echo $fulltime | cut -d ':' -f 1`; | |
minute=`echo $fulltime | cut -d ':' -f 2`; | |
second=`echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1`; | |
seconds=`expr 3600 \* $hour + 60 \* $minute + $second`; | |
ss=`expr $seconds / 2`; # from the middle of video | |
#echo "$vid: $ss / $seconds"; | |
# create thumbnail from middle of video | |
output=`ffmpeg -ss $ss -i "$i" -f image2 -vframes 1 -s $preview "$tnDir/$vid.jpg" 2>&1`; | |
output=`ffmpeg -ss $ss -i "$i" -f image2 -vframes 1 -s $standard "$tnDir/$vid-v.jpg" 2>&1`; | |
if [ "$size" == "$hd " ] && [ ! -f "$tnDir/$vid-hd.jpg" ]; then | |
output=`ffmpeg -ss $ss -i "$i" -f image2 -vframes 1 -s $hd "$tnDir/$vid-hd.jpg" 2>&1`; | |
fi | |
if [ -f "$tnDir/$vid.jpg" ] && [ -f "$tnDir/$vid-v.jpg" ] && [ -f "$tnDir/$vid-hd.jpg" ] && [ "$size" == "$hd " ]; then | |
echo "$(tput setaf 2)Thumbnails preview, standard and HD for [$vid] saved$(tput sgr0)"; | |
elif [ -f "$tnDir/$vid.jpg" ] && [ -f "$tnDir/$vid-v.jpg" ]; then | |
echo "$(tput setaf 2)Thumbnails preview and standard for [$vid] saved$(tput sgr0)"; | |
else | |
echo "$(tput setaf 1)Error: Thumbnails for [$vid] were not saved$(tput sgr0)"; | |
fi | |
else | |
echo "$(tput setaf 6)Notice: Thumbnails preview and standard for [$vid] already exists.$(tput sgr0)"; | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting error as below
Notice: Thumbnail /var/www/data/videos/.tn/232.jpg already exists.
Could you suggest me what is wrong?