Google Django 中的 TTS:从 base64 字符串创建 Javascript 中的音频文件
Google TTS in Django: Create Audio File in Javascript from base64 String
我目前正在我的一个 Django 视图中使用 Google's TTS Python API "synthesize_text" 函数。
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
# Removing this because I do not care about writing the audio file
# ----------------------------------------------------
'''
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
'''
# ----------------------------------------------------
# instead return the encoded audio_content to decode and play in Javascript
return response.audio_content
def my_view(request):
test_audio_content = synthesize_text('Test audio.')
return render('my_template.html', {'test_audio_content': test_audio_content})
我对 "synthesize_text" 函数所做的唯一更改是 return audio_content 而不是将其写入音频文件。这是因为我不关心存储文件,而是只想使用 Javascript 在我的模板中播放它。 Google 声称他们在 base64 中对 audio_content 进行了编码:"Cloud Text-to-Speech API allows you to convert words and sentences into base64 encoded audio data of natural human speech. You can then convert the audio data into a playable audio file like an MP3 by decoding the base64 data." 所以我尝试按照建议 here:
使用以下代码创建和播放音频文件
<!-- my_template.html -->
<script>
var audio_content = "{{ test_audio_content }}";
var snd = new Audio("data:audio/mp3;base64," + audio_content);
console.log(snd);
snd.play();
</script>
但我收到以下错误:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
我注销了 audio_content,它开始为 b'ÿóDÄH
.. 不确定那是否是 base64。
我还尝试通过以下方式解码 audio_content:
var decoded_content = window.atob(audio_content);
这也给了我一个错误,声称它不是 base64。
根据你的例子:
The response's audio_content is binary
这意味着您需要先将结果编码为base64,然后才能使用它:
import base64
...
return base64.b64encode(response.audio_content).decode('ascii'))
那么这应该与您的 JS 代码段完全按照您的预期工作。
我目前正在我的一个 Django 视图中使用 Google's TTS Python API "synthesize_text" 函数。
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
# Removing this because I do not care about writing the audio file
# ----------------------------------------------------
'''
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
'''
# ----------------------------------------------------
# instead return the encoded audio_content to decode and play in Javascript
return response.audio_content
def my_view(request):
test_audio_content = synthesize_text('Test audio.')
return render('my_template.html', {'test_audio_content': test_audio_content})
我对 "synthesize_text" 函数所做的唯一更改是 return audio_content 而不是将其写入音频文件。这是因为我不关心存储文件,而是只想使用 Javascript 在我的模板中播放它。 Google 声称他们在 base64 中对 audio_content 进行了编码:"Cloud Text-to-Speech API allows you to convert words and sentences into base64 encoded audio data of natural human speech. You can then convert the audio data into a playable audio file like an MP3 by decoding the base64 data." 所以我尝试按照建议 here:
使用以下代码创建和播放音频文件<!-- my_template.html -->
<script>
var audio_content = "{{ test_audio_content }}";
var snd = new Audio("data:audio/mp3;base64," + audio_content);
console.log(snd);
snd.play();
</script>
但我收到以下错误:
Uncaught (in promise) DOMException: Failed to load because no supported source was found.
我注销了 audio_content,它开始为 b'ÿóDÄH
.. 不确定那是否是 base64。
我还尝试通过以下方式解码 audio_content:
var decoded_content = window.atob(audio_content);
这也给了我一个错误,声称它不是 base64。
根据你的例子:
The response's audio_content is binary
这意味着您需要先将结果编码为base64,然后才能使用它:
import base64
...
return base64.b64encode(response.audio_content).decode('ascii'))
那么这应该与您的 JS 代码段完全按照您的预期工作。