commit e02509cdf2edc7de1e04563bf8fa0ac7d913cf1b Author: Sebastian Hugentobler Date: Thu Jun 22 09:46:43 2023 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..765efc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*~ +.DS_Store +*.pkg.tar.zst +*.pkg.tar.zst.sig diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..62c71c0 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,20 @@ +# Maintainer: Sebastian Hugentobler + +pkgname=music-compiler +pkgver=0.1.0 +pkgrel=1 +pkgdesc="Transcode a directory tree to mp3." +arch=("any") +url="https://code.vanwa.ch" +license=('MIT') +depends=("ffmpeg" "bash") +source=( + "music-compiler" +) +sha256sums=("7401e88c219ffc27066fbe424a9b6ea44377bec4130b3186e626a7b5714e2f31") + +package() { + cd "$srcdir/" + + install -Dm 755 music-compiler "$pkgdir/usr/bin/music-compiler" +} diff --git a/music-compiler b/music-compiler new file mode 100755 index 0000000..3085d4f --- /dev/null +++ b/music-compiler @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -o errexit + +source="./" +dest="" +nproc="$(nproc)" + +display_help() { + echo "Usage: $0 [option...] " >&2 + echo + echo " -s, source directory [default: $source]" + echo " -d, destination directory (the structure of source will be replicated there)" + echo " -n, number of processes to use [default: $nproc]" + echo " -h, display this help and exit" + echo +} + +parse_args() { + while getopts ":hs:d:n:" opt; do + case $opt in + h) + display_help + exit 0 + ;; + s) + source=$OPTARG/ + ;; + d) + dest=$OPTARG/ + ;; + n) + nproc=$OPTARG + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + display_help + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + display_help + exit 1 + ;; + esac + done +} + +transcode() { + local input="$1" + local source="$2" + local dest="$3" + local tmp_dest="${input/$source/$dest}" + + for ending in %.flac %.m4a; do + tmp_dest="${tmp_dest/$ending/.mp3}" + done + local final_dest="$tmp_dest" + + local parent_dir + parent_dir="$(dirname "$final_dest")" + [[ -d "$parent_dir" ]] || mkdir -p "$parent_dir" + + ffmpeg -y -i "$input" -c:a libmp3lame -qscale:a 4 "$final_dest" +} +export -f transcode + +cover_process() { + local input="$1" + local source="$2" + local dest="$3" + local final_dest="${input/$source/$dest}" + + local parent_dir + parent_dir="$(dirname "$final_dest")" + [[ -d "$parent_dir" ]] || mkdir -p "$parent_dir" + + cp "$input" "$final_dest" +} +export -f cover_process + +parse_args "$@" + +SHELL="$(type -p bash) -exv" \ + find "$source" -type f -name "*.flac" -o -name "*.mp3" -o -name "*.m4a" | + parallel -j"$nproc" transcode {} "$source" "$dest" + +SHELL=$(type -p bash) \ + find "$source" -type f -name "cover.jpg" | + parallel -j"$nproc" cover_process {} "$source" "$dest"