python - 如何组合几个 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))
或者你还需要知道别的吗?不幸的是,一切都取决于缺少麦克风(
解决方案
您的编码示例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=
推荐阅读
- ruby - 为什么这个字符串值被转换成一个数组?
- hyperledger-fabric - Hyperledger Fabric:创建通道配置包时出错
- batch-file - windows中没有管理员权限的进程命令行
- vba - 如果单元格不为空,我需要使用 VBA 更改单元格格式
- nosql - 合并 - 谷歌云数据存储中的 upsert/delete
- ruby-on-rails - Rails 由于关联而无法保存后在表中创建条目
- python - 在 Spark 中,RDD 是不可变的,那么 Accumulators 是如何实现的呢?
- mysql - 为什么我的 IfNull() 函数不起作用?
- angular - ng serve 命令提示 Angular 2 项目中的错误 549
- sql - 我如何在 sql server 2008 中计算字符