Skip to content

Instantly share code, notes, and snippets.

@garywill
Last active April 11, 2024 07:03
Show Gist options
  • Save garywill/f9e0bd988d80f5cc915e822233f0b352 to your computer and use it in GitHub Desktop.
Save garywill/f9e0bd988d80f5cc915e822233f0b352 to your computer and use it in GitHub Desktop.
imagemagick batch compress jpg photo, output to another path. Keep original folder and sub-folders structure. And video (ffmpeg h265)
#!/bin/bash
# 用法 usage:
# shell置于总放照片的目录之上 put this shell in a path containing original photos (photos are here or in subfolders)
# 脚本 目标文件夹 源文件夹1 源文件夹2 源文件夹3 .....
# scriptname <dest> <source1> <source2> <source3> ....
function get_filename_from_path()
{
echo ${1##*/}
}
function remove_filename_from_path()
{
echo ${1%/*}
}
function remove_filename_suffix()
{
echo "${1%.*}"
}
DEST_F="$1"
shift
echo "Destination: $DEST_F"
while [[ "$1" ]] ;
do
PSOURCE="$1"
mkdir -p "$DEST_F/$PSOURCE"
echo
echo "Photos source: $PSOURCE"
find "$PSOURCE" -mindepth 1 -maxdepth 1 -type f | grep -i "\.jpg$" | while read line ;
do
filename_full=$(get_filename_from_path "$line")
filename=$(remove_filename_suffix "$filename_full")
echo -n "Converting $PSOURCE/$filename "
echo -n $(du -k "$PSOURCE/$filename_full" | cut -f1)k
echo -n " -> "
convert -quality 40 "$PSOURCE/$filename_full" \
"$DEST_F/$PSOURCE/$filename_full"
echo $(du -k "$DEST_F/$PSOURCE/$filename_full" | cut -f1)k
done
shift
done
#!/bin/bash
# 用法:
# shell置于总放照片的目录之上 需要vfile.txt
# 找所有视频 find . -type f -exec file -N -i -- {} + | sed -n 's!: video/[^:]*$!!p' 要删掉开头的./再存成vfile.txt
# 脚本 目标文件夹
function get_filename_from_path()
{
echo "${1##*/}"
}
function remove_filename_from_path()
{
if [[ "$1" =~ '/' ]]; then
echo "${1%/*}"
else
echo ""
fi
}
function remove_filename_suffix()
{
echo "${1%.*}"
}
DEST_F="$1"
shift
echo "Destination: $DEST_F"
while read -r line ;
do
filename_full=$(get_filename_from_path "$line")
filename=$(remove_filename_suffix "$filename_full")
PSOURCE=$(remove_filename_from_path "$line")
mkdir -p "$DEST_F/$PSOURCE"
echo "Converting $line"
ffmpeg -nostdin -hide_banner -i "$line" \
-c:v libx265 -c:a aac -preset veryslow \
"$DEST_F/$PSOURCE/$filename.mkv" >/dev/null 2>&1
echo -n "Convertion done $(du -k "$line" | cut -f1)k"
echo -n " -> "
echo "$(du -k "$DEST_F/$PSOURCE/$filename.mkv" | cut -f1)k"
sleep 150
done < vfiles.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment