Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-08-2005, 01:54 PM
|
#1
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Rep:
|
Bash script for LAME
Hi all,
I'm fairly new to Linux and totally new to bash scripting. I've been using LAME to convert .wav files to .mp3 files and the task has become a tedious one so I'm trying to simplify things.
When I use XMMS with its CD-Writer plugin to extract songs from a CD I end up with this:
Track\ 01.wav
Track\ 02.wav
Track\ 03.wav
and so on...
My ultimate goal is to end up with a directory of songs that appear like this:
band_name-song_name.mp3
band_name-song_name.mp3
band_name-song_name.mp3
and so on...
Currently I have a script which I call "lameall" and it reads:
--begin script--
#!/bin/bash
# Rename disk writer's output by taking out "Track\".
echo Ranaming files...
mv Track\ 01.wav 01.wav;
mv Track\ 02.wav 02.wav;
mv Track\ 03.wav 03.wav;
mv Track\ 04.wav 04.wav;
mv Track\ 05.wav 05.wav;
mv Track\ 06.wav 06.wav;
mv Track\ 07.wav 07.wav;
mv Track\ 08.wav 08.wav;
mv Track\ 09.wav 09.wav;
mv Track\ 10.wav 10.wav;
mv Track\ 11.wav 11.wav;
mv Track\ 12.wav 12.wav;
mv Track\ 13.wav 13.wav;
mv Track\ 14.wav 14.wav;
mv Track\ 15.wav 15.wav;
echo "Ignore errors, it's okay";
echo "Starting LAME";
# convert *.wav into *.mp3
LIST=$(ls *.wav)
for i in $LIST; do
ORIG=$i
DEST=$(ls $i | cut -d "." -f 1).mp3
lame -h $ORIG $DEST
echo "done converting $i, removing.."
rm $i
done;
echo LAME complete!
--end script--
This works pretty well, but I'm not satisfied. I know the first part of the script is sloppy and it always results in errors if there are less than 15 tracks in the directory (no such file errors).
This script ultimately leaves me with:
01.mp3
02.mp3
03.mp3
and so on...
How can I use a while loop in renaming the "Track\ 01.wav" to "01.wav" so that I won't get the "no such file" errors? I need to take out the "Track\" because I run into problems with LAME.
How can I write an interactive script so that it will ask me for the band_name then ask me for each song_name then it automatically prepends the song_name with the band_name?
Like this:
band_name-song_name.mp3
I hope I am clear here. Remember, I'm a newbie so forgive me if this is stupid...this is more of a scripting exercise for me than anything else, but it would also be a useful script for me.
Thanks,
LocoMojo
|
|
|
01-08-2005, 02:43 PM
|
#2
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
For the first part, why not implement a for loop to look at each song and use sed to remove the "Track \". That's what I would do.
As for the second part, can't help you there as I don't know how XMMS lists the songname/bandname.
-twantrd
|
|
|
01-08-2005, 02:56 PM
|
#3
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Hi Twantrd,
Thanks for your reply!
"For the first part, why not implement a for loop to look at each song and use sed to remove the "Track \"."
What is sed and how would I implement it in this case? Could you give me an example?
"As for the second part, can't help you there as I don't know how XMMS lists the songname/bandname. "
As I said earlier XMMS outputs:
Track\ 01.wav
Track\ 02.wav
Track\ 03.wav
and so on...
I'm trying to first rename the "Track\ number.wav" to "number.wav" so that LAME can work with it. Then after LAME does its job I want to be presented with questions which will accept answers from me.
The first question should be, "what is the band name?"
I would answer, for instance, "iron_maiden".
Then it should as me, "what is the name of the first song (01.mp3)?
I would answer, "song_name".
Then it should prepend the song_name with the band_name and add the .mp3 extension so that it appears as such:
band_name-song_name.mp3
Then it should ask me, "what is the name of the second song (02.mp3)?
I would answer, "song_name".
and so on....
Thanks,
LocoMojo
|
|
|
01-09-2005, 01:47 PM
|
#4
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
Code:
What is sed and how would I implement it in this case? Could you give me an example?
twantrd@debian:~/testing$ echo Track\ 01.wav | sed 's/.* //'
01.wav
As you can see, the way I've used sed removes "Track ".
Code:
Then after LAME does its job I want to be presented with questions which will accept answers from me
LIST=$(ls *.wav)
for i in $LIST; do
echo "What is the bandname?"
read band
echo "What is the name of the first song?"
read song
#ORIG=$i #As you don't need this.
DEST=$(ls $i | cut -d "." -f 1).mp3
lame -h $i $DEST
echo "done converting $i, removing.."
rm $i
done;
I've given you the syntax on how to ask questions and how to grab the input. Now, you can change the script around to suit your needs. Let me know if you have any questions...good luck
-twantrd
|
|
|
01-09-2005, 02:39 PM
|
#5
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Hey twantrd,
Thanks for that, but I got it all figured out last night.
I didn't use sed though, I used more code to do the work as follows:
LIST=$(ls Track* | cut -c 7-8)
for i in $LIST; do
ORIG=Track_$i.wav
DEST=$i.wav
mv $ORIG $DEST
done;
I guess I need to do some learning about sed
Also, your way of grabbing the band name and song name runs you through a loop of band names when you only need it once. I did it this way:
LIST=$(ls *.mp3 | cut -c 1-2)
echo -n "Please type the name of the band and press [Enter]:"
read bname
for i in $LIST; do
echo -n "Please type the name of the song for $i.mp3 and press [Enter]:"
read sname
mv $i.mp3 $bname-$sname.mp3
done;
This way I am only asked for the band name once and then for each song name.
Thanks for your replies! Man, I'm so happy I moved to Linux...I love this stuff!
LocoMojo
|
|
|
01-09-2005, 03:02 PM
|
#6
|
Senior Member
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,306
Rep:
|
this is part of a script i use to do something similar. it might give you a few ideas. I'll have to edit it for you, so i may make a mistake.
for i in *.wav ; do
nice -n 15 lame --preset 160 -b 64 -B 224 --lowpass 17k "$i"
rm "$i"
done
rename "Track " "" *.mp3
rename .wav.mp3 .mp3 *.mp3
and now that you've had fun scripting, why aren't you using grip and the online cddb.
Last edited by whansard; 01-09-2005 at 03:10 PM.
|
|
|
01-09-2005, 06:41 PM
|
#7
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
Code:
Also, your way of grabbing the band name and song name runs you through a loop of band names when you only need it once. I did it this way
Ahh yes, I meant to put that outside the for loop. Good catch.
-twantrd
|
|
|
All times are GMT -5. The time now is 03:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|