Initialised a git hook for pre commit in client side Need to establish some sort of server connection to make it available on the server side and need to prepare so that it also checks if the server is indeed capable with FFMPEG(libopus) and IMAGEMAGICK (6 preferably) to intialise the image and provide a reasonable way to implementation
178 lines
5.1 KiB
Bash
Executable File
178 lines
5.1 KiB
Bash
Executable File
#!/bin/sh
|
|
GITDIR=$(git rev-parse --show-toplevel)
|
|
|
|
# TODO : MIGHT need a failsafe to skip extremely large files, maybe less than 100mb is fine
|
|
# TODO : MIGHT also implement a filename "uncompressed-*" to end up without any compression
|
|
# TODO : MIGHT need to implement replacement of files with spaces to dashes
|
|
# TODO : MIGHT need to implement same file realiser, two filenames with the same will lead to some errors
|
|
# TODO : MIGHT be able to export with functions and eval parameters to reduce this weird repitition (PRIORITY)
|
|
|
|
# Installing webp images with assets/images
|
|
WEBP_IMAGES=$(git diff --name-only --cached | grep -E "\.(webp|WEBP)$")
|
|
NON_WEBP_IMAGES=$(git diff --name-only --cached | grep -E "\.(jpg|png|JPG|PNG)$")
|
|
IMG_LIMIT=150
|
|
|
|
for filename in $WEBP_IMAGES; do
|
|
QUALITY=80
|
|
SIZE=100
|
|
UNSUFFIXED_FILENAME=${filename%.*}
|
|
|
|
git restore --staged "$GITDIR/$filename"
|
|
|
|
# One conversion atleast
|
|
convert $GITDIR/$filename \
|
|
-resize $SIZE% \
|
|
-quality $QUALITY \
|
|
-format "webp" \
|
|
$GITDIR/$UNSUFFIXED_FILENAME.webp;
|
|
|
|
while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ] && \
|
|
[ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.webp" | \
|
|
awk -F' ' '{print $1}') -ge $IMG_LIMIT ] && \
|
|
[ $(($SIZE > 0)) ]; do
|
|
|
|
SIZE=$(($SIZE - 20))
|
|
|
|
convert $GITDIR/$UNSUFFIXED_FILENAME.webp \
|
|
-resize $SIZE% \
|
|
-quality $QUALITY \
|
|
-format "webp" \
|
|
$GITDIR/$UNSUFFIXED_FILENAME.webp;
|
|
done;
|
|
done;
|
|
|
|
|
|
|
|
for filename in $NON_WEBP_IMAGES; do
|
|
QUALITY=80
|
|
SIZE=100
|
|
UNSUFFIXED_FILENAME=${filename%.*}
|
|
|
|
git restore --staged "$GITDIR/$filename"
|
|
|
|
if [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ]
|
|
then
|
|
rm "$GITDIR/$filename"
|
|
echo "SKIPPING $filename : the filename already exists"
|
|
continue
|
|
fi
|
|
|
|
# One conversion atleast
|
|
convert $GITDIR/$filename \
|
|
-resize $SIZE% \
|
|
-quality $QUALITY \
|
|
-format "webp" \
|
|
$GITDIR/$UNSUFFIXED_FILENAME.webp;
|
|
|
|
rm "$GITDIR/$filename"
|
|
|
|
while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ] && \
|
|
[ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.webp" | \
|
|
awk -F' ' '{print $1}') -ge $IMG_LIMIT ] && \
|
|
[ $(($SIZE > 20)) ]; do
|
|
|
|
SIZE=$(($SIZE - 20))
|
|
|
|
convert $GITDIR/$UNSUFFIXED_FILENAME.webp \
|
|
-resize $SIZE% \
|
|
-quality $QUALITY \
|
|
-format "webp" \
|
|
$GITDIR/$UNSUFFIXED_FILENAME.webp;
|
|
done;
|
|
done;
|
|
|
|
# Installing opus audio with assets/audio
|
|
NON_OPUS_AUDIOS=$(git diff --name-only --cached | grep -E "\.(mp3|wav|flac|m4a|ogg|wma)")
|
|
OPUS_AUDIOS=$(git diff --name-only --cached | grep -E "\.opus")
|
|
AUDIO_LIMIT=64
|
|
|
|
|
|
for filename in $OPUS_AUDIOS ; do
|
|
COMPRESSION_LEVEL=10
|
|
UNSUFFIXED_FILENAME=${filename%.*}
|
|
BITRATE=128
|
|
|
|
git restore --staged "$GITDIR/$filename"
|
|
|
|
# One conversion atleast
|
|
# This will do opus to opus with decoder
|
|
# Degrading once quality but we tank it
|
|
ffmpeg -hide_banner -y -loglevel warning \
|
|
-i "$GITDIR/$filename" \
|
|
-codec:a libopus \
|
|
-compression_level $COMPRESSION_LEVEL \
|
|
-vbr on \
|
|
"$GITDIR/$UNSUFFIXED_FILENAME-temp.opus";
|
|
|
|
mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus"
|
|
|
|
while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ] && \
|
|
[ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.opus" | \
|
|
awk -F' ' '{print $1}') -ge $AUDIO_LIMIT ] && \
|
|
[ $COMPRESSION_LEVEL -ge 3 ]; do
|
|
|
|
COMPRESSION_LEVEL=$(($COMPRESSION_LEVEL - 3))
|
|
BITRATE=$(($BITRATE / 2))
|
|
|
|
ffmpeg -hide_banner -y -loglevel warning \
|
|
-y -i "$GITDIR/$UNSUFFIXED_FILENAME.opus" \
|
|
-codec:a libopus \
|
|
-compression_level $COMPRESSION_LEVEL \
|
|
-vbr constrained \
|
|
-b:a "${BITRATE}k" \
|
|
"$GITDIR/$UNSUFFIXED_FILENAME-temp.opus";
|
|
|
|
mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus";
|
|
done;
|
|
done;
|
|
|
|
|
|
for filename in $NON_OPUS_AUDIOS ; do
|
|
COMPRESSION_LEVEL=10
|
|
UNSUFFIXED_FILENAME=${filename%.*}
|
|
BITRATE=128
|
|
|
|
git restore --staged "$GITDIR/$filename"
|
|
|
|
if [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ]
|
|
then
|
|
rm "$GITDIR/$filename"
|
|
echo "SKIPPING $filename : the filename already exists"
|
|
continue
|
|
fi
|
|
|
|
# One conversion atleast
|
|
# This will do opus to opus with decoder
|
|
# Degrading once quality but we tank it
|
|
ffmpeg -hide_banner -y -loglevel warning \
|
|
-i "$GITDIR/$filename" \
|
|
-codec:a libopus \
|
|
-compression_level $COMPRESSION_LEVEL \
|
|
-vbr on \
|
|
"$GITDIR/$UNSUFFIXED_FILENAME-temp.opus";
|
|
|
|
mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus"
|
|
rm "$GITDIR/$filename"
|
|
|
|
while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ] && \
|
|
[ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.opus" | \
|
|
awk -F' ' '{print $1}') -ge $AUDIO_LIMIT ] && \
|
|
[ $COMPRESSION_LEVEL -ge 3 ]; do
|
|
|
|
COMPRESSION_LEVEL=$(($COMPRESSION_LEVEL - 3))
|
|
BITRATE=$(($BITRATE / 2))
|
|
|
|
ffmpeg -hide_banner -y -loglevel warning \
|
|
-y -i "$GITDIR/$UNSUFFIXED_FILENAME.opus" \
|
|
-codec:a libopus \
|
|
-compression_level $COMPRESSION_LEVEL \
|
|
-vbr constrained \
|
|
-b:a "${BITRATE}k" \
|
|
"$GITDIR/$UNSUFFIXED_FILENAME-temp.opus";
|
|
|
|
mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus";
|
|
done;
|
|
done;
|
|
|
|
git add $GITDIR
|