As I understand it the h264 files do not contain any timestamp data, so ffmpeg isn't able to generate an mp4 file with the original framerate specified in the rpicam-vid (libcamera-vid) command.
My python script above, records (and merges) them but the time for each file is always 0. That was my original question. How to modify the python script to synchronously record from two cameras (for 10 seconds), merge them (ffmpeg) and save the results as another mp4.
This is what my script produces visually. But the duration is 0. (Should be 10 sec)
IMG_0076.jpg
The generated files don't seem to have 0 duration just an invalid framerate which -when played with VLC for instance- shows up as a single frame.
The same files played with mpv media player do play but at a high framerate.
As suggested by DP, recording direct to .mp4 files ensures a valid set of timestamp/framerate data in the files.
A modified version of your code without the h264 to mp4 conversion, using direct to mp4 recording in each channel is below.
Don't think this will be any more, or less, synchronous than the h264 version? (Not sure though!)
Code:
import subprocessimport threadingimport os# Set parameters for video capturewidth, height = 1500, 1500fps = 15record_time = 10 # seconds# Output file pathsmp4_output1 = "camera1.mp4"mp4_output2 = "camera2.mp4"merged_output = "merged_output.mp4"# Function to record video from one camera using libcamera-viddef record_camera(camera_index, output_file): libcamera_cmd = [ "libcamera-vid", "-t", str(record_time * 1000), # Convert seconds to milliseconds "-o", output_file, "--width", str(width), "--height", str(height), "--framerate", str(fps), "--camera", str(camera_index) ] subprocess.run(libcamera_cmd)# Record from both cameras in paralleldef record_both_cameras(): thread1 = threading.Thread(target=record_camera, args=(0, mp4_output1)) # First camera thread2 = threading.Thread(target=record_camera, args=(1, mp4_output2)) # Second camera # Start recording thread1.start() thread2.start() # Wait for both threads to finish thread1.join() thread2.join()# Merge the two MP4 videos side-by-side using ffmpegdef merge_videos(): ffmpeg_cmd = [ "ffmpeg", "-i", mp4_output1, "-i", mp4_output2, "-filter_complex", "hstack", "-c:v", "libx264", "-crf", "23", "-preset", "veryfast", merged_output ] subprocess.run(ffmpeg_cmd)# Remove temporary MP4 filesdef clean_up(): try: os.remove(mp4_output1) os.remove(mp4_output2) print(f"Removed {mp4_output1} and {mp4_output2}") except OSError as e: print(f"Error: {e.filename} - {e.strerror}")if __name__ == "__main__": # Record videos from both cameras record_both_cameras() # Merge the two mp4 videos side by side merge_videos() # Clean up by removing the temporary MP4 files clean_up() print(f"Recording completed and videos merged into {merged_output}")
Statistics: Posted by sandyol — Fri Oct 11, 2024 11:03 pm