Search code examples
python-3.xaudiogstreamervorbislamemp3

How to change the bitrate of audio encoding in Gstreamer in the PLAYING state?


  • I want to convert a FLAC file to a MP3 (and Vorbis, in a second time) file.

  • These MP3/Vorbis streams are then transmitted, raw, to a second device that decodes them.

  • Depending on the quality of the transmission, I want to be able to change the bitrate on-the-fly.

  • The change must be gapless (hence the "in the PLAYING state" in the title).

  • The specific encoders are lamemp3enc and vorbisenc (and cannot be changed).


To my knowledge, changing the bitrate while playing is actually not possible with these codecs.

But I guess there are clean and simple ways to change the bitrate without introducing any gap in the stream: I'd like to learn about any of them.

(NB: I did write any, not all, I am not asking for the "best" way, I am not asking for a review, I just want something that works.)


Solution

  • Read through this ..

    You will:

    • block element before lamemp3enc
    • flush the encoded frames into queue with EOS sent to lame and discard the EOS when it comes out of lame
    • then set the lamemp3enc into NULL state
    • change the parameters
    • set lame to PLAYING or PAUSED - this will preroll it again with new data using new bitrate
    • check when lame is in playing and then you know everything is working
    • there should be no gap as queue has lot of old buffers which it sends forward during you are doing the witch

    You can inspire yourself with the example from the link above.. However you are not doing any removing and adding new elements.. Do not forget to set it to NULL state as it will discard all internal states (well hopefully if its not buggy). Then You would just change the parameters with g_object_set...

    Also I never did this so you may also ask on IRC of #gstreamer at freenode if you are stuck or not sure.

    HTH