Register
It is currently Sat Aug 30, 2014 8:25 pm

mp3 -> wav -> 48k resample


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Dec 05, 2008 12:33 am   

Joined: Fri Dec 05, 2008 12:23 am
Posts: 3
unfortunately my sound card isn't supported in 40khz mode, it is an onboard card in 48khz, so whenever i get a directory of mp3 files for instance, i have to convert them to wav with mpg321 and then resample with sox.

once i am in the directory of music, i would do

Code:
mpg321 -w file.wav file.mp3
to each file individually, and then do

Code:
sox file.wav -r 48000 48k-file.wav resample
to each newly made wav file individually. i would like to have a script that converts all the mp3s to wav with mpg321 one after the other without having to retype it, and then resample them with sox one after the other, either as two separate scripts (for mpg321 and sox) or one together, whatever is easier.

this will save me a lot of time and be instructional.


Top
 Profile  
 PostPosted: Fri Dec 05, 2008 2:25 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
for i in $(ls --color=no); do
   file_w_ext=$i
   file_wo_ext=$(echo $i | sed -e "s/^\(.*\)\.[a-zA-Z0-9]{3}$/\1/")
   mpg321 -w "${file_wo_ext}.wav" "$file_w_ext"
   if [ $? -eq 0 ]; then
      sox "${file_wo_ext}.wav" -r 48000 "new_${file_wo_ext}.wav" resample
   fi
done

This script will loop throu all files in your current directory, then chop off the file extension (this is to preserve the filename when you change the extension to .wav).
Then it'll encode as you wish it to. If the mpg321 is successful in running it'll resample the file with sox and naming the new one to "new_file.wav"

You could of course add some kind of error handling with an "else"-statement if the mpg321 fails.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Dec 05, 2008 5:14 am   

Joined: Fri Dec 05, 2008 12:23 am
Posts: 3
thank you very much! works excellent (as long as the file names don't have any funny characters in it, i suppose sed would do the job to take out spaces/#/! and replace them with something legal, though i have thunar to do it without knowing any code).


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP