Watermarking mp3s as stream

56 views
Skip to first unread message

Stefan Westerfeld

unread,
Apr 8, 2022, 12:34:39 PM4/8/22
to audio...@googlegroups.com
Hi!

I'd like to answer to a question on github (issue #13), which is not
really a bug report, but more a question on how to do mp3 from stdin
properly, so I think it could be useful to have the reply on the list /
list archive.

### So the first question is: audiowmark doesn't support mp3 as input on
stdin. Is there a way to watermark mp3s as a stream? Can it be done
using raw streams?

Indeed, there are two ways, by using an mp3 decoder like madplay.

(1) the elegant way: using wav format as stdin

If we use madplay to decode the mp3, it writes its output in wav format,
and since audiowmark supports wav input, the elegant solution is this:

$ madplay -o wave:- in.mp3 | audiowmark add - out.wav f0

Using stdin:

$ cat in.mp3 | madplay -o wave:- - | audiowmark add - out.wav f0

The only downside here is a (mostly harmless) message

error: input frames (1073741823) != output frames (8524800)

which is because madplay doesn't know the number of samples it will
produce when decoding starts.

(2) the ugly way: using raw streams

$ madplay -o raw:- in.mp3 | audiowmark add --input-format raw --raw-rate
44100 - out.wav f0

You also can use stdin for madplay, like this:

$ cat in.mp3 | madplay -o raw:- - | audiowmark add --input-format raw
--raw-rate 44100 - out.wav f0

When using raw streams, the tricky part is getting the parameters right.
The mp3 file contains the number of channels and the sample rate, so it
would be necessary to analyze the mp3 and set the proper parameters for
audiowmark.

The bit depth can be configured for both, madplay and audiowmark, so
these must match. In my example everything is fine because both default
to 16 bit for raw mode.

The endianness for madplay is host order, for audiowmark the default is
little endian, so the above only works on little endian systems, for big
endian systems one would need to use --raw-endian big

### Second question: can we produce mp3 again?

By default audiowmark always produces uncompressed wav output. If you
want to produce mp3 again, you need an mp3 encoder like lame, for
example like this:

$ madplay -o wave:- in.mp3 | audiowmark add - - f0 | lame - > out.mp3

### Limitations

Both methods don't preserve tags. I think in the end it might be worth
considering adding native mp3 support of some sort to audiowmark. I also
didn't look at madplay's ReplayGain support here.

Cu... Stefan
--
Stefan Westerfeld, http://space.twc.de/~stefan
Reply all
Reply to author
Forward
0 new messages