Last active
January 22, 2025 19:14
-
-
Save jerturowetz/0ab5db870c7ab7c0f334fa7809664eb5 to your computer and use it in GitHub Desktop.
ffmpeg commands for optimized video output
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 | |
# | |
# Example usage: | |
# bash ffmpeg_the_works.sh PATH/TO/FILE.mp4 | |
if ! command -v ffmpeg > /dev/null; | |
then | |
echo "Missing ffmpeg!"; | |
exit 1; | |
fi | |
# --------------------------------------------------------- # | |
# ffmpeg_gif () # | |
# Converts a video with ffmpeg to a 15fps optimized gif # | |
# Parameters: $source_file $target_file # | |
# Returns: target_file # | |
# --------------------------------------------------------- # | |
function ffmpeg_gif() { | |
palette="/tmp/palette.png" | |
# filters="fps=15,scale=320:-1:flags=lanczos" | |
filters="fps=15" | |
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 | |
} | |
# ---------------------------------------------------------# | |
# ffmpeg_the_works () # | |
# Converts a video to mp4, webm & gif with ffmpeg # | |
# Parameters: $source_file # | |
# Returns: $file-comp.mp4, $file-comp.webm, $file-comp.gif # | |
# ---------------------------------------------------------# | |
function ffmpeg_the_works() { | |
FILENAME="${1}" | |
FILENAME_NO_EXT="${FILENAME%.*}" | |
ffmpeg -v warning -i "$FILENAME" -c:v libx264 -profile:v main -vf format=yuv420p -c:a aac -movflags +faststart "$2.mp4" | |
printf "\nRunning ffmpeg on %s to optimize mp4\n" "$FILENAME" | |
ffmpeg -v warning -i "$FILENAME" -b:v 0 -crf 25 "$FILENAME_NO_EXT-comp.mp4" | |
printf "\nRunning ffmpeg on %s to optimize webm\n" "$FILENAME" | |
ffmpeg -v warning -i "$FILENAME" -b:v 0 -crf 41 "$FILENAME_NO_EXT-comp.webm" | |
printf "\nRunning ffmpeg_gif\n" | |
ffmpeg_gif "$FILENAME" "$FILENAME_NO_EXT-comp.gif" | |
printf "\n######## EXAMPLE VIDEO CODE: #########" | |
printf "\n" | |
printf "\n<video autoplay loop muted preload playsinline poster=\"SOME_FALLBACK_IMAGE.png\">" | |
printf "\n\t<source src=\"%s-comp.webm\" type=\"video/webm\" />" "$FILENAME_NO_EXT" | |
printf "\n\t<source src=\"%s-comp.mp4\" type=\"video/mp4\" />" "$FILENAME_NO_EXT" | |
printf "\n\t<!--[if lt IE 9]><img src=\"%s-comp.gif\" /><![endif]-->" "$FILENAME_NO_EXT" | |
printf "\n</video>" | |
printf "\n" | |
printf "\n######################################" | |
printf "\n" | |
} | |
# run it | |
ffmpeg_the_works "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment