Skip to content

Commit

Permalink
Cleanup code descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvellu committed Jan 16, 2022
1 parent ecbb039 commit 6612542
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions gif.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh

# Converts any video to animated gif
# Converts video to high-quality animated GIF using FFmpeg

# Script parameters
usage() {
echo "Usage:";
echo "\t./gif.sh [<options>] -i input.avi -o output.gif";
Expand All @@ -20,16 +21,10 @@ usage() {

# Temporary palette file
PALETTE="/tmp/palette.png"

# Set start time
START=00:00

## Speed up or slow down
#SETPTS="0.4*PTS"
## Set crop area
#CROP="440:440:135:15"
## Scale if needed
#SCALE="440:-1:flags=lanczos"

# Parse arguments
while getopts "i:o:s:d:f:x:" option; do
case "${option}" in
Expand All @@ -43,22 +38,25 @@ while getopts "i:o:s:d:f:x:" option; do
esac
done

# Abort without required arguments
# TODO: Implement speed up or slow down using SETPTS="0.4*PTS"
# TODO: Define cropping area using CROP="440:440:135:15"

# Abort if required arguments are missing
if [ -z "${INPUT}" ] || [ -z "${OUTPUT}" ]; then
usage
fi

# Fetch duration from video if not provided
# Fetch duration from video, if not provided
if [ -z "${DURATION}" ]; then
DURATION=`ffmpeg -i ${INPUT} 2>&1 | grep -oE 'Duration: [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}.[[:digit:]]{2}' | cut -d : -f 4`
fi

# Fetch fps from video if not provided
# Fetch fps from video, if not provided
if [ -z "${FPS}" ]; then
FPS=`ffmpeg -i ${INPUT} 2>&1 | grep -oE '[[:digit:]]{1,4} fps' | cut -d ' ' -f 1`
fi

# Fetch width from video if not provided
# Fetch width from video, if not provided
if [ -z "${SCALE}" ]; then
SCALE=`ffmpeg -i ${INPUT} 2>&1 | grep -oE ', [[:digit:]]{1,4}x[[:digit:]]{1,4}' | cut -d ' ' -f 2 | cut -d 'x' -f 1`
fi
Expand All @@ -73,11 +71,11 @@ echo "fps = ${FPS}"
echo "width = ${SCALE}px"

# Apply filters
#filters="fps=15,setpts=0.4*PTS,crop=440:440:135:15,scale=440:-1:flags=lanczos"
FILTERS="fps=${FPS},scale=${SCALE}:-1:flags=lanczos"

# Run two pass conversion
echo "\nProcessing..."
ffmpeg -v warning -ss ${START} -t ${DURATION} -i ${INPUT} -vf "$FILTERS,palettegen" -y ${PALETTE}
ffmpeg -v warning -ss ${START} -t ${DURATION} -i ${INPUT} -i ${PALETTE} -lavfi "$FILTERS [x]; [x][1:v] paletteuse" -y ${OUTPUT}
echo "Done!"

0 comments on commit 6612542

Please sign in to comment.