4

I'm working on a project where I need to post-process a bunch of audio files in various formats.

  • Firstly, the files need to be converted to .WAV format.
  • Secondly, depending on their length, I need to insert a short audible watermark at certain intervals in each of the new .WAV files.

The first part is easy, using the LAME encoder cli. The second part is where it get's difficult - I've tried a few method with both LAME and FFmpeg, but can't seem get it working.

The script is running as a cron job in the background, so full cli access is available.

If possible, it would be great if someone could point me to an example script/gem or class that does this in some related way.

hakre
  • 193,403
  • 52
  • 435
  • 836
ghstcode
  • 2,902
  • 1
  • 20
  • 30
  • Is this an audible watermark, or something that should generally go undetected? – Brad Oct 07 '11 at 15:52
  • you say the script is running and there is really no question here. all three languages support shelling/system calls. What is your question? – horatio Oct 07 '11 at 15:57
  • @Brad , yes it's an audible watermark file. When you play the generated mp3, you should be able to hear it every few seconds. – ghstcode Oct 07 '11 at 17:34
  • Why do you want the watermark to be audible? Also, are you trying to encode information? – Nick ODell Oct 07 '11 at 17:47
  • @NickODell It needs to be audible because the sound loops are going to be sold online. So when they get previewed on the site, the person listening will hear the audible watermark - similar to what happens when you preview loops on http://audiojungle.net/ – ghstcode Oct 07 '11 at 17:54

2 Answers2

1

This gets complicated. You need to actually mix the audio, which to my knowledge, isn't possible with FFMPEG. The other problem you're going to have is loss of quality if you take an MP3, convert it to WAV so you can work with it, and re-encode it back to MP3.

I think you can use Sox for this: http://sox.sourceforge.net/

Use FFMPEG first to decode the audio to WAV, adjusting sample rate and bit depth as necessary.

Then, call out to soxmix: http://linux.die.net/man/1/soxmix

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Why use ffmpeg first? SoX can read mp3. If these are audio previews for a product for sale, loss of quality due to transcoding might even be a feature. – Matt K Oct 07 '11 at 19:18
  • see also: http://stackoverflow.com/questions/4588998/mix-audio-tracks-with-offset-in-sox – horatio Oct 07 '11 at 19:24
  • @mkb, I was under the impression that SoX only support a handful of formats, but I see now that my assumption was incorrect: http://sox.sourceforge.net/soxformat.html – Brad Oct 07 '11 at 19:48
  • Thanks @Brad, this led me to the following command: **sox -m -v 0.9 l.wav -v 0.4 2.wav out.wav** This solves my problem :) – ghstcode Oct 07 '11 at 22:23
1

If you're ready to take the Python route, I would suggest SciPy, which can read WAV files into NumPy arrays:

from scipy.io import wavfile
fs, data = wavfile.read(filename)

(the official documentation contains details).

Sounds can be conveniently manipulated through the array manipulation routines of NumPy.

scipy.io.wavfile can then write the file back to the WAV format.

SciPy and NumPy are general scientific data tools. More music-centric Python modules can be found on the official web site.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260