Script to maintain a transcoded MP3 tree

Introduction

This script (mp3treesync.sh) can be used to keep an MP3 mirror of a non-MP3 source music tree. It is useful, for example, to maintain a portable music player-friendly music tree from your high quality FLAC tree. The mirror process only transcodes files to the destination tree if they're new or they have changed since the last mirroring - ie it will be slow the first time it is run, but should be relatively quick after that. The script depends on a few things to do the heavy lifting:

  • Bash. It's a bash script, which means you'll probably have some difficulty running this on anything other than Linux.
  • mp3fs, a FUSE filesystem that produces a virtual filesystem that transcodes music files to MP3 on-the-fly.
  • FUSE (filesystem in userspace), to prove the framework that mp3fs requires.

Currently only FLAC files will be transcoded to MP3 (a limitation of mp3fs at the time of writing); however, any MP3 files that already exist in the source tree will just be copied to the destination.

Warning

Because this is a mirror script, and it will delete destination files that don't exist in the source tree, it has the potential to do a LOT OF DAMAGE (such as delete your whole disk contents). Therefore it should be used carefully. A --test parameter can be added to allow dry-running of the script to see the changes that would be made prior to running it for real - use of this parameter is HIGHLY RECOMMENDED to prevent unexpected damage to the destination tree.

Usage

Basic usage is as follows:

mp3treesync.sh [--test] SOURCEDIR DESTDIR BITRATE

For example, if your main source music tree is /mnt/media/MusicFLAC and that contains FLAC files (or a mixture of FLAC and MP3 files), then you can create a mirror with the following command:

mp3treesync.sh /mnt/media/MusicFLAC /mnt/media/MusicMP3 128

The first time you run this the destination directory (/mnt/media/MusicMP3 in this example), should exist and should be empty. This will produce a destination directory tree under /mnt/media/MusicMP3 that contains 128kbps MP3 versions of the FLAC source files (note that any source MP3s are simply copied across and are not transcoded - hence they won't be converted to your specified bitrate).

The next time you run the script with the same parameters only the changes will be mirrored - ie any new files will be transcoded to the destination tree, and files removed will also be removed from the destination, and any files that have been modified (eg they're been retagged), will be updated in the destination. This means that it's easy to maintain the destination tree if you regularly schedule the command (such as with cron, or a cron.weekly script on Gentoo).

The script maintains a lock on the destination tree and so it won't allow more than one mirror operation to run simultaneously (useful if the previous mirror hasn't finished by the time the next mirror operation is scheduled).

Download and Installation

You can download an archive containing the script file below. To install it just expand the archive and move the mp3treesync.sh somewhere convenient on your PATH.

AttachmentSize
mp3treesync.tgz2.49 KB