Created
October 14, 2020 19:44
-
-
Save zenofile/8e766827fbf3ec072ab9ad153d939e51 to your computer and use it in GitHub Desktop.
Write opus track metadata extracted from filename
This file contains 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 | |
trim() { | |
local var="$*" | |
# remove leading whitespace characters | |
var="${var#"${var%%[![:space:]]*}"}" | |
# remove trailing whitespace characters | |
var="${var%"${var##*[![:space:]]}"}" | |
printf '%s' "$var" | |
} | |
cleanup() { | |
rm -rf "${MYTMP}" | |
} | |
(( $# != 1 )) && exit 1 | |
MYTMP=$(mktemp -qd) | |
trap 'cleanup' EXIT | |
write() { | |
local name out buf nr | |
name=$(basename "$1") | |
out="${MYTMP}/${name}" | |
buf=$(trim "$(printf '%s' "$name" | cut -d ' ' -f 1)") | |
nr=${buf##+(0)} # remove potential padding | |
if [[ $nr =~ ^[0-9]+$ ]]; then | |
ffmpeg -i "$1" -acodec copy -metadata TRACKNUMBER="$nr" "$out" | |
touch -r "$1" "$out" | |
mv "$out" "$(dirname "$1")" | |
fi | |
} | |
write "$(realpath -e "$1")" | |
#EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment