Last active
April 17, 2023 01:27
-
-
Save myersjustinc/1e7d9a194f1e661044fd9695ebccf280 to your computer and use it in GitHub Desktop.
Handy ffmpeg helpers
This file contains 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
vid_get_scale() { | |
raw_path="$1" | |
long_dim="$2" | |
if [[ -z "${raw_path}" || -z "${long_dim}" ]]; then | |
echo "Usage: vid_get_scale RAW_PATH LONG_DIMENSION" | |
else | |
vid_meta="$( \ | |
ffprobe \ | |
-hide_banner \ | |
-v quiet \ | |
-print_format json \ | |
-select_streams v:0 \ | |
-show_streams \ | |
"${raw_path}" )" | |
read -r -d '' select_filter <<-'EOF' | |
if ( | |
[.streams[0].side_data_list[0].rotation] | | |
inside([-90, 90])) | |
then ("-2:" + $ARGS.positional[0]) | |
else ($ARGS.positional[0] + ":-2") | |
end | |
EOF | |
echo "${vid_meta}" | \ | |
jq \ | |
--raw-output \ | |
--monochrome-output \ | |
--join-output \ | |
--args \ | |
"${select_filter}" \ | |
"${long_dim}" | |
fi | |
} | |
vid_resize() { | |
raw_path="$1" | |
long_dim="$2" | |
if [[ -z "${raw_path}" || -z "${long_dim}" ]]; then | |
echo "Usage: vid_resize RAW_PATH LONG_DIMENSION" | |
else | |
scale_arg="$(vid_get_scale "${raw_path}" "${long_dim}")" | |
output_ext="$(echo "${raw_path}" | grep -Eo '[^.]+$')" | |
output_base="$(basename "${raw_path}" ".${output_ext}")" | |
output_path="${output_base}-smaller.${output_ext}" | |
ffmpeg \ | |
-y \ | |
-i "${raw_path}" \ | |
-vf "scale=${scale_arg}" \ | |
"${output_path}" | |
fi | |
} | |
vid_make_gif() { | |
raw_path="$1" | |
long_dim="$2" | |
frame_rate="$3" | |
pause_len="$4" | |
if [[ -z "${raw_path}" || -z "${long_dim}" || -z "${frame_rate}" ]]; then | |
echo "Usage: vid_make_gif RAW_PATH LONG_DIMENSION FRAME_RATE [PAUSE_LENGTH]" | |
else | |
scale_arg="$(vid_get_scale "${raw_path}" "${long_dim}")" | |
if [[ -z "${pause_len}" ]]; then | |
pause_filter='' | |
else | |
pause_filter="tpad=stop_mode=clone:stop_duration=${pause_len}," | |
fi | |
source_ext="$(echo "${raw_path}" | grep -Eo '[^.]+$')" | |
output_base="$(basename "${raw_path}" "${source_ext}")" | |
output_path="${output_base}gif" | |
ffmpeg \ | |
-y \ | |
-i "${raw_path}" \ | |
-filter_complex \ | |
"[0:v] fps=${frame_rate},scale=${scale_arg},${pause_filter}split [a][b];[a] palettegen [p];[b][p] paletteuse" \ | |
"${output_path}" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment