LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-08-2005, 01:54 PM   #1
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Rep: Reputation: 30
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
 
Old 01-08-2005, 02:43 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
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
 
Old 01-08-2005, 02:56 PM   #3
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
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
 
Old 01-09-2005, 01:47 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
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
 
Old 01-09-2005, 02:39 PM   #5
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
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
 
Old 01-09-2005, 03:02 PM   #6
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,306

Rep: Reputation: 66
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.
 
Old 01-09-2005, 06:41 PM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

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


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
ALAC & Lame Script Soulful93 Programming 6 04-15-2009 07:24 PM
Script: lame stumbles on spaces in filename browny_amiga Linux - General 5 05-14-2008 08:14 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:57 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
Open Source Consulting | Domain Registration