Skip to content

Instantly share code, notes, and snippets.

@dmytro
Last active February 20, 2025 23:53
Show Gist options
  • Save dmytro/2a77e007dda596dcf29c to your computer and use it in GitHub Desktop.
Save dmytro/2a77e007dda596dcf29c to your computer and use it in GitHub Desktop.
Set ID3 tags of MP3 files from file names and directory names.
#!/bin/bash
#
# If you have bunch of MP3's in a folder with filenames corresponding
# to the titles of songs, but ID3 tags are missing or misformed (for
# example, coming from Windows with cp1251 codepage, but you need
# UTF8), this script will read filenames and set ID3 tags
# correspondingly.
#
# cd to the directory with mp3 files and execute script
# there. Directories on the upper level should have names:
#
# mp3 files directory name: disc name
# directory 1 level up: author
#
# Convention for directory names is:
#
# ├── Author (composed
# │ ├── Disc title
# │ │ ├── 01 Track 1 <--- execute script in songs directory
# │ │ ├── 02 Track 2
# │ │ ├── 03 Track 3
#
# Requirements: uses eyeD3 Python script
#
# Author: Dmytro Kovalov, 2014
#
# Execute:
#
#
set -e
if [ "$1" = "--dry" ]; then
export DRYRUN="echo"
fi
write_tags () {
local file="$*"
local song_title=$(/usr/bin/basename "$file" .mp3)
track_number=$(echo $song_title | awk '$1 ~ /[0-9]+/ {print $1}' | tr -d .)
if [ ! -z $track_number ]; then
song_title=$(echo $song_title | sed "s/${track_number}//")
track_number_option="--track=$track_number"
fi
local file_dir=$(dirname "$file")
local disk_name=$(basename "$file_dir")
local disk_dir=$(dirname "$file_dir")
local artist_name=$(basename "$disk_dir")
echo ------------------------------------------------------------------
echo artist: $artist_name
echo disc: $disk_name
echo title: $song_title
echo track num: $track_number
${DRYRUN} eyeD3 --to-v2.4 --encoding utf8 \
--title "$song_title" \
--artist "$artist_name" \
--album-artist "$artist_name" \
--album "$disk_name" \
$track_number_option \
"$file"
echo ==================================================================
echo
}
attach_images() {
local file=$@
local dir=$(dirname "$file")
find "${dir}" -iname \*.jpg | while read IMAGE; do
local description=$(basename "${IMAGE}")
printf " --add-image '%s:OTHER' " "${IMAGE}"
done
}
main () {
/usr/bin/find "$(pwd)" -iname \*.mp3 | while read FILE; do
write_tags "${FILE}"
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment