Skip to content

Instantly share code, notes, and snippets.

@gphg
Forked from Mrfiregem/meme-text.sh
Created January 28, 2025 16:04
Show Gist options
  • Select an option

  • Save gphg/e1b3e71eff734b5c32dde61d7dbe0cb1 to your computer and use it in GitHub Desktop.

Select an option

Save gphg/e1b3e71eff734b5c32dde61d7dbe0cb1 to your computer and use it in GitHub Desktop.

Revisions

  1. Firegem revised this gist Oct 4, 2023. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions meme-text.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    : "${FFILE:="${HOME}/.fonts/i/impact.ttf"}" # Let user choose font file
    : "${FSIZE:=72}" # Let user choose font size in px
    : "${BSIZE:=5}" # Let user choose stroke size in px
    : "${OFFSET:='(h*0.05)'}" # Let user choose text offset in px
    # TTEXT - Text to display at the top of the image
    # BTEXT - Text to display at the bottom of the image

    @@ -12,16 +13,17 @@

    # Show how to use the program
    usage() {
    printf 'usage: [TTEXT=S] [BTEXT=S] [FFILE=S] [FSIZE=N] [BSIZE=N] %s IN_FILE OUT_FILE\n' "${0##*/}"
    printf 'usage: [TTEXT=S BTEXT=S FFILE=S FSIZE=N BSIZE=N OFFSET=N] %s IN_FILE OUT_FILE\n' "${0##*/}"
    }
    usage_long() {
    usage
    cat <<EOF
    FFILE Font to use (default: ${HOME}/.fonts/i/impact.ttf)
    FSIZE Font size in px (default: 72)
    BSIZE Stroke size in px (default: 5)
    FFILE Font to use (default: ${HOME}/.fonts/i/impact.ttf)
    FSIZE Font size in px (default: 72)
    BSIZE Stroke size in px (default: 5)
    TTEXT Top text (optional)
    BTEXT Bottom text (optional)
    OFFSET Text offset from edges (default: h*0.05)
    Due to the way shell strings work, using an apostrophe may require escaping
    multiple times, e.g.: TTEXT="I\\\\\\\\\'m okay" to display "I'm okay"
    EOF
    @@ -55,13 +57,13 @@ fi

    # run if only top text provided
    if [[ -n $TTEXT ]] && [[ -z $BTEXT ]]; then
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=20" -an "$2"
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=${OFFSET}" -an "$2"
    fi

    # run if only bottom text provided
    if [[ -z $TTEXT ]] && [[ -n $BTEXT ]]; then
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-20" -an "$2"
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-${OFFSET}" -an "$2"
    fi

    # run if both top and bottom text are provided
    ffmpeg -i "$1" -vf "[in] drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=20, drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-20 [out]" -an "$2"
    ffmpeg -i "$1" -vf "[in] drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=${OFFSET}, drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-${OFFSET} [out]" -an "$2"
  2. Firegem revised this gist Oct 3, 2023. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions meme-text.sh
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@
    # TTEXT - Text to display at the top of the image
    # BTEXT - Text to display at the bottom of the image

    # Exit value index:
    # 1 - Problem with command arguments
    # 2 - Problem with output customization

    # Show how to use the program
    usage() {
    printf 'usage: [TTEXT=S] [BTEXT=S] [FFILE=S] [FSIZE=N] [BSIZE=N] %s IN_FILE OUT_FILE\n' "${0##*/}"
    @@ -35,6 +39,13 @@ if [[ $# -ne 2 ]]; then
    fi
    fi

    # make sure input file exists
    if ! [[ -f $1 ]]; then
    printf "error: %s is not a file.\n" "$1" >&2
    usage >&2
    exit 1
    fi

    # exit if no text would be overlain
    if [[ -z $TTEXT ]] && [[ -z $BTEXT ]]; then
    echo 'error: both text fields are empty' >&2
  3. Firegem revised this gist Oct 3, 2023. No changes.
  4. Firegem created this gist Oct 3, 2023.
    56 changes: 56 additions & 0 deletions meme-text.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    #!/usr/bin/env bash
    # Initialize variables to customize output
    : "${FFILE:="${HOME}/.fonts/i/impact.ttf"}" # Let user choose font file
    : "${FSIZE:=72}" # Let user choose font size in px
    : "${BSIZE:=5}" # Let user choose stroke size in px
    # TTEXT - Text to display at the top of the image
    # BTEXT - Text to display at the bottom of the image

    # Show how to use the program
    usage() {
    printf 'usage: [TTEXT=S] [BTEXT=S] [FFILE=S] [FSIZE=N] [BSIZE=N] %s IN_FILE OUT_FILE\n' "${0##*/}"
    }
    usage_long() {
    usage
    cat <<EOF
    FFILE Font to use (default: ${HOME}/.fonts/i/impact.ttf)
    FSIZE Font size in px (default: 72)
    BSIZE Stroke size in px (default: 5)
    TTEXT Top text (optional)
    BTEXT Bottom text (optional)
    Due to the way shell strings work, using an apostrophe may require escaping
    multiple times, e.g.: TTEXT="I\\\\\\\\\'m okay" to display "I'm okay"
    EOF
    }

    # Make sure all fields are full
    if [[ $# -ne 2 ]]; then
    if [[ $# -eq 0 ]]; then
    usage_long
    exit 0
    else
    printf 'error: wrong number of arguments: got %d, expected 2.\n' "$#" >&2
    usage >&2
    exit 1
    fi
    fi

    # exit if no text would be overlain
    if [[ -z $TTEXT ]] && [[ -z $BTEXT ]]; then
    echo 'error: both text fields are empty' >&2
    usage >&2
    exit 2
    fi

    # run if only top text provided
    if [[ -n $TTEXT ]] && [[ -z $BTEXT ]]; then
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=20" -an "$2"
    fi

    # run if only bottom text provided
    if [[ -z $TTEXT ]] && [[ -n $BTEXT ]]; then
    exec ffmpeg -i "$1" -vf "drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-20" -an "$2"
    fi

    # run if both top and bottom text are provided
    ffmpeg -i "$1" -vf "[in] drawtext=fontfile=${FFILE}:text=${TTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=20, drawtext=fontfile=${FFILE}:text=${BTEXT}:fontcolor=white:borderw=${BSIZE}:fontsize=${FSIZE}:x=(w-text_w)/2:y=(h-text_h)-20 [out]" -an "$2"