为了为我的视频生成字幕,我将它们转换成音频文件,并使用了Cloud Speech-to-Text。它可以工作,但它只生成转录,而我需要的是一个*.srt
/*.vtt
/similar文件。
我需要的是YouTube所做的:生成转录并将它们与视频同步,就像字幕格式,即:字幕应该出现的时候的转录。
虽然我可以将它们上传到YouTube,然后下载自动生成的字幕,但这似乎不是很正确。
有没有办法使用Google Cloud Speech生成SRT文件(或类似文件)?
发布于 2018-09-23 21:08:22
实际上,无法直接从Speech- to -Text API完成此操作。您可以尝试对语音识别结果进行一些后处理。
例如,下面是一个对REST API的请求,该请求使用一个用于transcribe video的模型,并带有一个由谷歌提供的公共示例文件:
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://speech.googleapis.com/v1p1beta1/speech:longrunningrecognize \
--data "{
'config': {
'encoding': 'LINEAR16',
'sampleRateHertz': 16000,
'languageCode': 'en-US',
'enableWordTimeOffsets': true,
'enableAutomaticPunctuation': true,
'model': 'video'
},
'audio': {
'uri':'gs://cloud-samples-tests/speech/Google_Gnome.wav'
}
}"
上面使用了异步识别(speech:longrunningrecognize
),它更适合较大的文件。Enabling punctuation ('enableAutomaticPunctuation': true
)结合靠近每个句子开头和结尾的单词索引( start and end times of words,'enableWordTimeOffsets': true
)(您还必须将它从nanos转换为时间戳)可以让您在srt format中提供一个文本文件。您可能还必须包括一些关于在任何给定时间出现在屏幕上的句子的最大长度的规则。
上面的实现应该不会太难,但是,你很有可能仍然会遇到计时/同步问题。
发布于 2019-10-25 19:30:59
没有办法使用Google Cloud本身进行购买,建议您可以对结果进行后处理。
In this file我做了一个快速的代码,可以完成这项工作。您可能希望根据自己的需要对其进行调整:
function convertGSTTToSRT(string) {
var obj = JSON.parse(string);
var i = 1;
var result = ''
for (const line of obj.response.results) {
result += i++;
result += '\n'
var word = line.alternatives[0].words[0]
var time = convertSecondStringToRealtime(word.startTime);
result += formatTime(time) + ' --> '
var word = line.alternatives[0].words[line.alternatives[0].words.length - 1]
time = convertSecondStringToRealtime(word.endTime);
result += formatTime(time) + '\n'
result += line.alternatives[0].transcript + '\n\n'
}
return result;
}
function formatTime(time) {
return String(time.hours).padStart(2, '0')+ ':' + String(time.minutes).padStart(2, '0') + ':' +
String(time.seconds).padStart(2, '0') + ',000';
}
function convertSecondStringToRealtime(string) {
var seconds = string.substring(0, string.length - 1);
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor(seconds % 3600 / 60);
seconds = Math.floor(seconds % 3600 % 60);
return {
hours, minutes, seconds
}
}
发布于 2020-10-24 01:06:14
使用此请求参数"enable_word_time_offsets: True“获取单词组的时间戳。然后以编程方式创建一个srt。
https://stackoverflow.com/questions/52397890
复制相似问题