Python 中来自实时网络音频流的原始 numpy 数组
Raw numpy array from real-time network audio stream in Python
我想从我有URL。目标是实时 分析来自网络流的信号 。
似乎有很多 Python 音频模块或包装器,例如FFMPEG,但经过相当广泛的搜索后,我还没有找到一个完整管道的例子。
对于那些熟悉 OpenCV 的人,我正在寻找 OpenCV 的音频副本 VideoCampture
class。
欢迎提出要查看的模块或代码片段的任何建议!
好的,明白了。显然,这可以在不使用任何外部库的情况下完成,仅依赖 urllib
和 wave
。这是一个代码片段,它流式传输数据,将其转换为 numpy 数组(例如用于处理),然后返回以将其保存到文件中。测试 Python 3.
import urllib
import base64
import wave
import numpy as np
# Open the network wave stream
request = urllib.request.Request("http://url")
request.add_header('Authorization',
b'Basic ' + base64.b64encode(b'user:password'))
in_file = urllib.request.urlopen(request)
in_wave = wave.open(in_file, 'rb')
# Get parameters such as number of channels, framerate etc.
params = in_wave.getparams()
# Open and initialize an output file
out_wave = wave.open('/home/user/out.wav', 'wb')
out_wave.setparams(params)
while True:
# Get N frames as byte array
frame = in_wave.readframes(10000)
# Convert the bytes to numpy array
arr = np.fromstring(frame, 'Int16')
# Write a numpy array into a wave file
out_wave.writeframes(arr.tostring())
我想从我有URL。目标是实时 分析来自网络流的信号 。
似乎有很多 Python 音频模块或包装器,例如FFMPEG,但经过相当广泛的搜索后,我还没有找到一个完整管道的例子。
对于那些熟悉 OpenCV 的人,我正在寻找 OpenCV 的音频副本 VideoCampture
class。
欢迎提出要查看的模块或代码片段的任何建议!
好的,明白了。显然,这可以在不使用任何外部库的情况下完成,仅依赖 urllib
和 wave
。这是一个代码片段,它流式传输数据,将其转换为 numpy 数组(例如用于处理),然后返回以将其保存到文件中。测试 Python 3.
import urllib
import base64
import wave
import numpy as np
# Open the network wave stream
request = urllib.request.Request("http://url")
request.add_header('Authorization',
b'Basic ' + base64.b64encode(b'user:password'))
in_file = urllib.request.urlopen(request)
in_wave = wave.open(in_file, 'rb')
# Get parameters such as number of channels, framerate etc.
params = in_wave.getparams()
# Open and initialize an output file
out_wave = wave.open('/home/user/out.wav', 'wb')
out_wave.setparams(params)
while True:
# Get N frames as byte array
frame = in_wave.readframes(10000)
# Convert the bytes to numpy array
arr = np.fromstring(frame, 'Int16')
# Write a numpy array into a wave file
out_wave.writeframes(arr.tostring())