···5555Hibernates your computer. I usually run this after `render` or `chill`.
56565757### to
5858-Requires [ffmpeg](https://ffmpeg.org) (which I recommend installing using [Brew](https://brew.sh)).
5858+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`.
59596060Converts audio and video. Running `to` shows this help message:
6161```
+60-19
bin/to
···2233FILENAME=${0##*/}
4455+red() { echo "`tput setaf 1`"$@"`tput setaf 7`" ; }
66+green() { echo "`tput setaf 2`"$@"`tput setaf 7`" ; }
77+cyan() { echo "`tput setaf 6`"$@"`tput setaf 7`" ; }
88+59help() {
610echo "
77-`tput setaf 2`Usage:`tput setaf 7`
88- `tput setaf 6`$FILENAME`tput setaf 7` <format> [options] <file1> [file2...]
1111+`green Usage:`
1212+ `cyan $FILENAME` <format> [options] <file1> [file2...]
9131010-`tput setaf 2`Options:`tput setaf 7`
1111- `tput setaf 6`format`tput setaf 7` File format extension to convert to.
1212- `tput setaf 6` `tput setaf 7` - mp3s default to 320kbps
1313- `tput setaf 6`-v, --verbose`tput setaf 7` If you love logs.
1414- `tput setaf 6`-h, --help`tput setaf 7` Show this help message.
1515- `tput setaf 6``tput setaf 7`
1616- `tput setaf 6``tput setaf 7`FFmpeg options are supported too (see ffmpeg -h). Long
1717- `tput setaf 6``tput setaf 7`arguments need to be written with quotes (like \"-b:a 128k\")
1414+`green Options:`
1515+ `cyan format` Format to convert to.
1616+ `cyan --formats` List all available formats.
1717+ `cyan -v, --verbose` If you love logs.
1818+ `cyan -h, --help` Show this help message.
1919+2020+ FFmpeg options are supported for audio/video formats (see $ ffmpeg -h).
2121+ ImageMagick options are supported for image formats (see $ man magick).
2222+ Long arguments need to be written with quotes (like \"-b:a 128k\")
1823"
1924}
20252126# if only 0 or 1 arguments
2222-if [[ "$#" < 2 ]]; then help; exit 0; fi
2727+if [[ "$#" < 1 ]]; then help; exit 0; fi
23282429FORMAT=$1
2530# remove format argument
2631shift 1
3232+case "$FORMAT" in
3333+ mp4|mov|webm)
3434+ CMD=ffmpeg
3535+ ;;
3636+ mp3|wav|flac|aac|aiff|ogg)
3737+ CMD=ffmpeg
3838+ ;;
3939+ jpg|png|webp)
4040+ CMD=magick
4141+ ;;
4242+ *)
4343+ echo "`red Error:` Invalid format: $FORMAT"
4444+ exit 1
4545+ ;;
4646+esac
4747+VIDEO_FORMATS=("mp4", "mov", "webm")
4848+AUDIO_FORMATS=("mp3", "wav", "flac", "aac", "aiff", "ogg")
4949+IMAGE_FORMATS=("jpg", "png", "webp")
5050+5151+# if only 0 or 1 arguments
5252+if [[ "$#" < 1 ]]; then help; exit 0; fi
5353+27542855FILES=()
2956VERBOSE_FLAG=false
3057USER_OPTIONS=false
3158OPTIONS=()
5959+SKIPFILES=()
32603361if [[ "$FORMAT" = "mp3" ]]; then
3462 OPTIONS+=("-b:a")
···3967while (( "$#" )); do
4068 case "$1" in
4169 -h|--help)
4242- if [[ "$HELP_FLAG" = true ]]; then help; exit 0; fi
7070+ help; exit 0
7171+ shift ;;
7272+ --formats)
7373+ list_formats; exit 0
4374 shift ;;
4475 -v|--verbose)
4576 VERBOSE_FLAG=true
···5182 USER_OPTIONS=true
5283 OPTIONS+=("$1")
5384 else
5454- echo "Error: File does not exist (Skipping) or invalid argument: $1"
8585+ SKIPFILES+=("$1")
5586 fi
5687 shift ;;
5788 esac
5889done
59906060-if [[ "$VERBOSE_FLAG" = false ]]; then
9191+if [[ $CMD == "ffmpeg" && "$VERBOSE_FLAG" = false ]]; then
6192 OPTIONS+=("-loglevel")
6293 OPTIONS+=("warning")
9494+elif [[ $CMD == "magick" && "$VERBOSE_FLAG" = true ]]; then
9595+ OPTIONS+=("-verbose")
6396fi
64976598if [[ ${#FILES[@]} = 0 ]]; then
6666- echo "Error: No files specified"
9999+ echo "`red Error:` No files specified"
67100 help
68101 exit 1
102102+elif [[ ! ${#SKIPFILES[@]} = 0 ]]; then
103103+ for SKIPFILE in "${SKIPFILES[@]}"; do
104104+ echo "`red Error:` File does not exist (Skipping) or invalid argument: $SKIPFILE"
105105+ done
69106fi
7010771108if [[ "$USER_OPTIONS" = true ]]; then
7272- echo "Passing the following options to ffmpeg: ${OPTIONS[@]}"
109109+ echo "Passing the following options to $CMD: ${OPTIONS[@]}"
73110fi
7411175112for FILE in "${FILES[@]}"; do
7611377114 if [[ -f "$FILE" ]]; then # check if file exists
7878- echo "Converting $FILE"
115115+ echo "Converting `cyan $FILE`"
7911680117 FILENAME=$(basename "$FILE")
81118 FILE_WITHOUT_EXT=${FILE%.*} # remove extension
82119 # echo "FILENAME $FILENAME"
83120 # echo "TEMPDIR $TEMPDIR"
84121 NEW_FILE_WIP="$FILE_WITHOUT_EXT CONVERTING... $((10000 + RANDOM)).$FORMAT"
8585- ffmpeg -i "${FILE}" ${OPTIONS[@]} "$NEW_FILE_WIP"
122122+ if [[ $CMD == "ffmpeg" ]]; then
123123+ ffmpeg -i "${FILE}" ${OPTIONS[@]} "$NEW_FILE_WIP" || { echo `red Error:` Convertion failed; exit 1; }
124124+ else
125125+ magick ${OPTIONS[@]} "$FILE" "$NEW_FILE_WIP" || { echo `red Error:` Convertion failed; exit 1; }
126126+ fi
8612787128 NEW_FILE="$FILE_WITHOUT_EXT converted.$FORMAT"
88129 COUNT=1
···98139 done
99140 mv "$NEW_FILE_WIP" "$NEW_FILE"
100141 else
101101- echo "Error: File no longer exists (Skipping): $FILE"
142142+ echo "`red Error:` File no longer exists (Skipping): $FILE"
102143 fi
103144104145done