在浏览了pyaudio的文档并阅读了网上的其他文章后,如果我的理解是正确的,我会感到困惑.
这是在pyaudio网站上找到的录音代码:
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)
如果我添加这些行,那么我可以播放我记录的任何内容:
play=pyaudio.PyAudio()
stream_play=play.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
output=True)
for data in frames:
stream_play.write(data)
stream_play.stop_stream()
stream_play.close()
play.terminate()
Run Code Online (Sandbox Code Playgroud)
pyaudio.get_sample_size(pyaudio.paInt16). …我已经让OpenCV和PyAudio都工作了但是我不确定如何将它们同步到一起.我无法从OpenCV获得帧速率,并且测量帧的调用时间会随时变化.然而,对于PyAudio,它的基础是获取某个采样率.我如何以相同的速率同步它们.我假设有一些标准或某种方式编解码器做它.(我已经尝试了谷歌,我得到的是唇同步的信息:/).
OpenCV帧率
from __future__ import division
import time
import math
import cv2, cv
vc = cv2.VideoCapture(0)
# get the frame
while True:
before_read = time.time()
rval, frame = vc.read()
after_read = time.time()
if frame is not None:
print len(frame)
print math.ceil((1.0 / (after_read - before_read)))
cv2.imshow("preview", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print "None..."
cv2.waitKey(1)
# display the frame
while True:
cv2.imshow("preview", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Run Code Online (Sandbox Code Playgroud)
抓取并保存音频
from sys import byteorder
from array …Run Code Online (Sandbox Code Playgroud)