Search code examples
bashlame

How to convert all directory mp3 files in a subdirectory with lame using bash?


I'm using git bash in Windows 8.1 and I have lame installed. My git is in my Laravel directory. For 1 conversion I use :

/c/Lame/lame --mp3input -V2 -b 64 public/songs/album/album_1000/song_Emmene-moi_1379645693.mp3 public/songs/album/album_1000/64kb/song_Emmene-moi_1379645693.mp3

Which works great. But when I want to convert all at once I try :

for file in public/songs/album/album_1000/*.mp3; 
do /c/Lame/lame --mp3input -b 64 "$file" 64kb/"${file%.*}".mp3; done

I get error :

Can't init outfile '64kb/public/songs/album/album_1000/song_Bagay 9 (Remix)_1379645802.mp3' Can't init outfile '64kb/public/songs/album/album_1000/song_Bootleg_1379645790.mp3' Can't init outfile '64kb/public/songs/album/album_1000/song_Emmene-moi_1379645693.mp3' ...

I need to add them in a subdirectory called 64kb like public/songs/album/album_1000/64kb/song_Emmene-moi_1379645693.mp3

This is to use later in PHP code, I will have variable for album, album_1000 and the mp3 song name, so any string replace on album_1000/ to album_1000/64kb/ will be just fine.


Solution

  • After some tests I get it to work with the internal string replacement pattern

    {$Var//string_to_replace/new_string} :
    
    $ for file in public/songs/album/album_1000/*.mp3;
    do /c/Lame/lame --mp3input -b 64 "$file" "${file//album_1000/album_1000/64kb}"; done
    

    That worked for me.