LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to Convert wav to mp3 (https://www.linuxquestions.org/questions/linux-general-1/script-to-convert-wav-to-mp3-641171/)

dzollinger 05-09-2008 06:55 PM

Script to Convert wav to mp3
 
I have a Centos5 machine just laying around that I am going to use to convert huge numbers (300 gigs+) of wav files to mp3's. What I need to do is make a perl/bash script that can read in the creation date of the file. We need to maintain the original creation date for our records. So we are just going to take some wav file and make the outputted mp3 filename the created date from the wav.

For example:
MSG089348.wav created Jan 5, 2007 at 10:00AM
Would become:
01-05-07 10:00:00.mp3

So two things I need some help with are the perl/bash (whatever works better) to loop through a specified directory grabbing each wav file and it's creation date then running through something like mencoder to spit out the mp3 to a specified directory.


Any help would be appreciated!!

-David

Peacedog 05-09-2008 07:20 PM

I've got a couple of things here downloaded, not authored, that may help with some modification. This should handle the conversions provided you have lame.
Code:

for i in *.wav; do lame --preset standard $i `basename $i .wav`.mp3; done
This may help with the naming. batren Hope something there helps.
Good luck. ;-)

chrism01 05-11-2008 08:46 PM

Can I just suggest you DON'T use spaces in a filename, it always causes issues later. Try underscores.
Also, if you use the format
yyyymmdd_hhmmss.mp3
it'll sort properly by default.

Peacedog 05-11-2008 09:24 PM

I agree about spaces in file names, however, if you do have problems with spaces in file names you could always do this.
Code:

for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
Again, downloaded, not authored. There is also a "space remover" bash script somewhere in this forum that was posted that should be recursive IIRC.
Good luck. ;-)

dzollinger 05-16-2008 03:27 PM

Thanks for all the suggestions. I'm going to get started on this soon, and thanks for the tip about spaces.

dzollinger 05-16-2008 06:57 PM

Well I think I have a fully functional script. In order to run it you have to be in the directory you want to convert and then you pass as an argument the directory you are in. It will then convert them and place them in a new directory structure.

Code:

#!/bin/bash

#check for arguments before starting
if [ $# -gt 0 ]; then


#get options from command line
#first option is directory to process we are going to make the output directory match
DIR_TO_PROCESS=$1

#Other variables
ROOT_PATH_TO_OUTPUT="/home/phoneCalls"
FINAL_OUTPUT_PATH="$ROOT_PATH_TO_OUTPUT"'/'"$DIR_TO_PROCESS"

#check if the output directory exists, create if necessary
if [ -d "$FINAL_OUTPUT_PATH" ]; then
echo ""
else
`mkdir "$FINAL_OUTPUT_PATH"`
fi

#This puts it in this format 2008-05-15_17.33.46
#date -r $filename +%F_%H.%M.%S

#This is a loop for conversion
for filename in *.WAV
do
        OUTPUT_NAME=`date -r "$filename" +%F_%H.%M.%S`
        lame -S --preset 24 "$filename" "$FINAL_OUTPUT_PATH"'/'"$OUTPUT_NAME"'.mp3'
done

#end if no command line args was given
else
echo "Please enter directory to process. Ex: ./convert \"personal calls\""
exit 1
fi

It's still a little messy but it works. Thanks for the pointers.

--David

Peacedog 05-16-2008 07:12 PM

Cool, glad you got that sorted. I'll be filing this away in my "scripts that could be useful" collection.
Good luck. ;-)

vadkutya 05-16-2008 08:37 PM

what about?
 
mencoder <filename>.wav -oac mp3lame -o <filename>.mp3 to convert the files. there's an excellent man page btw ;). you need to have mp3lame support compiled in your mplayer. if not, recompile it.

the easiest way to convert all would be you rename the filenames. and do (assuming the wav files are all in the same directory)
Code:

$ for i in *wav; do mencoder "$i" -oac mp3lame -o "$i".mp3; done
this is not elegant because you end up having files named .wav.mp3 instead of plain mp3 but should work. i put the doublequotes because i do't know if the filenames will have whitespaces or not so this works either way. btw: you can script the renaming as well so this should be no big problem.

there's surely a much more elegant way but you don't do this for price winning i assume. so if in doubt use brute force...and do it quick and dirty ;)

good luck, vadkutya

p.s.: peacedog has provided a way to change the ending. haven't seen it. so maybe you should change the script above like this:
Code:

$ for i in *wav; do mencoder "$i" -oac mp3lame -o `basename "$i" wav`.mp3; done


All times are GMT -5. The time now is 02:44 PM.