LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 05-22-2007, 07:58 PM   #1
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 632

Rep: Reputation: 36
need a bash script to batch convert .wav to .mp3


hello everybody,
i take it there is no bash subforum so ill just right here.

i have installed the lame encoder, and i wanna write a bash script that reads the contents of a given folder, and runs a command for each file in it.. so basically batch process wav to mp3 conversion.

the files will obviously be *.wav files, the output should be the same file names with .mp3 instead of .wav, and the command is 'lame' along with some options....

i know little of bash in order to carry that out and im hoping that i will start understanding a few things once i see how this hopefully simple script works...

anyone has any idea/ has done it ?
nass
 
Old 05-22-2007, 08:13 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
I'd do something like this:
Code:
#!/bin/bash
LAMEOPTS="-S"

for FILE in *.wav ; do
    OUTNAME=`basename "$FILE" .wav`.mp3
    lame $LAMEOPTS "$FILE" "$OUTNAME"
done
 
Old 05-22-2007, 09:12 PM   #3
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 632

Original Poster
Rep: Reputation: 36
working on the aforementioned problem, the first thing to do is to actually me able to 'read' the filenames into lame
(lame works as: lame [OPTIONS] <input file> <output file>)

so i was thinking to use
Code:
ls | grep '\(.*\.\)wav.*'
to feed the output of ls (some .wav files) into grep that then looks for:
- any text (ie the filename) followed by
- a dot followed by
- the extension 'wav' followed by
- any other text that might be there after, or nothing..

the parenthesis: \( \) are there to save the matched filename followed by the dot, so as to be used with the extension mp3 later on.

im supposed to use \1 to address the filename (the matched expression in the parenthesis) but i haven't figured that out far yet. and at any rate i am not sure how i will concatenate this \1 with the word 'mp3'..

so i hope i got this right so far.. if you know of another way or can help in this way please do so

nass
 
Old 05-22-2007, 09:23 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Did you test my script? In my opinion, it is the most straightforward way. I'm not aware of any way to use grep for string replacement.
 
Old 05-22-2007, 09:44 PM   #5
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 632

Original Poster
Rep: Reputation: 36
i was typing while you replied had i known of your script, i wouldn't even have bothered trying the much more complex grep /sed way... if there is any that is

i tested the script and it works fine... thank you very much!
i will try to make a script so as to allow arguments to be input when calling it.


just out of curiosity though i've reached this far:

lame -m j -h -V 3 --vbr-new `grep '.*\.wav'<list` `sed s/wav/mp3/g<list`

lame [ OPTIONS ]< input file list >< output file list >

list is a temporary file created as ls > list

the thing is now lame exits saying 'excess arguments'... could it be that the whole list is dumped at once both from grep and from sed instead of one file at a time? - i think it is..
is there a way to serialize that ?

thnk you once again for your help
nass
 
Old 05-22-2007, 09:48 PM   #6
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 60
Quote:
`basename "$FILE" .wav`.mp3
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.

Could that also be..... $(basename "$FILE" .wav).mp3 ?

Last edited by rickh; 05-22-2007 at 09:58 PM.
 
Old 05-22-2007, 09:51 PM   #7
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 293Reputation: 293Reputation: 293
First make your list. Then

Code:
for i in `cat list`
do
lame -m j -h -V 3 --vbr-new "$i"
done
or thereabouts
 
Old 05-22-2007, 10:01 PM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Quote:
Originally Posted by rickh
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.

Could that also be..... $(basename "$FILE" .wav).mp3 ?
Yes, it could be. I guess I'm just a backtick kinda guy, though $( ) reads better and allows for nesting.
 
Old 05-22-2007, 10:05 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Quote:
Originally Posted by dive
First make your list. Then

Code:
for i in `cat list`
do
lame -m j -h -V 3 --vbr-new "$i"
done
or thereabouts
Careful with 'cat list'.... This will split oddly on whitespace. You might need to add quotes in some manner.
 
Old 05-22-2007, 10:25 PM   #10
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 632

Original Poster
Rep: Reputation: 36
ok so the script now looks like

Code:
#!/bin/bash
LAMEOPTS=""

if [ "$#" -gt 0 ]; then
  LAMEOPTS="$*"
else
  LAMEOPTS="-m j -h -V 3 --vbr-new"
  echo "no arguments. Assuming: $LAMEOPTS ."
fi

for FILE in $PWD/*.wav ; do
    OUTNAME=`basename "$FILE" .wav`.mp3
    lame $LAMEOPTS "$FILE" "$OUTNAME"
done
~
in order to account for options that the user may prefer to set.
i also placed the script in /usr/bin so everyone can access it
and i added the $PWD so as to be usable anywhere on the filesystem.

problem is that in a folder name has spaces PWD returns an incomplete path

so if , say, the path is /mnt/hd/my\ stuff
echo $PWD will return /mnt/hd/my
which is not a valid directory.... how can i overcome this?
 
Old 05-22-2007, 10:38 PM   #11
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
You should not need the $PWD. It will act on the current working directory as-is.
 
Old 05-22-2007, 10:40 PM   #12
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Well, $PWD should not return an incomplete path as long as you put it in quotes like:

Code:
"$PWD"
 
Old 05-23-2007, 07:35 AM   #13
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slack(64|32)_v(13.37|14.0), debian6, ubuntu
Posts: 632

Original Poster
Rep: Reputation: 36
quite right, i remove the $PWD and it works like a charm.
thank you very much
 
Old 05-23-2007, 09:13 AM   #14
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,525

Rep: Reputation: 94
Quote:
Originally Posted by rickh
Hey! That's a great little addition. This question, has obviously been asked and answered many times, but that's the first time I've seen that included.

Could that also be..... $(basename "$FILE" .wav).mp3 ?
You can also use bash to do this, for example "${FILE//.wav/.mp3}" or "${FILE%%.wav}.mp3". Even more nice ways to do that

If you like to dig deeper, I saw this first few days ago in a great article on bash parameters: http://www-128.ibm.com/developerwork...BashParameters
 
Old 05-23-2007, 01:36 PM   #15
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Wouldn't be Linux if there weren't 5 ways to get the same thing done.
 
  


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
quick bash script to convert flac to mp3 zuralin Programming 8 08-19-2011 04:50 AM
i want to convert .wav file into mp3 files in batch mode sparsh Linux - Software 6 11-20-2007 11:53 AM
Convert mp3 to wav trey85stang Linux - Software 5 08-23-2006 10:57 PM
convert mp3 to wav LadyArtemis Linux - Software 3 05-05-2006 01:43 PM
mp3 to wav convert shibainucan Suse/Novell 1 10-22-2004 11:46 AM


All times are GMT -5. The time now is 05:00 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