如何解决如何组合几个 base64 音频块来自麦克风
我从麦克风获取 base64 数据块。
我需要将它们连接起来并作为一个 Google Api
发送到 base64 string
以进行语音识别。粗略地说,在第一个块中,单词 Hello
被编码,而在第二个 world!
中。我需要粘合两个块,将它们发送到一行的 Google Api 并接收 Hello World!
响应
您可以以 Google Speech-to-Text
为例。 Google 还使用网络套接字从 base64 string
中的麦克风发送数据(请参阅 Network
)。
不幸的是,我手头没有麦克风 - 我无法检查它。我们必须现在就做。
假设我得到
chunk1 = "TgvsdUvK ...."
chunk2 = "UZZxgh5V ...."
我的理解是否正确,仅此而已
base64.b64encode (chunk1 + chunk2))
或者你还需要知道什么?不幸的是,一切都取决于缺少麦克风(
解决方法
您的编码 chunk1 + chunk2
示例不起作用,因为 base64 字符串在末尾有填充。如果只是将两个 base64 字符串连接在一起,则无法对其进行解码。
例如,字符串 StringA
和 StringB
,当它们的 ascii 或 utf-8 表示以 base64 编码时,如下所示:U3RyaW5nQQ==
和 U3RyaW5nQg==
。每一个都可以很好地解码。但是,如果将它们连接起来,结果将是 U3RyaW5nQQ==U3RyaW5nQg==
,这是无效的:
concatenated_b64_strings = 'U3RyaW5nQQ==U3RyaW5nQg=='
concatenated_b64_strings_bytes = concatenated_b64_strings.encode('ascii')
decoded_strings = base64.b64decode(concatenated_b64_strings_bytes)
print(decoded_strings.decode('ascii')) # just outputs 'StringA',which is incorrect
因此,为了将这两个字符串(我将其用作代替二进制数据的示例)并将它们连接在一起,仅从它们的 base64 表示开始,您必须对它们进行解码:
import base64
string1_base64 = 'U3RyaW5nQQ=='
string2_base64 = 'U3RyaW5nQg=='
# need to convert the strings to bytes first in order to decode them
base64_string1_bytes = string1_base64.encode('ascii')
base64_string2_bytes = string2_base64.encode('ascii')
# now,decode them into the actual bytes the base64 represents
base64_string1_bytes_decoded = base64.decodebytes(base64_string1_bytes)
base64_string2_bytes_decoded = base64.decodebytes(base64_string2_bytes)
# combine the bytes together
combined_bytes = base64_string1_bytes_decoded + base64_string2_bytes_decoded
# now,encode these bytes as base64
combined_bytes_base64 = base64.encodebytes(combined_bytes)
# finally,decode these bytes so you're left with a base64 string:
combined_bytes_base64_string = combined_bytes_base64.decode('ascii')
print(combined_bytes_base64_string) # output: U3RyaW5nQVN0cmluZ0I=
# let's prove that it concatenated successfully (you wouldn't do this in your actual code)
base64_combinedstring_bytes = combined_bytes_base64_string.encode('ascii')
base64_combinedstring_bytes_decoded_bytes = base64.decodebytes(base64_combinedstring_bytes)
base64_combinedstring_bytes_decoded_string = base64_combinedstring_bytes_decoded_bytes.decode('ascii')
print(base64_combinedstring_bytes_decoded_string) # output: StringAStringB
在您的情况下,您要组合的不仅仅是两个输入 base64 字符串,但过程是相同的。取所有字符串,将每个字符串编码为 ascii
字节,通过 base64.decodebytes()
对其进行解码,然后通过 +=
运算符将它们全部加在一起:
import base64
input_strings = ['U3RyaW5nQQ==','U3RyaW5nQg==']
input_strings_bytes = [input_string.encode('ascii') for input_string in input_strings]
input_strings_bytes_decoded = [base64.decodebytes(input_string_bytes) for input_string_bytes in input_strings_bytes]
combined_bytes = bytes()
for decoded in input_strings_bytes_decoded:
combined_bytes += decoded
combined_bytes_base64 = base64.encodebytes(combined_bytes)
combined_bytes_base64_string = combined_bytes_base64.decode('ascii')
print(combined_bytes_base64_string) # output: U3RyaW5nQVN0cmluZ0I=
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。