如何使 MP3 解码过程静音

How to silent the MP3 decoding process

我正在学习 ffmpeg 并制作了一个 MP3 解码器,但是当我执行它时,我的终端上打印了某种信息,但我不想要它。那么如何静音呢?

这是代码(完整代码)

/* FFmpeg Usage Example
 * Date : 28 July 2019
 */
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <assert.h>

int decode_packet(AVCodecContext*, AVPacket*, AVFrame*);

int main(void) {
        AVFormatContext *pFormatContext = avformat_alloc_context();
        AVCodecParameters *pCodecParameters = NULL;
        if(avformat_open_input(&pFormatContext,"song.mp3",NULL,NULL)!=0) {
                fprintf(stderr,"Could not open file\n");
                return -1;
        }
        if(avformat_find_stream_info(pFormatContext,NULL)<0) {
                fprintf(stderr,"Could not find stream\n");
                return -1;
        }

        size_t stream_index = 0;
        for(;stream_index<pFormatContext->nb_streams;stream_index++) {
                if(pFormatContext->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
                        pCodecParameters = pFormatContext->streams[stream_index]->codecpar;
                }
                        break;
        }

        if(stream_index == -1) {
                fprintf(stderr,"could not retrive stream info from file\n");
                return -1;
        }
        AVStream *stream = pFormatContext->streams[stream_index];
        pCodecParameters = stream->codecpar;
        AVCodec *cdc = avcodec_find_decoder(pCodecParameters->codec_id);
        AVCodecContext *cdc_ctx = avcodec_alloc_context3(cdc);
        assert(pCodecParameters);

        if(avcodec_parameters_to_context(cdc_ctx,pCodecParameters) < 0) {
                fprintf(stderr,"Can't copy params to codec context\n");
                return -1;
        }

        if(avcodec_open2(cdc_ctx,cdc,NULL) < 0) {
                fprintf(stderr,"Failed to open decoder for stream\n");
                return -1;
        }

        AVFrame *frame = av_frame_alloc();
        if(!frame) {
                fprintf(stderr,"could not allocate memory for frame\n");
                return -1;
        }

        AVPacket *packet = av_packet_alloc();
//      av_init_packet(&packet);
        if(!packet) {
                fprintf(stderr,"could not allocate memory for packet");
                return -1;
        }

        packet->data=NULL;
        packet->size=0;
        // lets read the packets
        while(av_read_frame(pFormatContext,packet) >= 0) {
                if(packet->stream_index==stream_index) {
                        int response = 0 ;
            response = decode_packet(cdc_ctx,packet,frame);

                        if(response < 0)
                                continue;
                }
        av_packet_unref(packet);
        }

        return 0;
}

int decode_packet(AVCodecContext *cdc_ctx , AVPacket *pkt, AVFrame *frm) {
    int response = avcodec_send_packet(cdc_ctx,pkt);
    if(response < 0)
        return response;
    while(response >= 0) {
        response = avcodec_receive_frame(cdc_ctx,frm);
        if(response == AVERROR(EAGAIN) || response == AVERROR_EOF)
            return -1;
        else if(response < 0)
            return response;
    }
    return 0;
}

预期行为:屏幕上不应打印任何内容

实际行为:自动打印某种日志

这是输出日志(其中一些)

[mp3float @ 0x75172e7400] overread, skip -6 enddists: -5 -5         
[mp3float @ 0x75172e7400] overread, skip -7 enddists: -6 -6
[mp3float @ 0x75172e7400] overread, skip -6 enddists: -5 -5
[mp3float @ 0x75172e7400] overread, skip -6 enddists: -4 -4

将日志级别设置为 AV_LOG_QUIET。函数原型在 libavutil/log.h