Find the answer to your Linux question:
Results 1 to 2 of 2
I'm looking for a script that can find all the mp3s within a folder and all it's sub-directories that either have a bit rate greater than 192kb/s or are vbr, ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    2

    converting mp3s

    I'm looking for a script that can find all the mp3s within a folder and all it's sub-directories that either have a bit rate greater than 192kb/s or are vbr, convert them to 192kb/s, and in the case of the vbr ones, remove the bigger of the two. If possible it would be really be nice if I could retain the id3v2 tags as well, as it would save some work afterwords.

    I don't care what you use to make it work, as long as it's relitively easy to find and compile. I'm currently using arch linux if that makes any difference.

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    Here's a fairly bad one that I just whipped up. It requires id3info, id3cp (both from the id3lib package) and lame (from the package of the same name).

    Create a file called "convert-mp3.sh" in /usr/local/bin, e.g.:

    Code:
    gedit /usr/local/bin/convert-mp3.sh
    Then copy and pasted this code in there:

    Code:
    #!/bin/bash
    
    # get desired bitrate from command line args
    [ $# -ne 1 ] && echo "$0 <Target_BitRate>" && exit 1
    target_br=$1
    
    # make sure bitrate given is a number
    echo $target_br|grep -q ^[0-9]*$
    if [ $? -ne 0 ]; then
      echo "\`$target_br' does not look like a number"
      exit 1
    fi
    
    # path to id3info
    id3info=$(which id3info)
    [ -z "$id3info" ] && echo "id3info: Executable not found" && exit 1
    
    # path to id3cp
    id3cp=$(which id3cp)
    [ -z "$id3cp" ] && echo "id3cp: Executable not found" && exit 1
    
    # path to lame
    lame=$(which lame)
    [ -z "$lame" ] && echo "lame: Executable not found" && exit 1
    
    # input field separator (handles filenames with spaces)
    IFS='
    '
    
    # declare an array
    declare -a mp3s
    
    # find all mp3 files and store in array
    mp3s=($(find . -type f -iname '*.mp3'))
    echo "Number of MP3s found: ${#mp3s[*]}"
    
    # loop thru mp3s
    for (( i=0;i<${#mp3s[*]};i++)); do
      mp3=${mp3s[$i]}
      echo -e "\n$i) $mp3"
      curBr=$($id3info $mp3|grep Bitrate|sed -e 's|^Bitrate:[[:space:]]*\(.*\)kbps|\1|i')
      printf "Current Bitrate: ${curBr}KBps..."
      if [ $curBr -gt $target_br ]; then
        printf "too high! Converting ..."
        out=$($lame -b $target_br $mp3 ${mp3}.${target_br} 2>&1)
        if [ $? -eq 0 ]; then
          echo OK
          # copy id3 tag
          printf "Copying ID3 tag info..."
          out=$($id3cp $mp3 ${mp3}.${target_br} 2>&1)
          if [ $? -eq 0 ]; then
            echo OK
          else
            echo FAILED
            printf "$out\n"
            exit 1
          fi
    
          # uncomment the line below ONLY if you are sure!
    #      mv $mp3 ${mp3}.save && mv ${mp3}.${target_br} $mp3
          
        else
          echo FAILED
          printf "$out\n"
          exit 1
        fi
      else
        echo "okay"
      fi
    done
    Then save and quit the editor, and make the script executable:

    Code:
    chmod +x /usr/local/bin/convert-mp3.sh
    It works on whatever directory you are in (so cd to the parent dir containing MP3s that you want it to chew on). It takes the maximum bitrate as its sole argument - you would want to pass it "192". So putting all that together, you might do this:

    Code:
    cd /mp3s
    /usr/local/bin/convert-mp3.sh 192
    For each MP3 that it finds, it will find the bitrate, and if greater than the bitrate given as the 1st argument to the script (e.g., 192) then it will convert it to that bitrate (192). It will not overwrite the original mp3, it will create a new file that is the same name as the original file, and using the new bitrate as an extension (e.g., "My Sharona.mp3.192"). If the conversion is successful, it will then also copy the ID3 tag info to the new file.

    There is a commented out line after that, which will delete the original mp3 and move the new (".192") file into its place. Only uncomment this line after heavy testing (i.e., do not blame me if I trash your entire MP3 collection!).

    Let me know if it works, or not, or you need it to do something else.

    Note: I did not include anything about vbr cuz i don't have an mp3s with that.

    Edit: Please please please back up anything you care about and test test test before running wild with this!
    Last edited by atreyu; 12-01-2011 at 04:25 AM. Reason: maximum, not minimum

Posting Permissions

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