Hex3

From LQWiki
Jump to navigation Jump to search

Hex3 is a script to change ID3 tags in MP3 files. The information in an ID3 tag is what shows up on the screen of an MP3 player when a song is being played; artist, track-title, etc.

hex3.sh edits MP3 files when normal ID3 tag editors can't. It is written especially for KCRW's "Today's Top Tune". All ID3 tags of these tracks are set to "Podcast". This script changes the string "Podcast" to "Unknown".

The information in the track file is in a field called "TCON" (Tag CONTENT, I'm guessing), and although I can see the field with id3info, I cannot change it with id3tag.

I suppose a good extension of this script would be an input parser which would translate command line arguments from ASCII into hex. Maybe I'll do that later.


GOTCHA 1:

There is the slight possibility that the same sequence of hex digits may be found elsewhere in the file, but;

  • The ID3 tags are usually at the beginning so without using 'g' on sed it will stop after changing the first sequence
  • Even if you did change a sequence of seven words or so, it probably wouldn't be noticeable in an audio file

GOTCHA 2:

Because these are binary/hex fields, the replacement string has to be the same length as the original (pad with spaces - 0020 hex - if shorter, truncate if longer), otherwise you risk corrupting the actual data. Check to make sure the edited file is the same byte size as the original.

This script is not set to overwrite any existing files, but it might be a good idea to copy the files you want to change to a separate directory, just to be doubly-paranoid.

So, without further preamble:

Take the code below, paste it into a file hex3.sh and execute it like this: sh hex3.sh mysong.mp3

#!/bin/bash
#
# hex3.sh

EXT=`echo $1 | sed -e 's/^.*\.//'`
NAME=`basename $1 ".$EXT"`

echo "NAME = $NAME"
echo "EXT = $EXT"

# Hex dump the file and remove newlines
xxd -p $NAME.$EXT | tr -d '\n' > $NAME.hex

#  0050 006f 0064 0063 0061 0073 0074
#     P    o    d    c    a    s    t
#
#  0055 006e 006b 006e 006f 0077 006e
#     U    n    k    n    o    w    n
#
# Replace hexadecimal Podcast with Unknown
sed -e 's/0050006f00640063006100730074/0055006e006b006e006f0077006e/' $NAME.hex > $NAME-f.hex

# Restore the newlines
sed -e 's/\(.\{60\}\)/\1\n/g' $NAME-f.hex > $NAME.hex
# On Solaris, the above didn't work.  I had to use perl
#perl -pe 's/(.{60})/\1\n/sg' $NAME-f.hex > $NAME.hex

# Revert the dump to binary
xxd -r -p $NAME.hex $NAME-x.mp3

# Get rid of temp files
rm -f $NAME.hex $NAME-f.hex