Skip to content

Instantly share code, notes, and snippets.

@devadvance
Created February 28, 2021 20:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devadvance/03d3c8f57b3e0254fb989e9464587556 to your computer and use it in GitHub Desktop.
Save devadvance/03d3c8f57b3e0254fb989e9464587556 to your computer and use it in GitHub Desktop.
Convenient ffmpeg shell functions
#######################################
# Displays a yes/no prompt to continue that repeats until input matches.
# Arguments:
# $1 - (optional) a string representing the yes/no question to ask
# $2 - (optional) a string representing the prompt style.
# Returns:
# 0 to proceed further (indicates "yes"), 1 to stop (indicates "no").
#######################################
continue_prompt() {
local prompt default reply
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
while true; do
# Display the prompt.
echo -n "$1 [$prompt] "
# Read the answer from tty (avoids stdin issues).
read reply </dev/tty
# Default case for empty user reply.
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid.
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
#######################################
# Create a center crop MP4 of a video with the specified width using ffmpeg.
# Note: assumes a three character file extension and maintains input height.
# Globals:
# ffmpeg available on the path.
# Arguments:
# $1 - (required) path to a video file from which to create a cropped video.
# $2 - (required) integer value in pixels for the width of the cropped video.
# Outputs:
# Writes new video to the same directory as the input file.
#######################################
ff_center_crop() {
outputname="${1%????} (Center Crop).mp4"
echo "Input file: ${1}"
echo "Output file: ${outputname}"
echo "Crop width (pixels): ${2}"
if continue_prompt "Do you want to proceed?"; then
echo "Running..."
ffmpeg -i "$1" -filter:v "crop=$2:in_h" -c:a copy "$outputname"
else
echo "Exiting..."
fi
}
#######################################
# Create a CounterClockwise 90degree rotated MP4 of a video using ffmpeg.
# Note: assumes a three character file extension.
# Globals:
# ffmpeg available on the path.
# Arguments:
# $1 - (required) path to a video file from which to create a rotated video.
# Outputs:
# Writes new video to the same directory as the input file.
#######################################
ff_rotate_video_cc90() {
outputname="${1%????} (Rotated).mp4"
echo "Input file: ${1}"
echo "Output file: ${outputname}"
if continue_prompt "Do you want to proceed?"; then
echo "Running..."
ffmpeg -i "$1" -vf "transpose=2" -vcodec libx264 -crf 22 -pix_fmt yuv420p "$outputname"
else
echo "Exiting..."
fi
}
#######################################
# Create a 10fps GIF from a video using ffmpeg.
# Note: assumes a three character file extension.
# Globals:
# ffmpeg available on the path.
# Arguments:
# $1 - (required) path to a video file from which to create a GIF.
# $2 - (required) integer value in pixels for the width of the output GIF.
# Outputs:
# Writes a GIF to the same directory as the input file.
#######################################
ff_gif() {
outputname="${1%????}_${2}.gif"
echo "Input file: ${1}"
echo "Output file: ${outputname}"
echo "Output width (pixels): ${2}"
if continue_prompt "Do you want to proceed?"; then
echo "Running..."
ffmpeg -i "$1" -vf "fps=10,scale=$2:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 $outputname
else
echo "Exiting..."
fi
}
#######################################
# Create a 15fps animated WebP from a video using ffmpeg.
# Note: assumes a three character file extension.
# Globals:
# ffmpeg available on the path.
# Arguments:
# $1 - (required) path to a video file from which to create a GIF.
# $2 - (required) integer value in pixels for the width of the output GIF.
# Outputs:
# Writes a GIF to the same directory as the input file.
#######################################
ff_webp() {
outputname="${1%????}_${2}.webp"
echo "Input file: ${1}"
echo "Output file: ${outputname}"
echo "Output width (pixels): ${2}"
if continue_prompt "Do you want to proceed?"; then
echo "Running..."
ffmpeg -i "$1" -vf "fps=15,scale=$2:-1:flags=lanczos" -vcodec libwebp -lossless 0 -compression_level 6 -q:v 50 -loop 0 -preset picture -an -vsync 0 $outputname
else
echo "Exiting..."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment