LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-19-2008, 06:28 PM   #1
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Rep: Reputation: 0
Question 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.
 
Old 08-19-2008, 06:36 PM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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
 
Old 08-19-2008, 07:35 PM   #3
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
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...
 
Old 08-20-2008, 01:47 AM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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
 
Old 08-20-2008, 05:52 AM   #5
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 08-20-2008, 07:06 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
If you mean tag all the files for every artist and every album then try:-
Code:
for fn in /home/user/domain/mp3s/*/*/*.mp3
 
Old 08-20-2008, 07:48 AM   #7
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks!

For you:
http://www.smh.com.au/ffximage/2006/..._470x323,0.jpg
 
Old 08-30-2008, 08:32 PM   #8
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 08-31-2008, 02:36 AM   #9
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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

Last edited by eco; 08-31-2008 at 02:40 AM.
 
Old 08-31-2008, 04:54 AM   #10
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 09-01-2008, 09:49 AM   #11
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Quote:
Originally Posted by jason7 View Post
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.
 
Old 09-01-2008, 02:17 PM   #12
jason7
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
for you!
http://cafeultravioleta.files.wordpr...er-posters.jpg
 
Old 09-01-2008, 02:38 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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.
 
Old 09-02-2008, 01:10 PM   #14
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
Quote:
Originally Posted by jason7 View Post
Hey, just glad I could help.

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

Cheers,
-eco
 
Old 07-14-2009, 02:27 AM   #15
Jobauma
LQ Newbie
 
Registered: Jul 2009
Posts: 2

Rep: Reputation: 0
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. ;)

Last edited by Jobauma; 07-14-2009 at 06:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 09:07 AM
No japanese in ID3 tags NobeyamaGP Linux - Software 0 02-25-2007 07:13 PM
Truncated ID3 Tags Napalm Llama Linux - Software 9 03-17-2006 01:48 AM
id3 tags don't seem to appear Valhalla Linux - Software 1 12-13-2005 11:17 AM
Reading Id3 tags jpc82 Programming 7 10-10-2005 08:50 AM


All times are GMT -5. The time now is 09:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration