Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A suite of shell scripts for FLACs and MP3s UNIX
When I first encountered the iTunes-LAME Encoder, I was excited about the idea of making the process of ripping CDs to LAME-encoded MP3s easier. I was, however, pretty underwhelmed by the product. Despite the genorous comments on MacUpdate and VersionTracker, I found the software inelegent and generally rough around the edges. So I set out to accomplish the same task via the command line, which I did. And more. What began as a simple hack for batch-encoding CDs and setting ID3 tags turned into a suite of programs aimed at streamlining the handling of MP3 and flac files from the command line. Here are my seven simple scripts to make your audio life more fun:
  1. lameit - rips CDs to mp3 format using LAME
  2. flacit - rips CDs to flac format
  3. flacmp3 - converts flac files to mp3 files
  4. id3hack - uses the filename to set a track's name and number in its id3 tag
  5. vchack - creates a vorbis comment for a flac file using the above method
  6. vctool - borrows the id3tool interface to set vorbis comments in flac files
  7. vcid3 - converts vorbis comments in flac files to id3 tags in mp3 files
There are, of course, some pre-required binaries that you need in order to make these scripts work. I reccomend you install them using fink or put them in your $HOME/bin directory. They are: Read the rest of the hint for the scripts...

[robg adds: I haven't tested these scripts. Remember to make them excecutable (chmod 755 script_name) after you create them...]

And away we go! The first script uses LAME to rip a CD to the current directory.
#!/bin/sh
#
# lameit - rip a cd to lame-encoded mp3s
#
if [ "$1" ]
then
  for file in "$1"/[1-9]\ *.aiff
  do
    if [ -e "$file" ]
    then
      lame -h -m s -b 192 "$file" "0$(basename "$file" .aiff).mp3"
    else
      echo >&2 "No appropriate files exist in directory: "$1""
      exit 1
    fi
  done
  for file in "$1"/[1-9][0-9]\ *.aiff
  do
    if [ -e "$file" ] 
    then
      lame -h -m s -b 192 "$file" "$(basename "$file" .aiff).mp3"
    fi
  done
else
  echo >&2 "Usage: "$(basename "$0")" /path/to/cd"
  exit 1
fi
This script does the same thing, but with FLAC.
#!/bin/sh
#
# flacit - rip a cd to flac format
#
if [ "$1" ]
then
  for file in "$1"/[1-9]\ *.aiff
  do
    if [ -e "$file" ]
    then
      flac \
        --endian=little \
        --sign=signed \
        --channels=2 \
        --sample-rate=44100 \
        --bps=16 \
        --skip=20 \
        --output-name="0$(basename "$file" .aiff).flac" \
        "$file"
    else
      echo >&2 "No appropriate files exist in directory: "$1""
      exit 1
    fi
  done
  for file in "$1"/[1-9][0-9]\ *.aiff
  do
    if [ -e "$file" ]
    then
      flac \
        --endian=little \
        --sign=signed \
        --channels=2 \
        --sample-rate=44100 \
        --bps=16 \
        --skip=20 \
        --output-name="$(basename "$file" .aiff).flac" \
        "$file"
    fi
  done
else
  echo >&2 "Usage: "$(basename "$0")" /path/to/cd"
  exit 1
fi
And here's the way to get from FLAC to MP3 in one step. It outputs the MP3 files to your current directory but the FLAC files needn't be in your current directory.
#!/bin/sh
#
# flacmp3 - convert a flac file to mp3
#
if [ "$1" ]
then
  for file
  do
    if [ -e "$file" ]
    then
      flac -c -d "$file" | lame -h -m s -b 192 - "$(basename "$file" .flac).mp3"
    else
      echo >&2 "No such file: "$1""
      exit 1
    fi
  done
else
  echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
  exit 1
fi
Next comes the question of labeling the files. I use id3tool to slap together id3 tags before importing them into iTunes because they get lost in my collection otherwise. id3tool works fine for labeling the artist, album, year, and genre, but setting the track number and song title can become tedious. So here's a little hack I whipped up. It only works if the files are named with the two-digit track number followed by its name. For example: "04 And Here We Test Our Powers of Observation.mp3" or "01 Moondance.mp3" or "05 500 Miles.mp3." You can specify as many files as you want on the command-line. I usually just use the "*.mp3" wildcard.
#!/bin/sh
#
# id3hack - add track names and numbers to id3 tags
#
if [ "$1" ]
then
  for file
  do
    if [ -e "$file" ]
    then
      id3tool \
        --set-title="$(echo "$file" | sed 's/...\(.*\)\.mp3/\1/')" \
        --set-track="$(echo "$file" | sed 's/\(..\).*/\1/')" \
        "$file"
    else
      echo >&2 "No such file: "$1""
      exit 1
    fi
  done
else
  echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
  exit 1
fi
And again, this is the same script as the one above, only it creates vorbis comments for FLAC files as opposed to id3 tags for mp3s:
#!/bin/sh
#
# vchack - add track names and numbers to flac files
#
if [ "$1" ]
then
  for file
  do
    if [ -e "$file" ]
    then
      metaflac \
        --set-vc-field=TITLE="$(echo "$file" |
          sed 's/...\(.*\)\.flac/\1/')" \
        --set-vc-field=TRACKNUMBER="$(echo "$file" |
          sed 's/\(..\).*/\1/' |
          sed 's/0\(.\)/\1/')" \
        "$file"
    else
      echo >&2 "No such file: "$1""
      exit 1
    fi
  done
else
  echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
  exit 1
fi
Speaking of vorbis comments, those things are a pain to work with. Out of frustration, I wrote a script that brought the id3tool interface over to the world of vorbis comments and FLAC.
#!/bin/sh
#
# vctool - set vorbis comments in flac files
#
if [ "$1" ]
then
  while getopts t:a:r:y:g:c:h option
  do
    case "$option" in
      t) TITLE="--set-vc-field=TITLE="$OPTARG"";; 
      a) ALBUM="--set-vc-field=ALBUM="$OPTARG"";;
      r) ARTIST="--set-vc-field=ARTIST="$OPTARG"";;
      y) DATE="--set-vc-field=DATE="$OPTARG"";;
      g) GENRE="--set-vc-field=GENRE="$OPTARG"";;
      c) TRACKNUMBER="--set-vc-field=TRACKNUMBER="$OPTARG"";;
      h) echo ""$(basename "$0")" <options> <filename>"
         echo "  -t WORD    Sets the title to WORD"
         echo "  -a WORD    Sets the album to WORD"
         echo "  -r WORD    Sets the artist to WORD"
         echo "  -y WORD    Sets the date to WORD"
         echo "  -g WORD    Sets the genre to WORD"
         echo "  -c WORD    Sets the track number to WORD";;
    esac
  done
  shift $((OPTIND - 1))
  for file
  do
    if [ -e "$file" ]
    then
      for var in "$TITLE" "$ALBUM" "$ARTIST" "$DATE" "$GENRE" "$TRACKNUMBER"
      do
        if [ "$var" ]
        then
          metaflac "$var" "$file"
        fi
      done
    else
      echo >&2 "No such file: "$file""
      exit 1
    fi
  done
else
  echo >&2 "Type "$(basename "$0")" -h for help."
  exit 1
fi
Lastly, a script that converts vorbis comments to id3 tags.
#!/bin/sh
#
# vcid3 - convert vorbis comments to id3 tags
#
if [ -e "$1" ]
then
  if [ -e "$2" ]
  then
    TITLE="$(metaflac --show-vc-field=TITLE "$1" |
      sed 's/TITLE=\(.*\)/\1/')"
    ARTIST="$(metaflac --show-vc-field=ARTIST "$1" |
      sed 's/ARTIST=\(.*\)/\1/')"
    ALBUM="$(metaflac --show-vc-field=ALBUM "$1" |
      sed 's/ALBUM=\(.*\)/\1/')"
    TRACK="$(metaflac --show-vc-field=TRACKNUMBER "$1" |
      sed 's/TRACKNUMBER=\(.*\)/\1/')"
    YEAR="$(metaflac --show-vc-field=DATE "$1" |
      sed 's/DATE=\(.*\)/\1/')"
    GENRE="$(metaflac --show-vc-field=GENRE "$1" |
      sed 's/GENRE=\(.*\)/\1/')"
    if [ "$GENRE" ]
    then
      id3tool --set-genre-word="$GENRE" "$2"
    fi
    id3tool \
      --set-title="$TITLE" \
      --set-artist="$ARTIST" \
      --set-album="$ALBUM" \
      --set-track="$TRACK" \
      --set-year="$YEAR" \
      "$2"
  else
    echo >&2 "No such file: "$2""
    echo >&2 "Usage: "$(basename "$0")" FLACFILE MP3FILE"
    exit 1
  fi
else
  echo >&2 "No such file: "$1""
  echo >&2 "Usage: "$(basename "$0")" FLACFILE MP3FILE"
  exit 1
fi
Hope you find these useful. Do with them what you wish...
    •    
  • Currently 2.60 / 5
  You rated: 4 / 5 (5 votes cast)
 
[27,323 views]  

A suite of shell scripts for FLACs and MP3s | 8 comments | Create New Account
Click here to return to the 'A suite of shell scripts for FLACs and MP3s' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A suite of shell scripts for FLACs and MP3s
Authored by: Myrddin on Mar 30, '04 07:39:15PM

Thanks ChrisR, grass roots is always the way to go.



[ Reply to This | # ]
A suite of shell scripts for FLACs and MP3s
Authored by: koncept on Mar 31, '04 11:26:05AM

cmd=`basename $0` # works but runs an external command
cmd=${0##*/}  # same thing, but runs within the shell, and faster

Or the file extension of a file:
fileExt=${file##*.}



[ Reply to This | # ]
flacit and flacmp3 v1.1
Authored by: ChrisR on Apr 04, '04 03:37:02AM

I made changes to two of the scripts that I feel are important enough to post here. Below you will find two .diff files. These are used to apply changes to text files and they will save you a lot of time picking through the code. To make the changes to your file simply copy this text into files named "flacit.diff" and "flacmp3.diff" and put them in the directory where the original scripts are stored. Then, in the terminal window, type:


patch flacit flacit.diff
patch flacmp3 flacmp3.diff

Here's the one for flacit. It adds an option to the flac command-line that supresses a warning about the files on the CD not being proper .aiff files.


11a12
>         --force-raw-format \
29a31
>         --force-raw-format \

The changes to flacmp3 are a little more substantial. I modified the lame command-line so that it now does the vorbis comment-to-id3 tag translation in parallel with the actual flac-to-mp3 conversion. This will save you some time on the command line, as now you won't have to run vcid3 after flacmp3 in order to translate the metadata.


3c3
< # flacmp3 - convert a flac file to mp3
---
> # flacmp3 - convert a flac file and its tag data to mp3/id3 format
11c11,25
<       flac -c -d "$file" | lame -h -m s -b 192 - "$(basename "$file" .flac).mp3"
---
>       flac -c -d "$file" |
>         lame -h -m s -b 192 \
>           --tt "$(metaflac --show-vc-field=TITLE "$file" |
>             sed 's/TITLE=\(.*\)/\1/')" \
>           --ta "$(metaflac --show-vc-field=ARTIST "$file" |
>             sed 's/ARTIST=\(.*\)/\1/')" \
>           --tl "$(metaflac --show-vc-field=ALBUM "$file" |
>             sed 's/ALBUM=\(.*\)/\1/')" \
>           --ty "$(metaflac --show-vc-field=DATE "$file" |
>             sed 's/DATE=\(.*\)/\1/')" \
>           --tn "$(metaflac --show-vc-field=TRACKNUMBER "$file" |
>             sed 's/TRACKNUMBER=\(.*\)/\1/')" \
>           --tg "$(metaflac --show-vc-field=GENRE "$file" |
>             sed 's/GENRE=\(.*\)/\1/')" \
>           - "$(basename "$file" .flac).mp3"
13,14c27
<       echo >&2 "No such file: "$1""
<       exit 1
---
>       echo >&2 "No such file: "$file""
18c31
<   echo >&2 "Usage: "$(basename "$0")" INPUTFILE [...]"
---
>   echo >&2 "Usage: "$(basename "$0")" FLACFILE [...]"

Again, I hope you enjoy the scripts. Feel free to email me with any questions.

ChrisR

[ Reply to This | # ]

A suite of shell scripts for FLACs and MP3s
Authored by: MonsterCow on Jun 20, '04 04:26:51PM

I got the id3hack script a few months ago, and it worked great! However, when I try it again now it does not work like it should. Instead of giving correct ID3 tags it gives a track name that is a part of the file path to the mp3 (for example "ers/Shared/Shakta/01 The Futur") and it does not give a track number at all.
Another weird thing is that if I copy the code from this site with IE or Mozilla, the script returns an error ("#: bad interpreter: No such file or directory") and if I copy past the script from Safari it has the naming problem but does not return an error.
Maybe it's a good idea to keep the scripts in a downloadable zip file if this is a common problem?
Anyone got any idea what could be wrong?



[ Reply to This | # ]
A suite of shell scripts for FLACs and MP3s
Authored by: ChrisR on Jun 27, '04 01:11:46AM
MonsterCow,

Converting each occurence of

echo "$file"
to
basename "$file"
in id3hack and vchack should fix your main problem. Thanks for pointing it out.

Chris

[ Reply to This | # ]

A suite of shell scripts for FLACs and MP3s
Authored by: mfc2mfc2 on Sep 05, '04 05:06:22PM

I tried out your CD to FLAC script and it worked very well (OS 10.3). Thank you. However, I am not sure why you are using skip=20 in your script, so I removed it. I also added in a -V switch so that each file is verified.



[ Reply to This | # ]
A suite of shell scripts for FLACs and MP3s
Authored by: ChrisR on Sep 08, '04 08:05:41AM

Thanks, mfc2mfc2, I'm glad you found them useful. The "verify" switch is a good idea -- modify away! As for the "skip" switch, I was getting a pop in the beginning of my mp3s when I transcoded them and I discovered that FLAC was including some sort of header space that most encoders ignore. After some md5 checksum testing, I found that a FLAC file with the first 20 blocks chopped off, once decompressed, was identical to a WAV file ripped in iTunes. Feel free to get rid of that option, but I needed it to make authentic copies.



[ Reply to This | # ]
Enhancements
Authored by: rastan on Sep 13, '04 08:43:31PM

Greetings,

I love your flac to MP3 shell script. It gave me a good start for what I wanted. Thanks! I'm on Linux and it works just fine.

I'll post a diff here with some additional features, such as:

-case insensitivity for the id3tag encoding (mine barfed on some tags which were put in as genre= instead of GENRE=)
-the ability to pass a directory and it will convert all files in that directory, not converting files if they already exist (to speed up batch processing)
-a recursion switch to allow it to delve into directories for easy processing

Once again, many thanks for the inspirational start!



[ Reply to This | # ]