Skip to content

Instantly share code, notes, and snippets.

@myersjustinc
Last active April 17, 2023 01:27
Show Gist options
  • Save myersjustinc/1e7d9a194f1e661044fd9695ebccf280 to your computer and use it in GitHub Desktop.
Save myersjustinc/1e7d9a194f1e661044fd9695ebccf280 to your computer and use it in GitHub Desktop.

Revisions

  1. myersjustinc revised this gist Apr 17, 2023. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions ffmpeg_helpers.bash
    Original file line number Diff line number Diff line change
    @@ -13,13 +13,13 @@ vid_get_scale() {
    -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
    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 \
    @@ -30,6 +30,7 @@ vid_get_scale() {
    "${long_dim}"
    fi
    }

    vid_resize() {
    raw_path="$1"
    long_dim="$2"
    @@ -47,6 +48,7 @@ vid_resize() {
    "${output_path}"
    fi
    }

    vid_make_gif() {
    raw_path="$1"
    long_dim="$2"
  2. myersjustinc created this gist Apr 17, 2023.
    74 changes: 74 additions & 0 deletions ffmpeg_helpers.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    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
    }