LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-13-2010, 11:12 AM   #1
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Rep: Reputation: 0
Script to clean music


Hi, I am looking for a script that renames all MP3 files (and maybe its corresponding tags) of a specific folder and its subfolders.

I want to just rename the file name, so that only the first letter is capitalized, and the first letter that stand behind a "-" (Regard! An empty slot is not a letter!). For examle:

From:
La Quinta Estación - Me Muero
to
La quinta estación - Me muero

(the first letter and the letter that comes after a Hyphen)

##########################################################################

If it is possible, I want to do the same with the IDtag.
E.g in the name of the album:
from
The Mating Game
to
The mating game

The lyric, delete, and the cover too.

Has anyone any idea how to do the first with pyRenamer?
 
Old 01-14-2010, 12:53 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
here's some clues to get you started:

Code:
songname='La Quinta Estación - Me Muero'
fixed_name=$(echo "$songname" | tr "[A-Z]" "[a-z]" | \
sed 's/\(^[[:alpha:]]\)\(.*\)\(- [[:alpha:]]\)\(.*$\)/\U\1\E\2\U\3\E\4/')
echo "$fixed_name"
The above will:
1) change the whole song name to lower case, using `tr`
2) Uppercase the first letter of the whole song name
3) Uppercase the first letter following a '-'

Now:
Code:
tagname='La Quinta Estación'
fixed_tag=$(echo "$tagname" | tr "[A-Z]" "[a-z]" | \
sed 's/\(^[[:alpha:]]\)\(.*$\)/\U\1\E\2/')
echo "$fixed_tag"
The above, does the exact same thing, except it omits the part about the stuff after a dash.

I've never heard of pyRenamer, and am not familiar with python, but the above is done in bash shell, using sed and tr. If you want to recurse through whole directories, it's up to you to add that code to search the directories, run each filename/tag/whatever through the above codes, and rename the files how you like.
I would use `find` to do the recursive search, and of course, `mv` to rename the files.

PS - It would be educational for you if you actually understand the sed code used, (IF you're interested in using it, that is..) so that you can create and/or modify similar code in the future for yourself. If you would like an explanation (a breakdown) of what sed is doing here, just ask.

Cheers!
Sasha

Last edited by GrapefruiTgirl; 01-14-2010 at 01:03 AM.
 
1 members found this post helpful.
Old 01-14-2010, 08:22 AM   #3
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Hi Sasha!
You say that if I have questions, I should ask you, right? So here it goes:

I'm not interested just change the name of one song.
I want to modify the files of all subfolders in a specified folder.

The music folder is located at: /media/160Gb/Musica.
It contains many folders, and these again others and these others, etc ...

I would like to simply run this script from the Music folder, and that he choice all subfolders in the folder where the script is located.
For example: If the script is located on the desktop, and on the desktop are folders with music, when I run the script from the desktop folder, the script will change all files in the subfolders on the desktop.

It seems a bit hard to explain, so I understand if you do not understand me. Sorry.

The most important thing for me is to rename the files. Because there are programs that have the function to change the tags, according to the filename.

Thank you very much for your help,
Patrick
 
Old 01-14-2010, 09:27 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by pkramerruiz View Post
Hi Sasha!
You say that if I have questions, I should ask you, right? So here it goes:

I'm not interested just change the name of one song.
I want to modify the files of all subfolders in a specified folder.

The music folder is located at: /media/160Gb/Musica.
It contains many folders, and these again others and these others, etc ...

I would like to simply run this script from the Music folder, and that he choice all subfolders in the folder where the script is located.
For example: If the script is located on the desktop, and on the desktop are folders with music, when I run the script from the desktop folder, the script will change all files in the subfolders on the desktop.


It seems a bit hard to explain, so I understand if you do not understand me. Sorry.

The most important thing for me is to rename the files. Because there are programs that have the function to change the tags, according to the filename.

Thank you very much for your help,
Patrick
Start from

man find

and/or learn a scripting language like Perl/Python/Ruby.
 
0 members found this post helpful.
Old 01-14-2010, 01:29 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Hi Patrick,

Yes, I understand perfectly what you're asking, and if you want to do the job using Bash (shell) then I have done the hardest part for you (the sed code to clean up each song name) and I did that for my own entertainment and education (I'd never used sed to change case before) but YOU must do some reading and toying around too -- nobody will usually create the whole project for you; you must do some of the work yourself.

I know for a fact that there are numerous threads around LQ, wherein other members have asked about code to do the EXACT same thing as you are trying to do: that is, search a folder or path full of music or videos, and rename all the items in there.

Personally, I don't know Perl, Python, or Ruby, but if you want to use some of these to do the job, take Sergei's advice above, and begin reading about these languages.

If you want to use shell as I have done, then read the bash man page to learn about the basics of FOR loops and IF-THEN-ELSE constructs, and variables; read the man page for the mv command, which will be used to do the actual renaming; and read the man page for find which would be used to do the actual recursive search in the directory and produce each song name for processing.

My offer of further information was mainly for if you wanted me to break down the `sed` statement, and show the various parts, so you would understand how/why the sed statement is doing what it's doing, however if you write some code to do the searching, I'll happily help you debug it.

So, search LQ a bit for some other threads where people are doing the exact same task as you want to do, and modify their examples or write some new code, and present to us what you've come up with. We will gladly help you sort out problems and errors, and point you in the right direction.

Best regards,
Sasha
 
0 members found this post helpful.
Old 02-23-2010, 07:16 AM   #6
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Talking

A very good friend wrote me this:

Quote:
Well if you ask me, i would split the problem
1. finding directories which contain mp3
something simple can be done in shell:
find . -type f -name '*.mp3' | sed 's%[^/]*$%%' | uniq
the find searches for "files" whose name matches the pattern *.mp3. it
returns the list of files found, one file per line
the sed removes the last part of each line, i.e it removes the name,
it keeps only the directory name
uniq merge identical lines, so you end up with all the directories you want

you should write it in perl if you have some knowledge, that could be
helpful for the rest below

2. renaming and tagging
for this one, you have to decide whether you want to code it in bash,
or in a higher level language such as perl or python
i personally know perl, and it comes with a simple library to modify
mp3 id3 tag.
so you should adapt the example given in the forum to perl, and then
use the adapted algo twice:
. once for renaming the file
. another time to rename the id3 tags you want (i.e. title, artist, etc...)

i don't remember by heart how to use id3 in perl, you can have a look
at http://husk.org/perl/id3s/notes.html for example
what i like with perl is the programmatic approach to reading tags and
modifying them
exerp from the link above:
http://husk.org/perl/id3s/notes.html
#!/usr/local/bin/perl
use strict; use warnings;

use MP3::Info;
use MP3::Info::set_mp3v2tag;
my $file = "example.mp3";
my $tag = get_mp3tag($file);

$tag->{GENRE} = "Rock";
$tag->{ARTIST} = "Coldplay";
set_mp3v2tag($file, $tag);

quite straightforward if you know the perl syntax.
MP3::Info is available by
sudo apt-get install libmp3-info-perl

if you're a warrior, you can even play a bit with perl interface to musicbrainz
this way you could grab the album cover for a mp3 and store it in the mp3 directly or save it to disk
Thanks go to: D.Ciabrini
 
Old 02-25-2010, 12:48 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Just thought I would put in another option for the sed:

sed -e 's/^./\U&/' -e 's/- ./\U&/'
 
1 members found this post helpful.
Old 02-25-2010, 01:56 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ grail,

Regarding: sed -e 's/^./\U&/' -e 's/- ./\U&/'

can you explain how/where that works? I just tried to use it for both example file-name & tag provided in the initial post, and the exact same thing came out as went in (so it did nothing)..

Sasha
 
1 members found this post helpful.
Old 02-26-2010, 05:40 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
This works in bash 4+

<code>
#!/bin/bash

songname="La Qunita Estacion - Me Muero"

fixed_name=$( echo "${songname,,}" | sed -e 's/^./\U&/' -e 's/- ./\U&/' )

echo "$fixed_name"
</code>

output# La qunita estacion - Me muero

If on lower bash you might want to use tr from above
 
1 members found this post helpful.
Old 02-26-2010, 06:34 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
hmmmm ... so you got me to thinking more about bash and in version 4 you could also do without the sed
if you so wish:

[CODE]
#!/bin/bash

#songname="La Qunita Estacion - Me Muero"

for songname in "$(find . -name *.mp3)"
do
lower_name="${songname,,}"
start_name="${lower_name%% -*}"
end_name="${lower_name##*- }"

mv "$songname" "${start_name^} - ${end_name^}"
done

#echo "${start_name^} - ${end_name^}"
[CODE]

This gets the same output of course, but was a little bit of fun

Last edited by grail; 02-26-2010 at 09:24 AM. Reason: And a little more help
 
1 members found this post helpful.
Old 02-26-2010, 06:54 AM   #11
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Good morning!
It is wonderful to see you help me!
Especially thanks to my friend D.Ciabrini and grail.

@ grail: I'm so glad, that it works with that song. But how must I run the script to make it operates on the entire folder and sub-folders?
As you will understand, I can't copy and paste the name of each song, and then run the script .:Blinking:. !
It must be possible, to put the script in the folder, and that applies to all content (including the contents of all sub-folders).

Many Thanks!
 
Old 02-26-2010, 07:42 AM   #12
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
Have a look at this
http://tldp.org/LDP/abs/html/loops.html
 
0 members found this post helpful.
Old 02-26-2010, 07:51 AM   #13
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
@whizje:
How interesting (Sarcastic)! An advanced course Bash!
Too bad I have no notions or elementary bash, right?

I promise that if I ever have time, I will learn.
And I mean it (seriously), because my desire is to learn python ...
 
Old 02-26-2010, 07:58 AM   #14
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 583

Rep: Reputation: 129Reputation: 129
If you learn python you will have little trouble with bash scripting.
But if you looked at man find as mentioned your problem would already be solved
a little help
Code:
find . -name '*.mp3' | sed ...
I think that needs to be double quotes because of the spaces
Code:
find . -name "*.mp3" | sed ...

Last edited by whizje; 02-26-2010 at 08:07 AM. Reason: little help
 
1 members found this post helpful.
Old 02-26-2010, 09:04 AM   #15
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
@whizje: Thank you very much! It is a further step towards the final result.
 
  


Reply

Tags
clean, music, rename, script


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to clean music. pkramerruiz Linux - Newbie 3 02-26-2010 07:18 AM
Need script to clean up file stevemcb Programming 26 01-18-2008 06:55 AM
clean music collection edgjerp Linux - Software 5 12-04-2007 03:38 AM
Help with clean-up script fiservguy Programming 5 01-27-2005 01:59 AM
Simple script to clean up old file rbeckett Red Hat 2 09-09-2004 03:38 PM


All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
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