From 668ee5a559f1ee08a71081bc4e62268bea43c3e9 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 22 Nov 2017 22:18:53 +0000 Subject: [PATCH] Add reflace & ignore ghostwriter backup files --- .gitignore | 1 + README.md | 1 + reflac | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 .gitignore create mode 100755 reflac diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c190512 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.backup diff --git a/README.md b/README.md index c40cb29..6500129 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Here's a list of the most interesting ones, along with what they do and where th - ssh-retry - I forget where it came from, but this command reepeatedly calls ssh until it succeeds in connecting. - quote-get - Displays a random quote from a remote file. The quote stays the same until the following day. The remote file is cached locally for convenience :smiley_cat: - teleport / telepeek / telepick - A pair of quick functions in my `.bash_aliases` file for quickly jumping about. Works best if you've got a decent amount of free RAM for the cache. + - [reflac](https://github.com/chungy/reflac) - Small flac recompression script ## cloudsurfer.sh `cloudsurfer.sh` is a new script I'm writing to bring some of the customisations I store here in this repository to terminals on shared machines - where I can't modify `.bashrc`, for example. diff --git a/reflac b/reflac new file mode 100755 index 0000000..93f3bee --- /dev/null +++ b/reflac @@ -0,0 +1,173 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' + +VERSION=1.5 + +VERBOSE=0 +RECURSIVE=0 +NOACTION=0 +SYNC=1 +FLAC_LEVEL=5 # Default FLAC compression level +PRESERVE_MODTIME="" # for -p|--preserve + +SELF="$(readlink -f "$0")" +OPTS=$(getopt -o Vvhrnps012345678 \ + -l version,verbose,help,recursive,preserve,no-action,no-sync,fast,best \ + -n reflac -- "$@") +TMPDIR="$(mktemp --tmpdir -d reflac.XXXXXXXXXX)" + +eval set -- "$OPTS" + +trap "exit 1" INT + +usage() +{ + cat < /dev/null 2>&1 || HAS_COVER_ART=0 #the error is handled later. do not exit the script + if [ $VERBOSE -gt 1 ]; then + flac --force-rf64-format --output-prefix="$TMPDIR/" -d -- "$flac" + flac --delete-input-file -$FLAC_LEVEL -- \ + "$TMPDIR/$(basename -s flac -- "$flac")rf64" + else + flac -s --force-rf64-format --output-prefix="$TMPDIR/" -d -- "$flac" + flac -s --delete-input-file -$FLAC_LEVEL -- \ + "$TMPDIR/$(basename -s flac -- "$flac")rf64" + fi + + if [ $HAS_COVER_ART -eq 1 ]; then + metaflac --no-utf8-convert $PRESERVE_MODTIME \ + --import-tags-from="$TMPDIR/$(basename -s flac -- "$flac")tag" \ + --import-picture-from="$TMPDIR/$(basename -s flac -- "$flac")img" \ + -- "$TMPDIR/$flac" + else + metaflac --no-utf8-convert $PRESERVE_MODTIME \ + --import-tags-from="$TMPDIR/$(basename -s flac -- "$flac")tag" \ + -- "$TMPDIR/$flac" + fi + mv -f -- "$TMPDIR/$flac" "$flac.new" + if [ $SYNC -eq 1 ]; then + sync "$flac.new" + mv -f "$flac.new" "$flac" + sync "$flac" + else + mv -f "$flac.new" "$flac" + fi + rm -f -- "$TMPDIR/$(basename -s flac -- "$flac")tag" \ + "$TMPDIR/$(basename -s flac -- "$flac")img" + fi + done +} + +while true ; do + case "$1" in + -v|--verbose) + VERBOSE=$((VERBOSE+1)) + shift ;; + -r|--recursive) + RECURSIVE=1 + shift ;; + -0|-1|-2|-3|-4|-5|-6|-7|-8) + FLAC_LEVEL=${1#-} + shift ;; + --fast) + FLAC_LEVEL=0 + shift ;; + --best) + FLAC_LEVEL=8 + shift ;; + -n|--no-action) + NOACTION=1 + shift ;; + -p|--preserve) + PRESERVE_MODTIME="--preserve-modtime" + shift ;; + -s|--no-sync) + SYNC=0 + shift ;; + -V|--version) + echo "reflac version $VERSION" + rmdir "$TMPDIR" + exit 0 ;; + -h|--help) + usage ;; + --) shift; break ;; + *) echo "$0: This should never happen!" >&2; exit 1 ;; + esac +done + +if [ -z "$*" ]; then + usage +fi + +if [ $VERBOSE -eq 0 ] && [ $NOACTION -eq 1 ]; then + rmdir "$TMPDIR" + exit 0 +fi + +for dir do + if [ $RECURSIVE -eq 1 ] && [ $NOACTION -eq 1 ]; then + find "$(readlink -f -- "$dir")" -type d -execdir "$SELF" -nv {} \; + elif [ $RECURSIVE -eq 1 ]; then + if [ $VERBOSE -eq 1 ]; then + find "$(readlink -f -- "$dir")" -type d \ + -execdir "$SELF" -v -"$FLAC_LEVEL" {} \; + elif [ $VERBOSE -ge 2 ]; then + find "$(readlink -f -- "$dir")" -type d \ + -execdir "$SELF" -vv -"$FLAC_LEVEL" {} \; + else + find "$(readlink -f -- "$dir")" -type d \ + -execdir "$SELF" -"$FLAC_LEVEL" {} \; + fi + else + pushd -- "$dir" >/dev/null + if [ $VERBOSE -gt 0 ]; then readlink -f .; fi + if [ ! -z "$(ls -- *.flac 2>/dev/null)" ]; then + recompress + fi + popd >/dev/null + fi +done + +rmdir "$TMPDIR"