Skip to content

Instantly share code, notes, and snippets.

@zenofile
Created October 14, 2020 19:44
Show Gist options
  • Save zenofile/8e766827fbf3ec072ab9ad153d939e51 to your computer and use it in GitHub Desktop.
Save zenofile/8e766827fbf3ec072ab9ad153d939e51 to your computer and use it in GitHub Desktop.
Write opus track metadata extracted from filename
#!/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