我在RaspberryPi上用python编写程序,在Picam上录制视频,并通过连接USB AUDIO DEVICE的麦克风捕获音频。
通过ffmpeg组合两个文件,直到30秒音频/视频同步良好但在30秒后,我开始遇到延迟问题。
你能指导我解决这个问题吗?
import picamera, subprocess, os
import pyaudio,wave,sys
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 60 #"Recording Duration"
WAVE_OUTPUT_FILENAME = 'Audio.wav'
H264_OUTPUT_FILENAME = 'Video.h264'
frames = []
with picamera.PiCamera() as camera:
camera.resolution= (640,480)
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
input_device_index = 0,
frames_per_buffer = CHUNK)
camera.start_preview(fullscreen=False, window=(930, 0, 425, 295))# "Start Video Preview"
camera.start_recording(H264_OUTPUT_FILENAME, quality = 30) # "Start Video Recording"
# "Start Audio Dubbing"
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream() # "Pause the Stream"
stream.close() # "Stream Stop"
p.terminate() # "Stream Cloase"
camera.stop_recording() # "Pause" the recording
camera.stop_preview() # "Stop Preview"
camera.close() # "Camera Close"
# Creation of MIC WAVE FILE
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# Combining/Merging of Audio/Video File into mkv
cmd = 'ffmpeg -y -i Audio.wav -r 30 -i Video.h264 -filter:a aresample=async=1 -c:a flac -c:v copy av.mkv'
subprocess.call(cmd, shell=True) # "Muxing Done
print('Muxing Done')