[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.

Added image support for "to" using ImageMagick

Kasper (Apr 12, 2019, 4:07 AM +0200) cfda5919 5a073410

+61 -20
+1 -1
README.md
··· 55 55 Hibernates your computer. I usually run this after `render` or `chill`. 56 56 57 57 ### to 58 - Requires [ffmpeg](https://ffmpeg.org) (which I recommend installing using [Brew](https://brew.sh)). 58 + 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`. 59 59 60 60 Converts audio and video. Running `to` shows this help message: 61 61 ```
+60 -19
bin/to
··· 2 2 3 3 FILENAME=${0##*/} 4 4 5 + red() { echo "`tput setaf 1`"$@"`tput setaf 7`" ; } 6 + green() { echo "`tput setaf 2`"$@"`tput setaf 7`" ; } 7 + cyan() { echo "`tput setaf 6`"$@"`tput setaf 7`" ; } 8 + 5 9 help() { 6 10 echo " 7 - `tput setaf 2`Usage:`tput setaf 7` 8 - `tput setaf 6`$FILENAME`tput setaf 7` <format> [options] <file1> [file2...] 11 + `green Usage:` 12 + `cyan $FILENAME` <format> [options] <file1> [file2...] 9 13 10 - `tput setaf 2`Options:`tput setaf 7` 11 - `tput setaf 6`format`tput setaf 7` File format extension to convert to. 12 - `tput setaf 6` `tput setaf 7` - mp3s default to 320kbps 13 - `tput setaf 6`-v, --verbose`tput setaf 7` If you love logs. 14 - `tput setaf 6`-h, --help`tput setaf 7` Show this help message. 15 - `tput setaf 6``tput setaf 7` 16 - `tput setaf 6``tput setaf 7`FFmpeg options are supported too (see ffmpeg -h). Long 17 - `tput setaf 6``tput setaf 7`arguments need to be written with quotes (like \"-b:a 128k\") 14 + `green Options:` 15 + `cyan format` Format to convert to. 16 + `cyan --formats` List all available formats. 17 + `cyan -v, --verbose` If you love logs. 18 + `cyan -h, --help` Show this help message. 19 + 20 + FFmpeg options are supported for audio/video formats (see $ ffmpeg -h). 21 + ImageMagick options are supported for image formats (see $ man magick). 22 + Long arguments need to be written with quotes (like \"-b:a 128k\") 18 23 " 19 24 } 20 25 21 26 # if only 0 or 1 arguments 22 - if [[ "$#" < 2 ]]; then help; exit 0; fi 27 + if [[ "$#" < 1 ]]; then help; exit 0; fi 23 28 24 29 FORMAT=$1 25 30 # remove format argument 26 31 shift 1 32 + case "$FORMAT" in 33 + mp4|mov|webm) 34 + CMD=ffmpeg 35 + ;; 36 + mp3|wav|flac|aac|aiff|ogg) 37 + CMD=ffmpeg 38 + ;; 39 + jpg|png|webp) 40 + CMD=magick 41 + ;; 42 + *) 43 + echo "`red Error:` Invalid format: $FORMAT" 44 + exit 1 45 + ;; 46 + esac 47 + VIDEO_FORMATS=("mp4", "mov", "webm") 48 + AUDIO_FORMATS=("mp3", "wav", "flac", "aac", "aiff", "ogg") 49 + IMAGE_FORMATS=("jpg", "png", "webp") 50 + 51 + # if only 0 or 1 arguments 52 + if [[ "$#" < 1 ]]; then help; exit 0; fi 53 + 27 54 28 55 FILES=() 29 56 VERBOSE_FLAG=false 30 57 USER_OPTIONS=false 31 58 OPTIONS=() 59 + SKIPFILES=() 32 60 33 61 if [[ "$FORMAT" = "mp3" ]]; then 34 62 OPTIONS+=("-b:a") ··· 39 67 while (( "$#" )); do 40 68 case "$1" in 41 69 -h|--help) 42 - if [[ "$HELP_FLAG" = true ]]; then help; exit 0; fi 70 + help; exit 0 71 + shift ;; 72 + --formats) 73 + list_formats; exit 0 43 74 shift ;; 44 75 -v|--verbose) 45 76 VERBOSE_FLAG=true ··· 51 82 USER_OPTIONS=true 52 83 OPTIONS+=("$1") 53 84 else 54 - echo "Error: File does not exist (Skipping) or invalid argument: $1" 85 + SKIPFILES+=("$1") 55 86 fi 56 87 shift ;; 57 88 esac 58 89 done 59 90 60 - if [[ "$VERBOSE_FLAG" = false ]]; then 91 + if [[ $CMD == "ffmpeg" && "$VERBOSE_FLAG" = false ]]; then 61 92 OPTIONS+=("-loglevel") 62 93 OPTIONS+=("warning") 94 + elif [[ $CMD == "magick" && "$VERBOSE_FLAG" = true ]]; then 95 + OPTIONS+=("-verbose") 63 96 fi 64 97 65 98 if [[ ${#FILES[@]} = 0 ]]; then 66 - echo "Error: No files specified" 99 + echo "`red Error:` No files specified" 67 100 help 68 101 exit 1 102 + elif [[ ! ${#SKIPFILES[@]} = 0 ]]; then 103 + for SKIPFILE in "${SKIPFILES[@]}"; do 104 + echo "`red Error:` File does not exist (Skipping) or invalid argument: $SKIPFILE" 105 + done 69 106 fi 70 107 71 108 if [[ "$USER_OPTIONS" = true ]]; then 72 - echo "Passing the following options to ffmpeg: ${OPTIONS[@]}" 109 + echo "Passing the following options to $CMD: ${OPTIONS[@]}" 73 110 fi 74 111 75 112 for FILE in "${FILES[@]}"; do 76 113 77 114 if [[ -f "$FILE" ]]; then # check if file exists 78 - echo "Converting $FILE" 115 + echo "Converting `cyan $FILE`" 79 116 80 117 FILENAME=$(basename "$FILE") 81 118 FILE_WITHOUT_EXT=${FILE%.*} # remove extension 82 119 # echo "FILENAME $FILENAME" 83 120 # echo "TEMPDIR $TEMPDIR" 84 121 NEW_FILE_WIP="$FILE_WITHOUT_EXT CONVERTING... $((10000 + RANDOM)).$FORMAT" 85 - ffmpeg -i "${FILE}" ${OPTIONS[@]} "$NEW_FILE_WIP" 122 + if [[ $CMD == "ffmpeg" ]]; then 123 + ffmpeg -i "${FILE}" ${OPTIONS[@]} "$NEW_FILE_WIP" || { echo `red Error:` Convertion failed; exit 1; } 124 + else 125 + magick ${OPTIONS[@]} "$FILE" "$NEW_FILE_WIP" || { echo `red Error:` Convertion failed; exit 1; } 126 + fi 86 127 87 128 NEW_FILE="$FILE_WITHOUT_EXT converted.$FORMAT" 88 129 COUNT=1 ··· 98 139 done 99 140 mv "$NEW_FILE_WIP" "$NEW_FILE" 100 141 else 101 - echo "Error: File no longer exists (Skipping): $FILE" 142 + echo "`red Error:` File no longer exists (Skipping): $FILE" 102 143 fi 103 144 104 145 done