8000 TTS API method "/api/tts_get_url" - incorrect handing of additional options - s16 is not supported by the libmp3lame encoder · Issue #136466 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content
TTS API method "/api/tts_get_url" - incorrect handing of additional options - s16 is not supported by the libmp3lame encoder #136466
@sstefanowski

Description

@sstefanowski

The problem

I need to generate MP3 with bitrate exactly 48kbps. For this purpose, I'm trying to use TTS API method "/api/tts_get_url" with additional options

NOTE WHY: The 48kbps (exactly!) is THE ONLY format that can be played on Alexa Media Player by putting url in "<audio src=..."> tag. This is a workaround forcing Alexa to play announcements in non-Alexa-friendly language.

I have noticed that "/api/tts_get_url" request can be extended with options forcing MP3 conversions:

"preferred_format"
"preferred_sample_rate"
"preferred_sample_channels"
"preferred_sample_bytes"

BUG:
I'm sending TTS API request with data containing

{
   'engine_id': 'microsoft', 
   'message': 'Any message', 
   'options': {'preferred_sample_rate': '16000', 'preferred_sample_channels': 2, 'preferred_sample_bytes': 2}
}

the TTS code converts preferred_sample_bytes option to ffmpeg parameter using this code

if to_sample_bytes == 2:
    # 16-bit samples
    command.extend(["-sample_fmt", "s16"])

and then the exception is raised:

Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> mp3 (libmp3lame))
[libmp3lame @ 0x7f96dc9d1000] Specified sample format s16 is not supported by the libmp3lame encoder
[libmp3lame @ 0x7f96dc9d1000] Supported sample formats:
[libmp3lame @ 0x7f96dc9d1000]   s32p
[libmp3lame @ 0x7f96dc9d1000]   fltp
[libmp3lame @ 0x7f96dc9d1000]   s16p

EXTENSION (PLEASE):
Using current set of options I'm still unable to convert MP3 to bitrate exactly 48kbps. It looks like ffmpeg requires exact audio bitrate parameter -b:a
So... Could you add addional option of TTS API
preferred_bitrate
so it could send request like

{
   'engine_id': 'microsoft', 
   'message': 'Any message', 
   'options': {'preferred_sample_rate': '16000', 'preferred_sample_channels': 2, 'preferred_bitrate': '48k'}
}

to enforce ffmpeg encoder to use desired bitrate on output

if to_bitrate is not None:
    command.extend(["-b:a", str(to_bitrate)])

The ffmpeg command I'm expecting to get (tested that produces MP3 acceptable by Alexa Media Player) is something like:
ffmpeg -y -i {input} -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 -f mp3 {output.mp3}

What version of Home Assistant Core has the issue?

core-2025.1.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

TTS

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tts/

Diagnostics information

Example YAML snippet

Anything in the logs that might be useful for us?

Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> mp3 (libmp3lame))
[libmp3lame @ 0x7f96dc9d1000] Specified sample format s16 is not supported by the libmp3lame encoder
[libmp3lame @ 0x7f96dc9d1000] Supported sample formats:
[libmp3lame @ 0x7f96dc9d1000]   s32p
[libmp3lame @ 0x7f96dc9d1000]   fltp
[libmp3lame @ 0x7f96dc9d1000]   s16p

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      < 2A2B /ul>
    0