Skip to content

Instantly share code, notes, and snippets.

@goncalossilva
Last active December 12, 2023 22:31
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save goncalossilva/865e90b66e4bddc58f40 to your computer and use it in GitHub Desktop.
Save goncalossilva/865e90b66e4bddc58f40 to your computer and use it in GitHub Desktop.
Encode mp4, webm and gif using ffmpeg (2.6 or above). Audio is discarded on all of these!
#!/bin/sh
# sh gifenc.sh input.mp4 output.gif
# Optionally accepts width / height (one or both).
palette="/tmp/palette.png"
filters="fps=15"
if [ -n "$3" ]; then
if [ -n "$4" ]; then
filters="$filters,scale=$3:$4"
else
filters="$filters,scale=$3:-1"
fi
filters="$filters:flags=lanczos"
fi
ffmpeg -v warning -i "$1" -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i "$1" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$2"
#!/bin/sh
# sh mp4enc.sh input.mp4 output.mp4 [quality]
# Optionally accepts a "quality" parameter (defaults to 640).
# https://www.virag.si/2012/01/web-video-encoding-tutorial-with-ffmpeg-0-9
quality="640k"
if [ -n "$3" ]; then
quality="$3k"
fi
options="-c:v libx264 -b:v $quality -maxrate $quality -bufsize 1000k -profile:v high -preset veryslow -tune film -movflags +faststart -threads 1"
ffmpeg -y -i "$1" $options -pass 1 -an -f rawvideo /dev/null && ffmpeg -i "$1" $options -pass 2 -an "$2"
#!/bin/sh
# sh wemenc.sh input.mp4 output.webm [quality]
# Optionally accepts a "quality" parameter (defaults to 700).
# https://www.virag.si/2012/01/webm-web-video-encoding-tutorial-with-ffmpeg-0-9
quality="700k"
if [ -n "$3" ]; then
quality="$3k"
fi
options="-c:v libvpx -b:v $quality -maxrate $quality -bufsize 1000k -preset veryslow -tune film -movflags +faststart -threads 1"
ffmpeg -y -i "$1" $options -pass 1 -an -f rawvideo /dev/null && ffmpeg -i "$1" $options -pass 2 -an "$2"
@Zettt
Copy link

Zettt commented Jan 16, 2016

May I add an improvement to gifenc.sh? It's better to encase $1 and $2 in double quotes, this allows for filenames to have spaces in their names.

Last line of your script would be:

ffmpeg -v warning -i "$1" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$2"

@Zettt
Copy link

Zettt commented Jan 29, 2016

Love your script. Been working on this today. This now accepts parameters from the command line and sets up defaults in case they are not present. Essentially only the input file is needed as parameter, everything else is automated. https://gist.github.com/Zettt/30b0b3b52f33a2cbcb55

Cheers!

@goncalossilva
Copy link
Author

@Zettt Apologies for the multi-year delay here, I missed your comments before! I've applied your suggestion, we certainly don't want word splitting.

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