LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script to insert id3 tags from file and directories (https://www.linuxquestions.org/questions/linux-general-1/bash-script-to-insert-id3-tags-from-file-and-directories-663936/)

jason7 08-19-2008 06:28 PM

bash script to insert id3 tags from file and directories
 
hi:

i have the id3tool script that outputs and inserts id3 1 tags into mp3 files.

i have many mp3s that have no tag at all on my server and would like to tag them according to my directory structure

for example if i have:

/home/mp3s/artist/album/filename.mp3

i want the id3 tags to be inserted into artist, album and filename into title.

i found this script here that is doing the opposite. (organizing mp3s according to id3 tags)

http://www.linuxquestions.org/questi...hlight=id3tool


thanks for any help.

eco 08-19-2008 06:36 PM

Hi,

Do something in the lines of:

$ echo "foo/bar/123" | awk -F/ '{print $1,$2,$3}'
foo bar 123

You can just change it arround:

$ echo "foo/bar/123" | awk -F/ '{print $3, $2, $1}'
123 bar foo

Hope this helps
-eco

jason7 08-19-2008 07:35 PM

thanks but i dont know how to connect it to the id3tool

these are the arguments:

id3tool [<options>] <filename>
-t, --set-title=WORD Sets the title to WORD
-a, --set-album=WORD Sets the album to WORD
-r, --set-artist=WORD Sets the artist to WORD
-y, --set-year=YEAR Sets the year to YEAR [4 digits]


sorry... :scratch:

Kenhelm 08-20-2008 01:47 AM

See if this works; the 'echo' command in front of 'id3tool' is to allow the script to be tested without writing the tags.
If the output is OK just remove the 'echo'.
Code:

#!/bin/bash
for fn in "/home/mp3s/artist/album/"*.mp3
do
 if [ -f "$fn" ];then
  p='.*/\([^/]*\)'
  title=$(echo "$fn" | sed "s%$p\.mp3$%\1%")
  album=$(echo "$fn" | sed "s%$p/$title\.mp3$%\1%")
  artist=$(echo "$fn" | sed "s%$p/$album/$title\.mp3$%\1%")
  # Remove 'echo' from next line after testing script
  echo id3tool -t "$title" -a "$album" -r "$artist"  "$fn"
 fi
done


jason7 08-20-2008 05:52 AM

Thanks Kenhelm, this is working. The last thing i guess would be to have the script read the folders the files are in so that i dont have to edit the script everytime.

I guess this is simple but i dont know how to do it.

for example if file is in

/home/user/domain/mp3s/artist/album/file.mp3

the script would ignore
/home/user/domain/mp3s/

and use the folders and filename after that to tag the mp3.

thanks, this is my last question, i promise. :)

Kenhelm 08-20-2008 07:06 AM

If you mean tag all the files for every artist and every album then try:-
Code:

for fn in /home/user/domain/mp3s/*/*/*.mp3

jason7 08-20-2008 07:48 AM

Thanks!

For you:
http://www.smh.com.au/ffximage/2006/..._470x323,0.jpg

jason7 08-30-2008 08:32 PM

after a few days playing with the script there is actually one more thing i would like it to do.

i would like it to add track number depending on the file list.

if i have this files in a folder:

song1.mp3
song2.mp3
song3.mp3
anothersongA.mp3
anothersongB.mp3
anothersongC.mp3


i would like to add track numbers according to the filelist so that i get:

song1.mp3 >> track 1
song2.mp3 >> track 2
song3.mp3 >> track 3
song8_anothersong.mp3 >> track 4
randomsong.mp3 >> track 5
singer.mp3 >> track 6

the script should read the filelist and add track numbers to each file in order.

id3tool -c filename...

thanks for any help.

eco 08-31-2008 02:36 AM

Hi,

Here is a basic script I came up with. You will need to modify it if you are using spaces:

filelist
Code:

song1.mp3
song2.mp3
song3.mp3
anothersongA.mp3
anothersongB.mp3
anothersongC.mp3

script
Code:

c=`cat filelist|wc -l`
for i in `seq 1 $c`; do
  echo "`head -n ${i} filelist |tail -n1` --> Track_${i}"
done

output
Code:

song1.mp3 --> Track_1
song2.mp3 --> Track_2
song3.mp3 --> Track_3
anothersongA.mp3 --> Track_4
anothersongB.mp3 --> Track_5
anothersongC.mp3 --> Track_6


jason7 08-31-2008 04:54 AM

thanks. i guess this is taking the last filenumber and using it to insert tracks???
that is good but not exactly what i need because some of my files dont have a number and are mixed with many others.

i would prefer if the script would just read the file list using "ls" and add tracks to all files in order.

is this possible?

thanks and sorry for the inconvenience.

eco 09-01-2008 09:49 AM

Quote:

Originally Posted by jason7 (Post 3265073)
thanks. i guess this is taking the last filenumber and using it to insert tracks???

Hi,

Not exactly. It's counting the number of lines
Code:

c=`cat filelist|wc -l`
and using the following to count from 1 to $c
Code:

`seq 1 $c`
You can adapt the script to do an ls rather than read from a file.

man bash is your friend ;)

Is it what you needed after all? It's basically taking the files in the order of the list and adding the 'line number' to it.

jason7 09-01-2008 02:17 PM

for you!
http://cafeultravioleta.files.wordpr...er-posters.jpg

David the H. 09-01-2008 02:38 PM

For the record, krename can rename files using id3/metadata tags and directory structures. Most gui tagging tools such as easytag and audiotagtools can also rename files from tags, and I believe you can usually add a directory path to the name with some of them as well.

eco 09-02-2008 01:10 PM

Quote:

Originally Posted by jason7 (Post 3266406)

Hey, just glad I could help.

Best of luck and don't give up, it's so much fun!

Cheers,
-eco

Jobauma 07-14-2009 02:27 AM

Hi.



I wondered if someone could help me. I have all my songs organized like this:
<artist>/<album> (<year>)/<track number> <title>.mp3

EXAMPLE
Aphex Twin/Windowlicker (1999)/01 Windowlicker.mp3

...with the exception that I sometimes do not include the year, like this:
<artist>/<album>/<track number> <title>.mp3 (if this isn't possible, let's say I always include the year)

In addition I would like all previous ID3 tags other than artist, album, year, track number and title to be removed, before the mentioned ones are applied.

What would the script look like then?

Code:

-t, --set-title=WORD Sets the title to WORD
-a, --set-album=WORD Sets the album to WORD
-r, --set-artist=WORD Sets the artist to WORD
-y, --set-year=YEAR Sets the year to YEAR
-n, --set-note=WORD Sets the note to WORD
-g, --set-genre=INT Sets the genre code to INT
-G, --set-genre-word=WORD Sets the genre to WORD
-c, --set-track=INT Sets the track number to INT



Also, I'm not a programmer in any way, so some guidance into how to execute scripts with id3tool is welcome. ;)

Jobauma 07-20-2009 05:10 AM

Can somebody please help me, please?


All times are GMT -5. The time now is 06:47 PM.