首页 > 解决方案 > 如何组合几个 base64 音频块(来自麦克风)

问题描述

我从麦克风中得到 base64 块。

我需要将它们连接起来并Google API作为base64 string语音识别发送。粗略地说,在第一个块中,单词Hello被编码,而在第二个块中world!。我需要粘合两个块,将它们发送到一行的google api并接收Hello world!响应

你可以看看Google Speech-to-Text例子。Google 还base64 string使用 websocket 从麦克风发送数据(请参阅 参考资料Network)。

不幸的是,我手头没有麦克风——我无法检查。我们现在必须这样做。

假设我得到

chunk1 = "TgvsdUvK ...."
chunk2 = "UZZxgh5V ...."

我是否正确理解这将足够

base64.b64encode (chunk1 + chunk2))

或者你还需要知道别的吗?不幸的是,一切都取决于缺少麦克风(

标签: pythonpython-3.xbase64speech-to-textmicrophone

解决方案


您的编码示例chunk1 + chunk2不起作用,因为 base64 字符串末尾有填充。如果您只是将两个 base64 字符串连接在一起,它们将无法被解码。

例如,字符串StringAStringB,当它们的 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=

推荐阅读