Project:

Convert audio files to MP3 (copyDirToMP3)

 

This is a perl based application developed to run in UNIX that is essentially a wrapper for several audio conversion programs. I wrote it so that I can keep all my bought music and ripped CDs as raw audio files in a secure location, along with associated music art work, in one place. The program (copyDirToMP3) copies the entire directory structure to a new location and converts audio files to a specific MP3 format suitable to our iPods and other MP3 players.

In the process of creating the mp3 files it converts the largest jpeg file in each directory into album artwork so that iTunes and MSoft Media Player will display the art work.

It is written to run as a perl script for UNIX, but could be ported to run on MSWindows.

 

Installation

The following all works on ubuntu 8.10 and should also work on Fedora and other Linux distributions. In May 2011 I reinstalled it on Ubuntu 10.04 and updated the documentation.

  1. Install LAME, this is critical because LAME is used to create the mp3 files, eg
    # sudo apt-get install lame
  2. Install eyeD3, this is used to add the album artwork
    # sudo apt-get install eyeD3
  3. Install ImageMagick, this is used to convert Folder.jpg to 200x200. I found that Windows Media Player and iTunes preferred this size otherwise it would not display the artwork.
    # sudo apt-get install imagemagick
  4. Install FLAC, this is optional, it is needed if you have audio files in the .flac extension, eg
    # sudo apt-get install flac
  5. Install FAAD, this is optional, it is needed if you have audio files in the .m4a (mpeg4) extension, eg
    # sudo apt-get install faad
  6. Install MPLAYER, this is optional, it is needed if you have audio files in the .wma extension, eg
    # sudo apt-get install mplayer
  7. Install imagemagick, it is needed to convert the image files to the correct size (http://www.imagemagick.org).\n"; # sudo apt-get install imagemagick
  8. Install the Perl Module "File::Find", it is used in the main loop to look at each file
    # perl -MCPAN -e 'shell'
    cpan[1]>install File::Find
  9. Install the Perl Module "MIME::Lite", it is used to send an email report (if you have a problem running perl try deleting "rm ~/.cpan/" first
    # perl -MCPAN -e 'shell'
    cpan[1]>install use MIME::Lite
  10. Download copyDirToMP3 and put it in your executable path, eg
    # sudo mv copyDirToMP3 /usr/sbin/copyDirToMP3
  11. Make copyDirToMP3 executable, eg
    # sudo chmod a+x /usr/sbin/copyDirToMP3

Usage

copyDirToMP3 [-f] [-o OWNER] [-g GROUP] [-s SOURE-DIR] [-d DESTINATION-DIR] [-V0 | -V1 | -V2 | -V3 | -V4 | -V5 | -V6 | -V7 | -V8 | -V9 |-b BITRATE] [-S] [-e ] [-a]

-h

See this help

-f

Overwrite all files in the destination [do not overwrite]

-o <USER>

Change the owner of the files/directories to USER, default is the owner's profile.

-g <GROUP>

Change the group of the files/directories to GROUP, [www-data]

-s <DIR>

The root directory to find all the source audio files, the default is ./music-raw

-d <DIR>

The destination root directory to put the converted audio files, the default is ./music-mp3

-b <BITRATE>

This is a LAME parameter, it selects constant bit rate encoding at the bit rate do you want the mp3, [VBR is the default not CBR].

-Vn

where 0<= n <= 9. This is a LAME parameter, is selects variable bit rate and selects the quality. V0 is the best quality and generates about 256kbps, V3 is around 175kbps. [The default is V0].

-S

Run silently, otherwise advise action on each directory and file, default is not to run silent.

-a

Just update the graphic files attached to the mp3 files.

-e

Send an email report, or errors to the provided email address.

-smtp

Send an email report, or errors to the provided email address.

-nice>

Run the process so they do not hog the CPU.

Usage example 1


sudo copyDirToMP3 -o www-data -g www-data -s /multimedia/music-raw -d /multimedia/music-mp3

The above does the following:
- looks in /multimedia/music-raw
- Creates the same directory structure starting at /multimedia/music-mp3 using a owner of www-data and a group called www-data
- Copies all the files to the new location.
- Converts any audio files to mp3 VBR V0 (maximum quality/size).
- If there isn't a Folder.jpg it will create a 200x200 pixel image from the largest available .jpg or .jpeg file so that MSWindows Explorer, MSoft Media Player, and iTunes (and thus iPOD) will display the album art work.
- Outputs what it does to each file as it goes.

Usage example 2

This is how I use the application.

  1. I create a samba share as \data\music-raw writeabe only by a specific user, eg a user called musicAdmin.

    I create another samba share \data\music-mp3 which is writeable only by a specific user, eg again musicAdmin, and readable by everyone.

  2. I use MSoft's Windows Media Player to rip my CDs and create the directories. I like MSoft's Windows Media Player because it appears to create clean directory structure and provides the album artwork for free. Also very importantly I get MSoft's Windows Media Player to rip the audio files as raw wav files so there is no losses. I use the same specific user musicMaker.

    Specific setup of MSoft's Windows Media Player are:
    Tools : Options : Rip Music
    Rip Music to this location: \data\music-raw
    Format: WAV (Lossless)

    Tools : Options : Library
    Enabled Rearrange music in rip music folder

  3. I can add music manually by creating a directory using the artists name, then a sub directory using the name of the album.

  4. I set up a cron job for root to run once a day that looks at /data/music-raw and copies the entire directory structure and files to /data/music-mp3 converting any audio files to mp3 format., here is the crontab entry I use:

    sudo crontab -e
    01 02 * * * copyDirToMP3 -o www-data -g www-data -s /data/music-raw -d /data/music-mp3 -S


    The above crontab entry does the following:
    - looks in /data/music-raw
    - Creates the same directory structure starting at /data/music-mp3 using a owner of www-data and a group called www-data
    - Does it all silently

    If I want to create the mp3 files manually I would run:

    sudo copyDirToMP3 -o www-data -g www-data -s /data/music-raw -d /data/music-mp3


Versions

Version and date Comment
1.5
May 2016
download

- Added -nice switch so user can tell rocess to not hoge the CPU [defult is nice is not used].
- Centralised escaping strange characters to a subroutine.

1.4
May 2016
download

- Change default so that if Owner (-o) is not provided it does not try to change it.
- same for Group (-g), if Group not specified it is left alone.
- User can now also specify permissions, if not provided then permissions are not changed.

1.3
May 2016
download

- Improved debug output a little bit.

1.2
Feb 2013
download

- Use avconv for m4a files to convert to flac before converting to mp3.

1.1
May 2011
download

- Resolved a bug if the user does not specify the lame parameter.
- updated the documentation.
- added sending reports via email using local Sendmail, or be specifying an SMTP host.
- Resolved a bug when attaching art to mp3 files if filename has a ` in it.

0.3
Jun 2009
download

- Uses the largest jpeg in the directory to create a 200x200 jpeg and call it Folder.jpg.
- Use Folder.jpg as the artwork to add to every mp3 in the directory as an ID3 v2.3 Tag.
- User can select the LAME parameters CBR as well as VBR mp3 encoding.
- added a switch -a to redo all the artwork, ie use Folder.jpg and add it to all the mp3s.
Tested with Windows Media Player ver 11 and iTunes.

0.2
Jun 2009
download

This version included mplayer to decode .wma formatted audio files.

0.1
May 2009
download

Can convert .wav, .mp3, and .flac, to mp3. It also copies all the other files over to the specified destination and recreate the directory structure. If Folder.jpg does not exist it will create one by copying from an available .jpg file.

License

copyAudioDirToMP3 is free software and distributed under the GNU General Public License (GPL) Version 3.

copyDirToMP3 uses the following applications:

- LAME is a high quality MPEG Audio Layer III (MP3) encoder licensed under the GNU GPL. Specifically to convert audio files to mp3 using:

lame -S -h --ta $artistName--tl $albumName --tt $srcFilePrefix -b $bitRate $srcFile $dstFile

Where:
 
-S run silent
-h highest quality
-b bit rate
--ta $artistName inbends the artists name in the mp3 file, we get it from the parent directory name, which is how MSoft Media Player organises the directories
--tl $albumName inbends the name of the album in the mp3 file, we get it we get from the directory name, which is how MSoft Media Player organises the directories
--tt $srcFilename inbends the name of the song in the mp3 file, we get it we get from the filename


- FLAC is Free Lossless Audio Codec and is a common audio format, and frac utility is licensed under the GNU GPL. We use frac to convert the audio file to a WAV format before getting lame to convert to mp3.

flac -d -f -s -o $dstFile $srcFile;
Where:
-d decode the flac file to wave
-f overwrite destination file
-s run silently
-o filename Write output to file

- imageMagick is really a software suite to create and edit image files. It is used to resize the album artwork to 200x200 jpeg that MSWindows Media Player and iTunes want. It is license is compatible with GNU.

convert -resize 200x200! Folder.jpg
Where:
-resize 200x200! Forces a resizing of the image to exactly 200x200 pixels

- MPLAYER is really a movie player that has a good command line, but I am using it here to decode .wma formatted audio to wav. It is licensed under GNU Version 2. To decode .wma I also had to install the w32codecs.

mplayer -nolirc -really-quiet -vo null -vc null -af resample=44100 -ao pcm:waveheader:file=$dstFile $srcFile
Where:
-nolirc Turns off the LIRC support for remote control
-really-quiet run silently
-vo null No video
-vc null No video codec
AF resample=44100 Set audio filter to typical CD
AO pcm:waveheader:file:$dstFile Output to PCM with waveheader and write to a file

 

- eyeD3 is used to edit the ID3 tags in the mp3 files. It is used to convert the IDE to version 2.3 and to add the album artwork to each mp3 file.

eyeD3 --to-v2.3 --add-image=Folder.jpg:FRONT_COVER $srcFile
Where:
--to-v2.3 Convert the IDE tags in the mp3 file to version 2.3.
--add-image=Folder.jpg:FRONT_COVER Use Folder.jpg as the album artwork and add it as an IDE to the mp3 file.
$srcFile Is the filename of the mp3 file.

 

Author and copyright

Stephen.Kingham <at> kingtech.com.au

Comments and suggests are welcome