[READ-ONLY] Mirror of https://github.com/probablykasper/my-bash-scripts. My personal bash scripts
cli
0

Configure Feed

Select the types of activity you want to include in your feed.

Moved "to" into seperate repo

Kasper (Apr 19, 2019, 8:13 PM +0200) 247b8601 5a038f9a

-201
-19
README.md
··· 56 56 ## sleepy 57 57 Hibernates your computer. I usually run this after `render` or `chill`. 58 58 59 - ## to 60 - Requires [ImageMagick](https://imagemagick.org) for images and [FFmpeg](https://ffmpeg.org) for audio and video. I recommend installing them using [Brew](https://brew.sh) by running `brew install ffmpeg imagemagick`. 61 - 62 - Converts audio and video. Running `to` shows this help message: 63 - ``` 64 - Usage: 65 - to <format> [options] <file1> [file2...] 66 - 67 - Options: 68 - format Format to convert to. 69 - --formats List all supported formats. 70 - -v, --verbose If you love logs. 71 - -h, --help Show this help message. 72 - 73 - Video/audio formats support FFmpeg options (see $ ffmpeg -h). 74 - Image formats support ImageMagick options (see $ man magick). 75 - Long arguments must have quotes (like "-b:a 128k"). 76 - ``` 77 - 78 59 ## to-gif 79 60 Requires [ffmpeg](https://ffmpeg.org) (which I recommend installing using [Brew](https://brew.sh)). 80 61
-182
bin/to
··· 1 - #! /bin/bash 2 - 3 - color() { 4 - echo -n "`tput setaf $1`${@:2}" 5 - # reset color if there are arguments: 6 - if [[ "$#" > 1 ]]; then echo -n "`tput setaf 7`"; fi 7 - } 8 - red() { color 1 "$@"; } 9 - green() { color 2 "$@"; } 10 - cyan() { color 6 "$@"; } 11 - reset() { color 7 "$@"; } 12 - 13 - help() { 14 - echo " 15 - `green Usage:` 16 - `cyan $FILENAME` <format> [options] <file1> [file2...] 17 - 18 - `green Options:` 19 - `cyan format` Format to convert to. 20 - `cyan --formats` List all supported formats. 21 - `cyan -v, --verbose` If you love logs. 22 - `cyan -h, --help` Show this help message. 23 - 24 - Video/audio formats support FFmpeg options (see $ ffmpeg -h). 25 - Image formats support ImageMagick options (see $ man magick). 26 - Long arguments must have quotes (like \"-b:a 128k\"). 27 - " 28 - } 29 - 30 - list_formats() { 31 - echo " 32 - `green Video formats:` 33 - `cyan mp4` x 34 - `cyan mov` x 35 - `cyan webm` x 36 - 37 - `green Audio formats:` 38 - `cyan mp3` Defaults to 320kbps 39 - `cyan wav` x 40 - `cyan flac` x 41 - `cyan aac` x 42 - `cyan aiff` x 43 - `cyan ogg` x 44 - 45 - `green Image formats:` 46 - `cyan jpg` x 47 - `cyan png` x 48 - `cyan webp` x 49 - " 50 - } 51 - 52 - FILENAME=${0##*/} 53 - ARGS=("$@") 54 - FORMAT="$1" 55 - CMD=ffmpeg 56 - # OPTIONS=() 57 - # NO_FORMAT=false 58 - # INVALID_FORMAT=false 59 - # HELP=false 60 - # LIST_FORMATS=false 61 - # VERBOSE=false 62 - # CUSTOM_OPTIONS=false 63 - # FILES=() 64 - # SKIPFILES=() 65 - 66 - # no args 67 - if [[ "$#" < 1 ]]; then help; exit 0; fi 68 - 69 - # format 70 - case "$FORMAT" in 71 - mp4|mov|webm) 72 - ;; 73 - mp3) 74 - OPTIONS+=("-b:a") 75 - OPTIONS+=("320k") 76 - ;; 77 - wav|flac|aac|aiff|ogg) 78 - ;; 79 - jpg|png|webp) 80 - CMD=magick 81 - ;; 82 - '') 83 - NO_FORMAT=true 84 - ;; 85 - *) 86 - INVALID_FORMAT=true 87 - ;; 88 - esac 89 - 90 - # parse args 91 - for (( i=1; i <= "$#"; i++ )); do 92 - ARG="${!i}" 93 - case "${!i}" in 94 - -h|--help) 95 - HELP=true 96 - ;; 97 - --formats) 98 - LIST_FORMATS=true 99 - ;; 100 - -v|--verbose) 101 - VERBOSE=true 102 - ;; 103 - -*) 104 - CUSTOM_OPTIONS=true 105 - OPTIONS+=("$1") 106 - ;; 107 - *) 108 - # dont save file if arg is first (aka format) 109 - if [[ $i == 1 ]]; then 110 - : 111 - elif [[ -f "$ARG" ]]; then # if file exists 112 - FILES+=("$ARG") 113 - else 114 - SKIPFILES+=("$ARG") 115 - fi 116 - ;; 117 - esac 118 - done 119 - 120 - if [[ $HELP == true ]]; then 121 - help; exit 0 122 - elif [[ $LIST_FORMATS == true ]]; then 123 - list_formats; exit 0 124 - elif [[ $NO_FORMAT == true ]]; then 125 - echo "`red Error:` No format provided"; exit 1 126 - elif [[ $INVALID_FORMAT == true ]]; then 127 - echo "`red Error:` Invalid format: $FORMAT"; exit 1 128 - elif [[ ${#SKIPFILES[@]} > 0 ]]; then 129 - for SKIPFILE in "${SKIPFILES[@]}"; do 130 - echo "`red Error:` Invalid argument or non-existant file: $SKIPFILE" 131 - done 132 - exit 1 133 - elif [[ ${#FILES[@]} == 0 ]]; then 134 - echo "`red Error:` No files specified"; exit 1 135 - fi 136 - 137 - # verbose 138 - if [[ $CMD == "ffmpeg" && $VERBOSE != true ]]; then 139 - OPTIONS+=("-loglevel") 140 - OPTIONS+=("warning") 141 - elif [[ $CMD == "magick" && $VERBOSE == true ]]; then 142 - OPTIONS+=("-verbose") 143 - fi 144 - 145 - # custom options 146 - if [[ $CUSTOM_OPTIONS == true || $VERBOSE == true ]]; then 147 - echo "Passing the following options to $CMD: `cyan "${OPTIONS[@]}"`" 148 - fi 149 - 150 - for FILE in "${FILES[@]}"; do 151 - 152 - # if file does not exist 153 - if [[ ! -f "$FILE" ]]; then 154 - echo "`red Error:` File no longer exists (Skipping): `cyan"$FILE"`" 155 - continue 156 - fi 157 - 158 - echo "Converting `cyan $FILE`" 159 - 160 - FILE_WITHOUT_EXT=${FILE%.*} 161 - NEW_FILE_TEMP="$FILE_WITHOUT_EXT CONVERTING... $((10000 + RANDOM)).$FORMAT" 162 - 163 - if [[ $CMD == "ffmpeg" ]]; then 164 - ffmpeg -i "$FILE" ${OPTIONS[@]} "$NEW_FILE_TEMP" || { echo `red Error:` Convertion failed; exit 1; } 165 - else 166 - magick ${OPTIONS[@]} "$FILE" "$NEW_FILE_TEMP" || { echo `red Error:` Convertion failed; exit 1; } 167 - fi 168 - 169 - COUNT=1 170 - NEW_FILE="$FILE_WITHOUT_EXT converted.$FORMAT" 171 - while [[ -f "$NEW_FILE" ]]; do 172 - if [[ -f "$NEW_FILE" ]]; then 173 - NEW_FILE="$FILE_WITHOUT_EXT converted ($COUNT).$FORMAT" 174 - (( COUNT++ )) 175 - else 176 - break 177 - fi 178 - done 179 - 180 - mv "$NEW_FILE_TEMP" "$NEW_FILE" 181 - 182 - done