Skip to content

Instantly share code, notes, and snippets.

@dbonates
Created November 13, 2019 05:40
Show Gist options
  • Select an option

  • Save dbonates/c9afee953088e6e0e1490b185951de8b to your computer and use it in GitHub Desktop.

Select an option

Save dbonates/c9afee953088e6e0e1490b185951de8b to your computer and use it in GitHub Desktop.
Some handy ffmpeg functions I use everyday.
#
# VIDEO / IMAGE RELATED
#
# requires: ffmpeg
# tip: put this funcion on your ~/.profile file
#
# Compress video files while keep their quality
# Usage: compress video_file
compress() {
sourcefile=$(basename "$1")
filename="${sourcefile%.*}"
ffmpeg -i ${sourcefile} -c:v libx264 -crf 24 -b:v 1M -c:a aac -strict -2 "${filename}"_compressed.mp4
}
# Convert video to gif file.
# Usage: video2gif video_file (scale) (fps)
video2gif() {
sourcefile=$(basename "$1")
filename="${sourcefile%.*}"
ffmpeg -y -i "${1}" -vf fps=${3:-10},scale=${2:-320}:-1:flags=lanczos,palettegen "${filename}.png"
ffmpeg -i "${1}" -i "${filename}.png" -filter_complex "fps=${3:-10},scale=${2:-320}:-1:flags=lanczos[x];[x][1:v]paletteuse" "${filename}".gif
rm "${filename}.png"
}
# Rescale video to file.
# Usage: rescale video_file desired_width
# example, will rescale a video to width = 640 keeping aspect ratio:
# rescale video_file 640
rescale() {
sourcefile=$(basename "$1")
filename="${sourcefile%.*}"
ffmpeg -i ${sourcefile} -vf scale=${2:-320}:-1 "${filename}_scaled.mp4"
}
# Speed up video to file.
# Usage: speedup video_file
# tip: could be used to optimize file size too
speedup() {
sourcefile=$(basename "$1")
filename="${sourcefile%.*}"
ffmpeg -i ${sourcefile} -r 16 -filter:v "setpts=0.5*PTS" "${filename}"_faster.mp4
}
# Speed down video to file.
# Usage: speedup video_file
# tip: could be used to optimize file size too
speeddn() {
sourcefile=$(basename "$1")
filename="${sourcefile%.*}"
ffmpeg -i ${sourcefile} -r 16 -filter:v "setpts=2*PTS" "${filename}"_slower.mp4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment