Results 1 to 3 of 3

Thread: Re-encode MP3s with lame (script)

  1. #1
    Join Date
    Jan 2009
    Location
    Millbury, MA
    Beans
    414
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED] Re-encode MP3s with lame (script)

    I'm formulating a simple 3-line shell script to batch re-encode MP3s to 128bps CBR using lame. The first line is to convert all spaces in the filenames to underscores:
    Code:
    for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
    and it works just fine.

    The second line does the actual bitrate conversion:
    Code:
    for x in `ls -1 *.mp3`; do lame -b 128 $x NEW-${x}; done
    It works fine too but note that I'm prefixing the output filename with 'NEW-' to avoid the 'Input and output filenames are the same' error. What I'd really like to do is just overwrite the original file with the new one using the original filename if possible.

    Lastly I'm attempting to change the underscores in the filenames back to spaces. I thought I could just reverse the two SET parameters (" " and "_") from the first line to reverse the procedure. When I do that I get an error message that I don't understand. For example, if the current filename is:

    "NEW-01._Led_Zeppelin_-_Black_Dog.mp3"

    and I run the command:
    Code:
    for i in *.mp3; do mv "$i" `echo $i | tr '_' ' '`; done
    I get the following error message:

    mv: target `Dog.mp3' is not a directory

    So why is the filename getting truncated to only the last word and why is 'mv' expecting a directory name?

    Any help would be appreciated.

    tgeer
    Last edited by tgeer43; September 9th, 2009 at 07:46 PM. Reason: SOLVED
    "Programming is an art form that fights back."

    HP Pavillion DV9500T 17" Notebook • 2.2GHz Core2 Duo • 4GB RAM • Nvidia Geforce 8600M GS 512MB
    Running Ubuntu 10.04 [64-bit]+Gnome+Compiz from HD / Puppy Linux 4.2.1SMP in RAM from USB Flash

  2. #2
    Join Date
    Jun 2007
    Beans
    17,337

    Re: Re-encode MP3s with lame (script)

    If you wish to do this way (you'll lose tags) then this may be more suitable - doesn't care about spaces ect.

    Code:
    #!/bin/bash
    mkdir save
    for f in *.mp3; do lame -b 128 "$f" ./save/"${f%.mp3}.mp3"; done
    rm *.mp3
    You may want to not use the rm and confirm first, then remove .mp3's from prompt.

    While not needed here this may work better for the underscore deal

    # Rename to remove all strange chars
    Code:
    rename 's/ /_/g' *.mp3
    # Rename to the original names
    Code:
    rename 's/_/ /g' *.mp3

  3. #3
    Join Date
    Jan 2009
    Location
    Millbury, MA
    Beans
    414
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Re-encode MP3s with lame (script)

    Thanks, mc4man.

    That works just perfectly for me and I've incorporated it.

    tgeer
    "Programming is an art form that fights back."

    HP Pavillion DV9500T 17" Notebook • 2.2GHz Core2 Duo • 4GB RAM • Nvidia Geforce 8600M GS 512MB
    Running Ubuntu 10.04 [64-bit]+Gnome+Compiz from HD / Puppy Linux 4.2.1SMP in RAM from USB Flash

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •