Making music previews

This site uses a lot of music 'extracts' or previews to make sense of its contents. Naturally, they come from assorted commercial audio recordings -and therefore might trigger copyright claims. To avoid that, and to be able to put up a 'fair use' defence should one be made, it's important that the previews are algorithmically produced: that is, they should be short previews of modest audio quality, no exceptions. The moment I start letting a preview go on a bit, in order to capture this or that lovely moment, I run the risk of breaching fair use guidelines. Similarly, if I up the audio quality to be lossless flac instead of 128kbps MP3, the record companies would be justified in going for my jugular!

In short: I need to produce all my music previews in an automated fashion that is consistent and predictable. In other words, it needs to be scripted! And here's the script I use:

find . -name "*.flac" -print0 | while read -d $'
find . -name "*.flac" -print0 | while read -d $'\0' file; do
filename=$(basename "${file/.flac}");
directory=$(dirname "$file");
ffmpeg -t 40 -i "$file" -af "afade='t='out:st='35:d='5" -codec:a libmp3lame -b:a 128k "$directory/$filename-preview.mp3" < /dev/null ;
rm -f "$file";
' file; do filename=$(basename "${file/.flac}"); directory=$(dirname "$file"); ffmpeg -t 40 -i "$file" -af "afade='t='out:st='35:d='5" -codec:a libmp3lame -b:a 128k "$directory/$filename-preview.mp3" < /dev/null ; rm -f "$file"; done

It runs on Linux, obviously. It's run in the parent directory of whatever set of folders contains the full-length FLAC tracks I'm hoping to convert to audio previews.

The critical line is the one starting "ffmpeg...": that uses the ffmpeg utility to extract a 40-second audio sample from the full-length FLAC original, starting from the very beginning of the file. The "afade=t=out..." switch in the middle of the ffmpeg command ensures that there is a 5 second gradual fade-out of the audio, starting from the 35th second. That just avoids the music preview stopping abruptly when its allotted 40 seconds is up.

For some audio tracks, which take a while to get going (I'm looking at you, Bach!), I alter that to read 90 seconds with a fade out starting at 80 seconds and lasting for 10 seconds. That line would thus become:

  ffmpeg -t 90 -i "$file" -af "afade='t='out:st='80:d='10" -codec:a libmp3lame -b:a 128k "$directory/$filename-preview.mp3" < /dev/null ;

So I can vary the duration of my clips a bit, but they are always output as MP3 tracks at 128kbps, which is about the minimum MP3 sample rate you can use for music tracks -so the quality of the preview is guaranteed to be as low as it can reasonably be and in no way a decent substitute for the lossless original.

Finally, whatever clip durations I might use, the script names the new MP3 file to be the same as the original, but with a "-preview.mp3" extension... and for good measure, it deletes the original FLAC. (So this isn't a script you want to run against your master music library!)

The script thus guarantees musical samples which are of fixed length, low quality and with a nice fade-out -all conditions which, I hope, will keep the copyright guys happy!