У меня есть рабочий код на Python, который генерирует файл srt, используя преобразование речи Google Cloud в текст.
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 = ""
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="",
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()
И он генерирует достойный SRT-контент, например:
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.
Однако можно ли каким-то образом попросить Google API генерировать SRT-контент, чтобы в любой момент времени было только одно или два слова? Примерно так:
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
Подробнее здесь: https://stackoverflow.com/questions/790 ... -speech-to
Как получить мелкозернистую srt с меньшим временным разрешением из облачной речи Google в текстовый API? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как настроить Google TTS SSML в соответствии с оригинальным временем SRT?
Anonymous » » в форуме Python - 0 Ответы
- 7 Просмотры
-
Последнее сообщение Anonymous
-