···5656## sleepy
5757Hibernates your computer. I usually run this after `render` or `chill`.
58585959-## to
6060-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`.
6161-6262-Converts audio and video. Running `to` shows this help message:
6363-```
6464-Usage:
6565- to <format> [options] <file1> [file2...]
6666-6767-Options:
6868- format Format to convert to.
6969- --formats List all supported formats.
7070- -v, --verbose If you love logs.
7171- -h, --help Show this help message.
7272-7373- Video/audio formats support FFmpeg options (see $ ffmpeg -h).
7474- Image formats support ImageMagick options (see $ man magick).
7575- Long arguments must have quotes (like "-b:a 128k").
7676-```
7777-7859## to-gif
7960Requires [ffmpeg](https://ffmpeg.org) (which I recommend installing using [Brew](https://brew.sh)).
8061
-182
bin/to
···11-#! /bin/bash
22-33-color() {
44- echo -n "`tput setaf $1`${@:2}"
55- # reset color if there are arguments:
66- if [[ "$#" > 1 ]]; then echo -n "`tput setaf 7`"; fi
77-}
88-red() { color 1 "$@"; }
99-green() { color 2 "$@"; }
1010-cyan() { color 6 "$@"; }
1111-reset() { color 7 "$@"; }
1212-1313-help() {
1414- echo "
1515- `green Usage:`
1616- `cyan $FILENAME` <format> [options] <file1> [file2...]
1717-1818- `green Options:`
1919- `cyan format` Format to convert to.
2020- `cyan --formats` List all supported formats.
2121- `cyan -v, --verbose` If you love logs.
2222- `cyan -h, --help` Show this help message.
2323-2424- Video/audio formats support FFmpeg options (see $ ffmpeg -h).
2525- Image formats support ImageMagick options (see $ man magick).
2626- Long arguments must have quotes (like \"-b:a 128k\").
2727- "
2828-}
2929-3030-list_formats() {
3131- echo "
3232- `green Video formats:`
3333- `cyan mp4` x
3434- `cyan mov` x
3535- `cyan webm` x
3636-3737- `green Audio formats:`
3838- `cyan mp3` Defaults to 320kbps
3939- `cyan wav` x
4040- `cyan flac` x
4141- `cyan aac` x
4242- `cyan aiff` x
4343- `cyan ogg` x
4444-4545- `green Image formats:`
4646- `cyan jpg` x
4747- `cyan png` x
4848- `cyan webp` x
4949- "
5050-}
5151-5252-FILENAME=${0##*/}
5353-ARGS=("$@")
5454-FORMAT="$1"
5555-CMD=ffmpeg
5656-# OPTIONS=()
5757-# NO_FORMAT=false
5858-# INVALID_FORMAT=false
5959-# HELP=false
6060-# LIST_FORMATS=false
6161-# VERBOSE=false
6262-# CUSTOM_OPTIONS=false
6363-# FILES=()
6464-# SKIPFILES=()
6565-6666-# no args
6767-if [[ "$#" < 1 ]]; then help; exit 0; fi
6868-6969-# format
7070-case "$FORMAT" in
7171- mp4|mov|webm)
7272- ;;
7373- mp3)
7474- OPTIONS+=("-b:a")
7575- OPTIONS+=("320k")
7676- ;;
7777- wav|flac|aac|aiff|ogg)
7878- ;;
7979- jpg|png|webp)
8080- CMD=magick
8181- ;;
8282- '')
8383- NO_FORMAT=true
8484- ;;
8585- *)
8686- INVALID_FORMAT=true
8787- ;;
8888-esac
8989-9090-# parse args
9191-for (( i=1; i <= "$#"; i++ )); do
9292- ARG="${!i}"
9393- case "${!i}" in
9494- -h|--help)
9595- HELP=true
9696- ;;
9797- --formats)
9898- LIST_FORMATS=true
9999- ;;
100100- -v|--verbose)
101101- VERBOSE=true
102102- ;;
103103- -*)
104104- CUSTOM_OPTIONS=true
105105- OPTIONS+=("$1")
106106- ;;
107107- *)
108108- # dont save file if arg is first (aka format)
109109- if [[ $i == 1 ]]; then
110110- :
111111- elif [[ -f "$ARG" ]]; then # if file exists
112112- FILES+=("$ARG")
113113- else
114114- SKIPFILES+=("$ARG")
115115- fi
116116- ;;
117117- esac
118118-done
119119-120120-if [[ $HELP == true ]]; then
121121- help; exit 0
122122-elif [[ $LIST_FORMATS == true ]]; then
123123- list_formats; exit 0
124124-elif [[ $NO_FORMAT == true ]]; then
125125- echo "`red Error:` No format provided"; exit 1
126126-elif [[ $INVALID_FORMAT == true ]]; then
127127- echo "`red Error:` Invalid format: $FORMAT"; exit 1
128128-elif [[ ${#SKIPFILES[@]} > 0 ]]; then
129129- for SKIPFILE in "${SKIPFILES[@]}"; do
130130- echo "`red Error:` Invalid argument or non-existant file: $SKIPFILE"
131131- done
132132- exit 1
133133-elif [[ ${#FILES[@]} == 0 ]]; then
134134- echo "`red Error:` No files specified"; exit 1
135135-fi
136136-137137-# verbose
138138-if [[ $CMD == "ffmpeg" && $VERBOSE != true ]]; then
139139- OPTIONS+=("-loglevel")
140140- OPTIONS+=("warning")
141141-elif [[ $CMD == "magick" && $VERBOSE == true ]]; then
142142- OPTIONS+=("-verbose")
143143-fi
144144-145145-# custom options
146146-if [[ $CUSTOM_OPTIONS == true || $VERBOSE == true ]]; then
147147- echo "Passing the following options to $CMD: `cyan "${OPTIONS[@]}"`"
148148-fi
149149-150150-for FILE in "${FILES[@]}"; do
151151-152152- # if file does not exist
153153- if [[ ! -f "$FILE" ]]; then
154154- echo "`red Error:` File no longer exists (Skipping): `cyan"$FILE"`"
155155- continue
156156- fi
157157-158158- echo "Converting `cyan $FILE`"
159159-160160- FILE_WITHOUT_EXT=${FILE%.*}
161161- NEW_FILE_TEMP="$FILE_WITHOUT_EXT CONVERTING... $((10000 + RANDOM)).$FORMAT"
162162-163163- if [[ $CMD == "ffmpeg" ]]; then
164164- ffmpeg -i "$FILE" ${OPTIONS[@]} "$NEW_FILE_TEMP" || { echo `red Error:` Convertion failed; exit 1; }
165165- else
166166- magick ${OPTIONS[@]} "$FILE" "$NEW_FILE_TEMP" || { echo `red Error:` Convertion failed; exit 1; }
167167- fi
168168-169169- COUNT=1
170170- NEW_FILE="$FILE_WITHOUT_EXT converted.$FORMAT"
171171- while [[ -f "$NEW_FILE" ]]; do
172172- if [[ -f "$NEW_FILE" ]]; then
173173- NEW_FILE="$FILE_WITHOUT_EXT converted ($COUNT).$FORMAT"
174174- (( COUNT++ ))
175175- else
176176- break
177177- fi
178178- done
179179-180180- mv "$NEW_FILE_TEMP" "$NEW_FILE"
181181-182182-done