From Tim's website
Jump to: navigation, search
(New page: === Resize and compress all images in a directory === The following command will using the imagemagick command to resize and recompress images to a small file: mogrify -resize 800x800 -qu...)
 
Line 1: Line 1:
=== Resize and compress all images in a directory ===
+
=== Saving space by compressing images ===
  +
The following line finds .JPG files (case sensitive) below the current directory between 3MB and 4MB and passes them to a compression script in /srv/:
  +
find . -name '*.JPG' -size 4M -execdir /srv/compress_jpg "{}" \;
  +
My script tries compressing to 90% quality (very high), and keeps the result if it saves more than 30%
  +
#!/bin/sh
  +
# This script requires one argument - the image to compress
  +
  +
convert "$1" -quality 90 temp.JPG
  +
  +
ratio=$(echo "`stat -c%s temp.JPG`*100/`stat -c%s \"$1\"`" | bc)
  +
useful=$(echo "$ratio<70" | bc)
  +
  +
if [ $useful == 0 ]; then
  +
echo "Image \'$1\' is already compressed efficiently"
  +
rm temp.JPG
  +
else
  +
echo "Compressing \'$1\' at 90% quality to $ratio% of original size"
  +
mv temp.JPG "$1"
  +
fi
  +
  +
=== Saving space by compressing videos ===
  +
The following line finds .AVI files (case sensitive) below the current directory and passes them to a compression script in /srv/:
  +
find . -name '*.AVI' -execdir /srv/compress_avi "{}" \;
  +
My script works on videos that are not already x264 compressed, uses 500kbps for 320x240 video and 1500kbps for all other sizes
  +
#!/bin/sh
  +
# This script requires one argument - the video to re-encode using x264
  +
file $1 | grep H.264 > /dev/null
  +
if [ $? == 0 ]; then
  +
echo "Video $1 is already compressed with x264"
  +
else
  +
file $1 | grep "320 x 240" > /dev/null
  +
if [ $? == 0 ]; then
  +
echo "Video $1 is 320 x 240, using 500 bps"
  +
bitrate=500
  +
else
  +
echo "Video $1 is not 320 x 240, using 1500 bps"
  +
bitrate=1500
  +
fi
  +
  +
mkdir recoded
  +
nice mencoder -ovc x264 -x264encopts pass=1:threads=auto:frameref=3:turbo=1:bitrate=$bitrate -nosound -o /dev/null $1
  +
nice mencoder -ovc x264 -x264encopts pass=2:threads=auto:frameref=6:subq=6:qcomp=0.8:me=umh:bitrate=$bitrate -oac copy -o recoded/$1 $1
  +
rm divx2pass.log
  +
  +
fi
  +
  +
=== Creating index images ===
 
The following command will using the imagemagick command to resize and recompress images to a small file:
 
The following command will using the imagemagick command to resize and recompress images to a small file:
 
mogrify -resize 800x800 -quality 10 -monitor *
 
mogrify -resize 800x800 -quality 10 -monitor *

Revision as of 16:02, 9 August 2010

Saving space by compressing images

The following line finds .JPG files (case sensitive) below the current directory between 3MB and 4MB and passes them to a compression script in /srv/:

find . -name '*.JPG' -size 4M -execdir /srv/compress_jpg "{}" \;

My script tries compressing to 90% quality (very high), and keeps the result if it saves more than 30%

#!/bin/sh
# This script requires one argument - the image to compress

convert "$1" -quality 90 temp.JPG

ratio=$(echo "`stat -c%s temp.JPG`*100/`stat -c%s \"$1\"`" | bc)
useful=$(echo "$ratio<70" | bc)

if [ $useful == 0 ]; then
  echo "Image \'$1\' is already compressed efficiently"
  rm temp.JPG
else
  echo "Compressing \'$1\' at 90% quality to $ratio% of original size"
  mv temp.JPG "$1"
fi

Saving space by compressing videos

The following line finds .AVI files (case sensitive) below the current directory and passes them to a compression script in /srv/:

find . -name '*.AVI' -execdir /srv/compress_avi "{}" \;

My script works on videos that are not already x264 compressed, uses 500kbps for 320x240 video and 1500kbps for all other sizes

#!/bin/sh
# This script requires one argument - the video to re-encode using x264
file $1 | grep H.264 > /dev/null
if [ $? == 0 ]; then
  echo "Video $1 is already compressed with x264"
else
  file $1 | grep "320 x 240" > /dev/null
  if [ $? == 0 ]; then
     echo "Video $1 is 320 x 240, using 500 bps"
     bitrate=500
  else
     echo "Video $1 is not 320 x 240, using 1500 bps"
     bitrate=1500
  fi

  mkdir recoded
  nice mencoder -ovc x264 -x264encopts pass=1:threads=auto:frameref=3:turbo=1:bitrate=$bitrate -nosound -o /dev/null $1
  nice mencoder -ovc x264 -x264encopts pass=2:threads=auto:frameref=6:subq=6:qcomp=0.8:me=umh:bitrate=$bitrate -oac copy -o recoded/$1 $1
  rm divx2pass.log

fi

Creating index images

The following command will using the imagemagick command to resize and recompress images to a small file:

mogrify -resize 800x800 -quality 10 -monitor *