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:
- Camera 1 is connected via USB 3.0.
- Camera 2 is connected via USB 2.0 with a 30-meter cable and repeaters placed every 10 meters.
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!
- Camera 1 connected via USB 3.0 can record at 1280x720 at 20 fps without issues.
- Camera 2 connected via USB 2.0 (30m cable with repeaters) can only record at a maximum of 10 fps.
Things I’ve Tried:
- I’ve reduced the resolution and frame rate, but I still face the issue where Camera 2 drops below 20 fps.
- I have tried using multiple threads to record the videos simultaneously but without much success in improving performance for the second camera.
- The Raspberry Pi 5 has a 27W power supply, and both cameras are powered via USB.
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:
- Can using both cameras on USB 3.0 resolve the issue, even if one is on a long cable?
- Are there any optimizations for using USB 2.0 with long cables and repeaters for video recording?
- Any other performance tips for achieving better results with the Raspberry Pi 5?