[Python] Google TTS Long Audio

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I newbie working with Google TTS, I have code that allows me to use the long audio TTS and get the file from a bucket, however, when I am trying to do it with a long file that may give me 20 minutes of audio, is not working, and creates a file that is a part of the text and aproximaly 1 minute long, here is my code:

    import os
    from google.cloud import texttospeech

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'mensajereino-google-cloud-ownerkey.json'


    def synthesize_long_audio(project_id: str, output_gcs_uri: str) -> None:
    """
    Synthesizes long input, writing the resulting audio to `output_gcs_uri`.

    Args:
    project_id: ID or number of the Google Cloud project you want to use.
    output_gcs_uri: Specifies a Cloud Storage URI for the synthesis results.
    Must be specified in the format:
    ``gs://bucket_name/object_name``, and the bucket must
    already exist.
    """

    client = texttospeech.TextToSpeechLongAudioSynthesizeClient()


    with open("01-fe-en-la-tormenta.txt", "r", encoding="utf-8") as file:
    text_block = file.read()

    synthesis_input = texttospeech.SynthesisInput(text=text_block)

    voice = texttospeech.VoiceSelectionParams(
    language_code="es-US", name="es-US-Chirp3-HD-Algieba"
    )

    audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.LINEAR16,
    effects_profile_id=['small-bluetooth-speaker-class-device'],
    speaking_rate=0.95,
    )

    parent = f"projects/mensajereino/locations/us-central1"

    request = texttospeech.SynthesizeLongAudioRequest(
    parent=parent,
    input=synthesis_input,
    audio_config=audio_config,
    voice=voice,
    output_gcs_uri= "gs://tts-audio-largo/long-audio3",
    )

    operation = client.synthesize_long_audio(request=request)
    # Set a deadline for your LRO to finish. 300 seconds is reasonable, but can be adjusted depending on the length of the input.
    # If the operation times out, that likely means there was an error. In that case, inspect the error, and try again.
    result = operation.result(timeout=1200)
    print(
    "\nAudio Processed.",
    result,
    )


    I tried Asincronuous and the same, it creates a file 3.5 MB aprox and nothing else happend even if I wait for hours, at this point it will be faster for me to do chunks my self and I bet I do it more affecient, but that does not make sense at all, I almost certain that I doing something wrong or missing something.

    If someone can help, I appreciate it a lot.

    I have tried asincronuous, and one single call, both work fine if the text is short, if it is a long text it creates a file that is 1 minutes aprox and 3.5MB only.

    Continue reading...

Compartilhe esta Página