I’m working with a Raspberry Pi 5 (27W power supply) and I’m facing an issue while recording two videos simultaneously with two identical USB cameras. Here's the setup:
Both cameras are capable of recording at 1280x720 at 20-30 fps, but when trying to record both videos concurrently, Camera 1 works fine at 1280x720 at 20 fps, while Camera 2 can only record at 10 fps or lower.
My goal is to have both cameras recording at 20 fps at 1280x720 resolution simultaneously.
Here is the code I’m using to record the videos:
import os
import subprocess
from threading import Thread
def record_video(camera_device, video_duration, output_file, resolution, frame_rate, threads=1):
try:
command = [
'ffmpeg',
'-f', 'v4l2',
'-input_format', 'mjpeg',
'-framerate', str(frame_rate),
'-video_size', resolution,
'-t', str(video_duration),
'-i', camera_device,
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-tune', 'zerolatency',
'-profile:v', 'baseline',
'-level', '3.0',
'-pix_fmt', 'yuv420p',
'-threads', str(threads),
'-bufsize', '2000k',
'-b:v', '1000k',
'-maxrate', '1000k',
'-f', 'mp4',
'-movflags', '+faststart',
output_file
]
subprocess.run(command, check=True, capture_output=True, text=True)
print(f"Video saved: {output_file}")
except Exception as e:
print(f"Error recording video from {camera_device}: {e}")
def record_two_videos_concurrently():
video_duration = 30 # Duration in seconds
output_folder1 = '/home/user/Desktop/first_video'
output_folder2 = '/home/user/Desktop/second_video'
os.makedirs(output_folder1, exist_ok=True)
os.makedirs(output_folder2, exist_ok=True)
output_file1 = os.path.join(output_folder1, "video1.mp4")
output_file2 = os.path.join(output_folder2, "video2.mp4")
thread1 = Thread(target=record_video, args=("/dev/video0", video_duration, output_file1, "1280x720", 20, 2)) # 2 threads for the first video
thread2 = Thread(target=record_video, args=("/dev/video2", video_duration, output_file2, "1280x720", 20, 1)) # 1 thread for the second video
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("Recording completed for both videos.")
if __name__ == "__main__":
record_two_videos_concurrently()
log info
Errore durante la registrazione del video su /dev/video2: Command '['ffmpeg', '-f', 'v4l2', '-input_format', 'mjpeg', '-framerate', '20', '-video_size', '1280x720', '-t', '30', '-i', '/dev/video2', '-c:v', 'libx264', '-preset', 'ultrafast', '-tune', 'zerolatency', '-profile:v', 'baseline', '-level', '3.0', '-pix_fmt', 'yuv420p', '-bufsize', '2000k', '-b:v', '1000k', '-maxrate', '1000k', '-f', 'mp4', '-movflags', '+faststart', '/home/lookatme/Desktop/lookatme-rpiserver/second_video/video2.mp4']' returned non-zero exit status 183.
Thank you in advance for any help!
Things I’ve Tried:
What I’m Looking For: I would appreciate any advice or suggestions on how to improve this setup to achieve at least two videos recording at 20 fps at 1280x720 simultaneously. Specifically:
I have a two camera system at my desk on a Rpi 4B so I looked into this question a bit. I have CM270 cameras which top out at 10fps when running 1280x720, so I was not able to duplicate the exact issue you are having, however, here are some steps to attempt to isolate the source of the bottleneck.
See if writing to the filesystem is the bottleneck. Try temporarily changing your output files to /dev/null and repeat the test, see if the fps improves on camera 2. If this helps, then you could write the files to a ramdisk and then move to filesystem after recording completes.
Use top to see CPU utilization real-time while you are running both ffmpeg sessions. Mine looked like this, and showed 100% utilization on first ffmpeg and second one showed CPU usage too, but also wouldn't encode so clearly broken.
USB signal integrity does come into question when trying to extend the physics with multiple repeaters in series. Do you have the ability to temporarily attach camera 2 directly to the Rpi 5 without the repeaters and see if that affects fps at all?
How are the signal repeaters powered? You mentioned a 27W power supply to power the Rpi 5 & the cameras over USB. Depending on the current draw from your cameras + powering the repeaters, you may be experiencing brown-out conditions on the USB path. The Rpi 5 requests 25W, and has 1.6A available for USB. Not sure if thats equally split across 4 USB ports or if all 1.6A is available on a single port to drive the camera + repeater chain? The CM270 cameras I have draw about 0.22A each. The USB repeaters could draw 0.5A each, making the USB load 0.22 + 0.22 + 0.5 + 0.5 + 0.5 = 1.94A exceeding the allowable current. I'd suspect you'd be getting some USB related failures on dmesg though.