LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-09-2008, 06:55 PM   #1
dzollinger
LQ Newbie
 
Registered: Dec 2007
Posts: 19

Rep: Reputation: 0
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
 
Old 05-09-2008, 07:20 PM   #2
Peacedog
LQ Guru
 
Registered: Sep 2003
Location: Danville, VA
Distribution: Slackware, Windows, FreeBSD, OpenBSD, Mac OS X
Posts: 5,296

Rep: Reputation: 168Reputation: 168
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. ;-)
 
Old 05-11-2008, 08:46 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 05-11-2008, 09:24 PM   #4
Peacedog
LQ Guru
 
Registered: Sep 2003
Location: Danville, VA
Distribution: Slackware, Windows, FreeBSD, OpenBSD, Mac OS X
Posts: 5,296

Rep: Reputation: 168Reputation: 168
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. ;-)
 
Old 05-16-2008, 03:27 PM   #5
dzollinger
LQ Newbie
 
Registered: Dec 2007
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks for all the suggestions. I'm going to get started on this soon, and thanks for the tip about spaces.
 
Old 05-16-2008, 06:57 PM   #6
dzollinger
LQ Newbie
 
Registered: Dec 2007
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 05-16-2008, 07:12 PM   #7
Peacedog
LQ Guru
 
Registered: Sep 2003
Location: Danville, VA
Distribution: Slackware, Windows, FreeBSD, OpenBSD, Mac OS X
Posts: 5,296

Rep: Reputation: 168Reputation: 168
Cool, glad you got that sorted. I'll be filing this away in my "scripts that could be useful" collection.
Good luck. ;-)
 
Old 05-16-2008, 08:37 PM   #8
vadkutya
Member
 
Registered: Apr 2008
Distribution: slackware 10.2
Posts: 117

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

Last edited by vadkutya; 05-16-2008 at 08:45 PM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
need a bash script to batch convert .wav to .mp3 nass Slackware 15 06-23-2007 01:00 AM
Convert mp3 to wav trey85stang Linux - Software 5 08-23-2006 09:57 PM
Convert .mp3 to .wav GtkUser Linux - Software 12 06-07-2006 07:24 AM
convert mp3 to wav LadyArtemis Linux - Software 3 05-05-2006 12:43 PM
mp3 to wav convert shibainucan SUSE / openSUSE 1 10-22-2004 10:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:48 PM.

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