Shell – Replace CD number in MP3 ID3 tag

id3mp3pipeshell

I have a library of MP3 files where in some files the "CD number" tag (TPOS) contains "CDx" values, e.g. "CD1" or "CD2". The ID3 spec says that TPOS values shall be numerical strings. Processing these files breaks some tag editors, specifically eyeD3 ignores the values.

What would be a smart way to replace these ID3 tags with numeric strings, via command line?

I have a solution which comes with some command line magic, I am looking for something simpler:

find . -name "*.mp3" -exec id3v2 -R {} \; |awk -F": " '/^Filename/ {f=$2};/^TPOS/ {print f, "\t", $2}'|grep CD1|awk -F" \t" '{print $1}'|xargs -L 1 -d "\n" -I{} id3v2 --TPOS "1" {}

In a nutshell: find mp3 files, extract the ID3 tags with id3v2, extract filename and TPOS with awk, grep for "CD1", cut out the filename and apply id3v2 to set the disc number to "1".

Best Answer

If file names don't contain newline characters, you could write it as:

exiftool -q -ext mp3 -if '$PartOfSet =~ /^CD\d+\z/' -p \
  '$PartOfSet:$Directory/$FileName' . | while IFS=: read -r p f; do
  id3v2 --TPOS="${p#CD}" "$f"; done
Related Question