Last active
October 14, 2025 00:42
-
-
Save elfakyn/dd62a92d701b494f1a06cdd57ac6da62 to your computer and use it in GitHub Desktop.
Make a lossy copy of your music library. https://elfakyn.com/knowledge/organize-your-music
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| HELP_STRING=\ | |
| "Make a lossy copy of your music library. https://elfakyn.com/knowledge/organize-your-music | |
| Prerequisites: parallel, opus-tools | |
| Usage: | |
| $(basename $0) SOURCE_DIR DEST_DIR" | |
| if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then | |
| echo "$HELP_STRING" | |
| exit 0 | |
| fi | |
| if [ $# -ne 2 ]; then | |
| echo "Error: Expected 2 parameters, got $#. Usage: $(basename $0) SOURCE_DIR DEST_DIR" | |
| exit 2 | |
| fi | |
| if [[ $(realpath "$2") = $(realpath "$1")/* || $(realpath "$1") = $(realpath "$2")/* ]]; then | |
| echo "Error: SOURCE_DIR and DEST_DIR overlap. Please specify non-overlapping source and destination directories." | |
| exit 1 | |
| fi | |
| if ! command -v opusenc >/dev/null 2>&1; then | |
| echo "Error: opusenc not found. Install opus-tools." | |
| exit 1 | |
| fi | |
| if ! command -v parallel >/dev/null 2>&1; then | |
| echo "Error: parallel not found. Install parallel." | |
| exit 1 | |
| fi | |
| convert_library_process_file() { # source dir, dest dir, file | |
| source="$3" | |
| dest="$2/$(realpath -s --relative-to="$1" "$source")" | |
| mkdir -p "$(dirname "$dest")" | |
| if [ "${source##*.}" == "flac" ]; then | |
| dest="${dest%.flac}.opus" | |
| fi | |
| if [ "$dest" -nt "$source" ]; then | |
| return | |
| fi | |
| if [ "${source##*.}" == "flac" ]; then | |
| # FLAC | |
| opusenc --quiet --vbr --bitrate 256 "$source" "$dest" | |
| elif [ "${source##*.}" == "m3u" ] || [ "${source##*.}" == "m3u8" ]; then | |
| # Playlist | |
| sed 's/\.flac\b/\.opus/g' "$source" > "$dest" | |
| else | |
| # Everything else | |
| cp "$source" "$dest" | |
| fi | |
| } | |
| export -f convert_library_process_file | |
| find "$1" -type f | parallel --progress --jobs 14 --eta --bar convert_library_process_file ${1@Q} ${2@Q} {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment