Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created July 22, 2024 03:55
Show Gist options
  • Save HirbodBehnam/d2d5bc85496bcb3e810e0fa5009167b0 to your computer and use it in GitHub Desktop.
Save HirbodBehnam/d2d5bc85496bcb3e810e0fa5009167b0 to your computer and use it in GitHub Desktop.
Archive media to other formats for better size
#!/bin/bash
DESTINATION="$1/"
if [[ "$DESTINATION" == "" ]]; then
echo "Please provide destination in arguments."
exit 1
fi
echo "Compressing files from $PWD to $DESTINATION"
true > errors.txt
# Iterate in folders
find . -type d | while read -r directory; do
# Create this folder in the destination
mkdir "$DESTINATION$directory"
# In that folder, look through the files
find "$directory" -maxdepth 1 -type f | while read -r filename; do
# Check the extension of the file
if [[ $filename == *.jpg ]]; then
# Based on what my mom said is good
if ! ffmpeg -y -i "$filename" -c:v libsvtav1 -crf 24 -preset 8 "$DESTINATION/$filename.avif" < /dev/null; then
echo "Error on file $filename" >> errors.txt
fi
elif [[ $filename == *.mov || $filename == *.mp4 || $filename == *.vob ]]; then
# OPUS 160 should deliver very good audio quality in any circumstances.
# On the meantime, HEVC is also a very good video compression algorithm. It's fast and widely supported.
# CRF of 22 should also perform very good and almost look identical. This can be tweeked a little to 20 or such to give better quality.
if ! ffmpeg -y -i "$filename" -c:a libopus -b:a 160K -c:v libx265 -crf 22 -tag:v hvc1 "$DESTINATION/$filename.mkv" < /dev/null; then
echo "Error on file $filename" >> errors.txt
fi
elif [[ $filename == *.wav ]]; then
# I don't like to touch MP3 audio files. Instead, I'll just change the codec of recorded audio files
# (a.k.a. wav files) to ogg and 128K OPUS. Should deliver VERY sharp audio quality.
if ! ffmpeg -y -i "$filename" -c:a libopus -b:a 128K "$DESTINATION/$filename.ogg" < /dev/null; then
echo "Error on file $filename" >> errors.txt
fi
else
# Otherwise, just copy the file and don't do anything
cp "$filename" "$DESTINATION/$filename"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment