Picamでビデオを録画し、USB AUDIO DEVICEに接続されたマイクを介してオーディオをキャプチャするために、RaspberryPiでPythonでプログラムを作成しています。
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')