http://www.perlmonks.org?node_id=1131831

Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

When parsing an mp3 file using Audio::TagLib, quite often I see these messages:
TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate. TagLib: MPEG::Header::parse() -- Invalid sample rate.
Sometimes a few lines, sometimes a lot. I understand the nature of the problem and it's safe to ignore. So is there anyway to silence these messages (and only these messages) without redirecting all output to /dev/null (or something like that)?

Thanks!

Replies are listed 'Best First'.
Re: TagLib messages
by stevieb (Canon) on Jun 24, 2015 at 17:12 UTC

    One way is to put the specific code that is generating the warning into a block that redirects STDOUT. You'll want to ensure you limit the scope, because if any other warnings are generated, you won't see them.

    #!/usr/bin/perl use warnings; use strict; sub warning { print "ERROR!!!\n"; } my $out; open my $stdout, '>', \$out or die $!; { local *STDOUT = $stdout; warning(); }

    -stevieb

      Perhaps a patch to the maintainer providing a method to categorize the warnings so you can silence the warnings of only the type you want?

      Jason L. Froebe

      Tech Blog