SomGG
SomGG

Reputation: 1

How to manage volume using FFMPEG

Is there any way to set/modify/change/update volume of audio when it is playing using FFMPEG library. Thanks in advance.

Upvotes: 0

Views: 2755

Answers (2)

SomGG
SomGG

Reputation: 1

My code to set new volume for decoded buffer is - 



  void FFMpegPlayer::UpdateVolumeForFrames(uint8_t* pStartFrame, uint8_t* pEndFrame, const float kfVolume)
    {
        if(kfVolume <= 0.0f)
        {
        memset(pStartFrame, 0, pEndFrame - pStartFrame) ;
        }

        int16_t* pStart = (int16_t*)pStartFrame, *pEnd = (int16_t*)pEndFrame;
        int32_t value = SHRT_MAX;
        while(pStart <= pEnd)
        {
        value = *pStart * kfVolume;
        if(value > SHRT_MAX)
        {
        value = SHRT_MAX;
        }

        if(value < SHRT_MIN)
        {
        value = SHRT_MIN;
        }
        *pStart = value;
        ++pStart;
        }
    }

Code to setup volume filter-
ret = avfilter_graph_create_filter(&fil_volume,
                                           avfilter_get_by_name("volume"), "volume",
                                           NULL, NULL, pVideoState->pAudioGraph);
        if (ret < 0)
            goto end;
        pVideoState->fCurrentVolume = 0.5f ;
        // Setting default volume value
        {
            AVDictionary* options_dict = nullptr;
            av_dict_set(&options_dict, "volume", AV_STRINGIFY(0.5f), 0);
            ret = avfilter_init_dict(fil_volume, &options_dict);
            av_dict_free(&options_dict);
            void *vol = (fil_volume->priv);
        }

Upvotes: 0

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11174

You can use ffmpeg's volume filter. You'll probably want to search around to figure out how to get ffmpeg filters working in your application, but it's generally worth the effort.

You can also do it manually by multiplying the PCM data in the decoded audio data (AVFrame->data[0]) by your volume multiplier (>1.0 to increase volume, <1.0 to decrease volume), but then you'd need to know the format of your decoded data.

Upvotes: 4

Related Questions