Skip to content

Instantly share code, notes, and snippets.

@toFrankie
Created June 1, 2024 12:43
Show Gist options
  • Save toFrankie/39268cb232e2b7ee3f4f74af24938d8e to your computer and use it in GitHub Desktop.
Save toFrankie/39268cb232e2b7ee3f4f74af24938d8e to your computer and use it in GitHub Desktop.
Used to obtain GIF image information
function gifinfo() {
# 检查是否安装了 ImageMagick
if ! command -v identify &>/dev/null; then
echo -e "\033[31mError: ImageMagick is not installed.\033[0m"
return 1
fi
local num_frames=-1
local filename=""
# 解析参数
while [[ $# -gt 0 ]]; do
case $1 in
-n)
shift
if [[ $# -gt 0 ]]; then
num_frames=$1
else
echo -e "\033[31mError: -n requires a number argument.\033[0m"
return 1
fi
;;
-*)
echo -e "\033[31mUsage: gifduration [-n number_of_frames] <filename>\033[0m"
return 1
;;
*)
filename=$1
;;
esac
shift
done
if [ -z "$filename" ]; then
echo -e "\033[31mUsage: gifinfo [-n number_of_frames] <filename>\033[0m"
return 1
fi
# 确定临时目录
if [[ "$OSTYPE" == "darwin"* || "$OSTYPE" == "linux-gnu"* ]]; then
temp_dir="/tmp"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
temp_dir="$TEMP"
else
echo "Error: Unsupported operating system."
return 1
fi
# 如果 filename 是 URL,下载到本地
if [[ "$filename" == http* ]]; then
local timestamp=$(date +%s%3N)
local temp_filename="${temp_dir}/gifinfo_temp_${timestamp}.gif"
curl -sL "$filename" -o "$temp_filename" || {
echo -e "\033[31mError: Failed to download GIF from $filename.\033[0m"
return 1
}
filename="$temp_filename"
fi
# https://imagemagick.org/script/escape.php
local info=$(magick identify -format "%w,%h,%s,%T,%n\n" "$filename")
# 文件大小
local filesize
if [[ "$OSTYPE" == "darwin"* ]]; then
filesize=$(stat -f%z "$filename")
else
filesize=$(stat -c%s "$filename")
fi
local filesize_kb=$(echo "scale=2; $filesize / 1024" | bc)
local filesize_mb=$(echo "scale=2; $filesize / 1048576" | bc)
local filesize_display
if (($(echo "$filesize_mb >= 1" | bc -l))); then
filesize_display="${filesize_mb}MB"
else
filesize_display="${filesize_kb}KB"
fi
echo -e "\033[1mSize: \033[32m${filesize_display}\033[0m"
# 宽高
local dimensions=$(echo "$info" | awk -F ',' '{if($1>w) w=$1; if($2>h) h=$2} END {print w "×" h}')
echo -e "\033[1mDimensions: \033[32m${dimensions}\033[0m"
# 总时长
local duration=$(echo "$info" | awk -F ',' '{sum+=$4} END {print sum/100}')
echo -e "\033[1mDuration: \033[32m${duration}s\033[0m"
# 总帧数
local total_frames=$(echo "$info" | head -n 1 | awk -F ',' '{print $5}')
echo -e "\033[1mFrames: \033[32m${total_frames}\033[0m"
# 每帧 delay time
echo -e "\033[1mFrame delays:\033[0m"
if [ "$num_frames" -gt 0 ]; then
echo "$info" | head -n "$num_frames" | awk -F ',' '{printf " \033[2m#%d:\033[0m \033[32m%.2fs\033[0m\n", NR-1, $4/100}'
local remaining_frames=$((total_frames - num_frames))
if [ "$remaining_frames" -gt 0 ]; then
local remaining_duration=$(echo "$info" | tail -n "$remaining_frames" | awk -F ',' '{sum+=$4} END {print sum/100}')
echo -e "\033[2m ${remaining_frames} frames (${remaining_duration}s in total) are not displayed.\033[0m"
fi
else
echo "$info" | awk -F ',' '{printf " \033[2m#%d:\033[0m \033[32m%.2fs\033[0m\n", NR-1, $4/100}'
fi
# 删除临时文件
if [[ "$filename" == "${temp_dir}/"* ]]; then
rm -f "$filename"
fi
}
@toFrankie
Copy link
Author

toFrankie commented Jun 1, 2024

Add this to your .bash_profile or .zshrc, then source it.

$ gifinfo [-n number_of_frames] <filename>

The filename supports local or network image.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment