libmp3lame忽略ffmpeg选项,并且不能更改音频比特率

时间:2018-08-27 09:03:06

标签: ffmpeg audio-streaming bitrate lamemp3

我有一个Java应用程序对音频进行编码,然后通过套接字将其发送到服务器,我使用ffmpeg获取压缩的avpacket,然后通过套接字发送此数据包,问题是带宽大于所需的带宽,并且无法控制它,我尝试将比特率选项设置为编码器(libmp3lame),但它会完全忽略它,而且我不知道这是代码的问题,还是因为我没有使用ffmpeg libformat将数据包发送到套接字?

这是我打开编解码器的方式

       codec = avcodec_find_encoder_by_name("libmp3lame");
        //codec = avcodec_find_encoder(AV_CODEC_ID_MP3);

        if(codec == null)
            throw new Exception("Codec not found");

        context = avcodec_alloc_context3(codec);

        if(context == null)
            throw new Exception("Could not allocate audio codec context");

        context.codec_tag(0);
        context.codec_id(AV_CODEC_ID_MP3);
        context.codec_type(AVMEDIA_TYPE_AUDIO);

        // set sample parameter
        context.bit_rate(audio_bitrate);
        context.sample_rate(samples_rate);
        context.channels(samples_channels);
        context.channel_layout(av_get_default_channel_layout(samples_channels));
        context.time_base().num(1).den(samples_rate);

        context.sample_fmt(AV_SAMPLE_FMT_S16);
        context.bits_per_raw_sample(16);        

        context.flags(context.flags() | AV_CODEC_FLAG_QSCALE);
        context.global_quality((int)Math.round(FF_QP2LAMBDA * audioQuality));

        if ((codec.capabilities() & AV_CODEC_CAP_EXPERIMENTAL) != 0) {
            context.strict_std_compliance(AVCodecContext.FF_COMPLIANCE_EXPERIMENTAL);
        }

        // priv_data()
        av_opt_set(context.priv_data() , "crf" , audioQuality + "" , 0);
        av_opt_set(context.priv_data() , "b" , "85" , 0);
        //av_opt_set(context.priv_data() , "V" , "2" , 0);
        //av_opt_set(context.priv_data() , "lowpass" , "120" , 0);
        av_opt_set(context.priv_data() , "abr" , "1" , 0);

        AVDictionary options = new AVDictionary(null);
        //av_dict_set(options, "crf", ""+audioQuality , 0);
        av_dict_set(options, "b:a", "8k" , 0);
        av_dict_set(options , "q:a" , "8" , 0);
        //av_dict_set(options , "lowpass" , "19.5" , 0);
        //av_dict_set(options, "b" , "8K" , 0);


        // open codec
        if (avcodec_open2(context, codec, (PointerPointer)null ) < 0) 
            throw new Exception("Could not open codec");
....

0 个答案:

没有答案