Last active
July 16, 2023 19:26
-
-
Save fabiotnt/288fcb39c30f8091f5e04d1b519fdd68 to your computer and use it in GitHub Desktop.
Simple Bash Script using FFMpeg to convert MP4 files to GIF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Check Dependecies | |
if ! command -v ffmpeg &> /dev/null | |
then | |
echo "ffmpeg command could not be found. It's a dependency to run this script" | |
exit | |
fi | |
if ! command -v ffprobe &> /dev/null | |
then | |
echo "ffprobe command could not be found. It's a dependency to run this script" | |
exit | |
fi | |
if ! command -v jq &> /dev/null | |
then | |
echo "jq command could not be found. It's a dependency to run this script" | |
exit | |
fi | |
if ! command -v bc &> /dev/null | |
then | |
echo "bc command could not be found. It's a dependency to run this script" | |
exit | |
fi | |
gifWidth=640 | |
gifHeight=480 | |
#Catch all matches and convert | |
for inputFile in *.mp4; do | |
if [ -f "$inputFile" ]; then | |
baseName=${inputFile##*/} | |
baseName=${baseName%%.*} | |
ext=${inputFile##*.} | |
#Video Info from FFProbe | |
videoData=`(ffprobe -v error -select_streams v:0 -show_format -show_entries format=size,duration,filename:stream=display_aspect_ratio,width,height,r_frame_rate,bit_rate,codec_name,duration -print_format json $inputFile)` | |
width=`(echo $videoData | jq '.["streams"][0]["width"]')` | |
height=`(echo $videoData | jq '.["streams"][0]["height"]')` | |
aratio=`(echo $videoData | jq '.["streams"][0]["display_aspect_ratio"]')` | |
duration=`(echo $videoData | jq '.["streams"][0]["duration"]' | bc)` | |
bit_rate=`(echo $videoData | jq '.["streams"][0]["bit_rate"]' | bc)` | |
bit_rate_M=`echo $(($bit_rate / 1024))` | |
minSize=$([ $width -le $height ] && echo "$gifWidth" || echo "$gifHeight") | |
maxSize=$([ $width -le $height ] && echo "$gifHeight" || echo "$gifWidth") | |
ffmpeg -i "$inputFile" -filter_complex "[0:v] fps=12,scale=w=$maxSize:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[b][p] paletteuse=new=1" "$baseName.gif" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment