bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
717 stars 111 forks source link

Capture libmpg123/libsndfile warnings #435

Open leoauri opened 5 months ago

leoauri commented 5 months ago

Hi there, in #421 suppressing warnings from underlying libraries is discussed.

In my case I would like to capture the errors, in order to discard mp3s which trigger warnings.

I tried something like:

import librosa
import io
from contextlib import redirect_stderr
f = io.StringIO()
with redirect_stderr(f):
    librosa.load('file.mp3')

But the errors go straight to stderr and are not captured by the context manager. Presumably something going on in a subprocess or something.

Any ideas on picking up libmpg123 warnings?

bastibe commented 5 months ago

Presumably, libsndfile is writing straight to STDERR. It seems a bit odd that redirect_stderr doesn't catch that, but perhaps it only operates on the python level?

jkyl commented 4 days ago

I tried redirecting the Warning: Xing stream size off by more than 1% messages using this method, which works for e.g. Blender and TensorFlow C-level logs, but it did not work in this case.

This may be because I am calling sf.read in subprocesses (with torch's DataLoader)... I will try confining it to the main process.

update: it fails to capture the output in that case, too.