LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with a little script using LAME (https://www.linuxquestions.org/questions/linux-software-2/help-with-a-little-script-using-lame-435308/)

rickh 04-15-2006 12:20 AM

Help with a little script using LAME
 
I have a bunch of .mp3 files I want to convert to monaural. I found an old thread here that lined out how to set up a loop for LAME to process all the files in a directory, and modified it to make the changes I needed. This is it:
Code:

for song in tmp/*.mp3; do lame -m m -b 160 $song; done
It works beautifully except that it writes the new files out with an additional .mp3 at the end so all the songs are named title.mp3.mp3. I assume it's doing this to protect the original files named title.mp3

How can I have it drop the outfiles into a different directory so that name change won't be neccessary?

acid_kewpie 04-15-2006 01:34 AM

well the lame manpage states you can provide an in file and and out file, so just provide names including paths there, e.g. old/blah.mp3 and new/blah.mp3 and that'll take care of what you need. tools like basename and dirname can be really useful for removing directories and extensions from filename strings too.

rickh 04-15-2006 09:31 AM

Quote:

...so just provide names including paths
I tried ... Believe me, I tried.

My shell scripting knowledge is sparse indeed, but in the case of this little jewel, it appears to me that infile is supplied by the variable 'song', and outfile by '$song'.

I tried several forms adding directory instructions to $song ("/$song", "home/$song", /home/"$song", etc) , but none worked.

If I can't get it to work in the script, I'll check out your file renaming tools. That was going to be my next area of investigation. This little script works so beautifully, tho ... I'd like to get it to finish the job.

acid_kewpie 04-15-2006 12:50 PM

well you should say what "none worked" really means... what happened on each attempt?

in your example above your file list is "tmp/a.mp3, tmp/b.mp3" etc... so if you try to use "home/$song" you'll be trying to write to "home/tmp/a.mp3" etc, and of course all these are going to be relative to the current directory.

rickh 04-15-2006 01:43 PM

OK. That gave me enough information to solve the problem.

In my ~/tmp directory where the original files are, I created another directory also named ~tmp/. This child directory MUST have the same name as the mother directory. (I hope that terminology is correct.) I then changed the code to
Code:

for song in tmp/*.mp3; do lame -m m -b 160 "$song" "tmp/$song"; done
What a beautiful thing!

archtoad6 04-16-2006 11:36 AM

Quote:

Originally Posted by rickh
... it appears to me that infile is supplied by the variable 'song', and outfile by '$song'. ...

In your original form of the script, infile was supplied by '$song' & outfile was not supplied at all. The 'song' in 'for song in tmp/*.mp3;' was setting the values to be used by the variable. 'song' & '$song' are the same variable, 'song' is its name & '$song' is its value -- it's a bash thing.


Quote:

Originally Posted by rickh
... This child directory MUST have the same name as the mother directory. ...

I think you will find any proper path will do in constructing the outfile suppied to LAME (or any other app.). I believe this would also work:
Code:

mkdir ~/mono_mp3s
for song in tmp/*.mp3
do lame -m m -b 160 "$song" "~/mono_mp3s/$song"
done

BTW, do you cd to ~/tmp when you run the script?

rickh 04-16-2006 12:12 PM

Quote:

BTW, do you cd to ~/tmp when you run the script?
No. I cd to the directory that contains /tmp/. Thanks for the further instruction. I know a number of people who can use this function. Do you think I can make it work from a Windows command prompt?

archtoad6 04-17-2006 07:19 AM

Quote:

Originally Posted by rickh
... Do you think I can make it work from a Windows command prompt?

Maybe. -- I think it's not so much getting the script to run, but getting LAME to run. The script itself is simple enough that it could probably be rewritten as a Win .cmd file (i.e. 2k/XP script).

Sorry I can't help you w/ the getting LAME to run part -- since May of last year all the computers I own run only GNU/Linux.

If you don't mind my asking, why do you want to run this from a Win prompt?

rickh 04-17-2006 09:16 AM

LAME is also available for Windows. I don't want to run it there, but most of the people I know use Windows. This sort of function is a good introduction to Open-source.

Also I looked over your script, and I don't think it will work unless I actually cd into the directory where the original files are. If I did that would I remove the 'in' from the initial line?

archtoad6 04-17-2006 02:21 PM

My bad, that script (of mine) is terrible, well sloppy anyway.

I think this is right, or at least better:
Code:

## initialize the following w/ the appropriate dirs
ORIG=<~/location/of/stereo/mp3s>
MONO=<~/location/to/put/mono/mp3s>

for SONG in $ORIG/*.mp3
do lame -m m -b 160 $SONG $MONO/`basename $SONG`
done


Quote:

If I did that would I remove the 'in' from the initial line?
NO! -- the 'in' is part of the 'for ... in ... do' syntax. See the bash man page, do a search on "do list" rather than "for" to get to the right section.

rickh 09-06-2006 01:07 PM

Everyone else can ignore this ...
 
It seems like I'm constantly coming up with the need to go back and review instructions I've received regarding the manipulation of .mp3 files using lame in a script, renaming mp3s, etc. As time goes by, I have a harder and harder time finding the pertinent threads ... so this is a little helper just for me.

Identify Duplicate Files by Filname
'ls' .. or something to identify filenames that have a (space)(numeral) pattern
Script to change file names with incremental counter.


All times are GMT -5. The time now is 09:30 PM.