[Python] How to get fine grained smaller time resolution srt from google cloud speech to text...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I have a working code in python which generates an srt file using google cloud speech to text.

    from google.api_core.client_options import ClientOptions
    from google.cloud.speech_v2 import SpeechClient
    from google.cloud.speech_v2.types import cloud_speech
    import json
    from google.protobuf.json_format import MessageToDict

    MAX_AUDIO_LENGTH_SECS = 8 * 60 * 60


    def run_batch_recognize():
    # Instantiates a client.
    client = SpeechClient(
    client_options=ClientOptions(
    api_endpoint="us-central1-speech.googleapis.com",
    ),
    )

    # The name of the audio file to transcribe:
    audio_gcs_uri = "<redacted>"

    config = cloud_speech.RecognitionConfig(
    explicit_decoding_config=cloud_speech.ExplicitDecodingConfig(
    encoding=cloud_speech.ExplicitDecodingConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=24000,
    audio_channel_count=1,
    ),
    features=cloud_speech.RecognitionFeatures(
    enable_word_confidence=True,
    enable_word_time_offsets=True,
    enable_automatic_punctuation=True,
    max_alternatives=5,
    ),
    # model="chirp_2",
    model="short",
    language_codes=["en-US"],
    )

    output_config = cloud_speech.RecognitionOutputConfig(
    inline_response_config=cloud_speech.InlineOutputConfig(),
    output_format_config=cloud_speech.OutputFormatConfig(
    srt=cloud_speech.SrtOutputFileFormatConfig()
    ),
    )

    files = [cloud_speech.BatchRecognizeFileMetadata(uri=audio_gcs_uri)]

    request = cloud_speech.BatchRecognizeRequest(
    recognizer="<redacted>",
    config=config,
    files=files,
    recognition_output_config=output_config,
    )
    operation = client.batch_recognize(request=request)

    print("Waiting for operation to complete...")
    response = operation.result(timeout=3 * MAX_AUDIO_LENGTH_SECS)
    # print(response)

    # Convert the protobuf response to a dictionary using MessageToDict
    response_dict = MessageToDict(response._pb)

    # Print the response as a formatted JSON string
    print(json.dumps(response_dict, indent=2))

    # Extract the SRT captions
    srt_output = response_dict["results"][audio_gcs_uri]["inlineResult"]["srtCaptions"]

    # Print the SRT output
    print("SRT Captions:\n")
    print(srt_output)


    run_batch_recognize()


    And it generates a decent srt content like so:

    1
    00:00:00,040 --> 00:00:02,960
    The sun set over the horizon
    painting the sky and hues of

    2
    00:00:02,960 --> 00:00:06,440
    orange and pink. A gentle breeze
    swept through the trees carrying

    3
    00:00:06,440 --> 00:00:10,440
    the scent of fresh pine. It was
    the perfect evening to unwind
    and relax.


    However is it possible to somehow ask google api to generate srt content such that there is only one or two words at any time? Something like below:

    1
    00:00:00,040 --> 00:00:00,540
    The

    2
    00:00:00,540 --> 00:00:01,040
    sun

    3
    00:00:01,040 --> 00:00:01,540
    set

    Continue reading...

Compartilhe esta Página