Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mibrahimdev/56d45b7ca68f23afa2268040666cb480 to your computer and use it in GitHub Desktop.
Save mibrahimdev/56d45b7ca68f23afa2268040666cb480 to your computer and use it in GitHub Desktop.
V2G.sh
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title V2G
# @raycast.mode compact
# Optional parameters:
# @raycast.icon 📸
# Documentation:
# @raycast.description Video to Gif
# @raycast.author Mohamed Ibrahim
# @raycast.authorURL https://github.com/MohamedISoliman
# @raycast.icon badge
# @raycast.argument1 { "type": "text", "placeholder": "Video Path"}
# Check if ffmpeg and gifsicle are installed
if ! command -v ffmpeg &> /dev/null || ! command -v gifsicle &> /dev/null; then
echo "ffmpeg and gifsicle must be installed. Please install them."
exit 1
fi
# Check input argument
if [ -z "$1" ]; then
echo "An input video path must be provided."
exit 1
fi
# Assigning argument to variable
INPUT_VIDEO="$1"
OUTPUT_GIF="${INPUT_VIDEO%.*}.gif" # Initial output filename based on the video file
# Function to calculate scaling while maintaining aspect ratio
function calc_scale {
local width=640 # Desired width
local dims=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$INPUT_VIDEO")
local src_width=${dims%x*}
local src_height=${dims#*x}
local scale_height=$((src_height * width / src_width))
echo "${width}:${scale_height}" # Output format: width:height
}
# Ensure the GIF does not overwrite an existing file by appending a number
count=1
while [[ -f "$OUTPUT_GIF" ]]; do
OUTPUT_GIF="${INPUT_VIDEO%.*}_${count}.gif"
((count++))
done
# Get scaling dimensions preserving aspect ratio
SCALING=$(calc_scale)
# Conversion processing from video to GIF with quality optimizations
ffmpeg -i "$INPUT_VIDEO" -vf "fps=20,scale=$SCALING:flags=lanczos" -loop 0 -f gif - | gifsicle --optimize=3 --colors 256 --delay=5 > "$OUTPUT_GIF"
echo "GIF created at: $OUTPUT_GIF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment