Skip to content

Commit

Permalink
Added configurable video format/codec
Browse files Browse the repository at this point in the history
  • Loading branch information
connervieira committed May 27, 2024
1 parent c97e5f0 commit 10ec776
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ This update focuses on improving the reliability of Predator, especially when op
- Added a per-device configuration to set a minimum expected frame-rate, below which a warning is displayed. This value does not have any impact on the actual recording frame-rate.
- Overhauled dash-cam saving.
- Frames captured during dash-cam recording are now saved in a separate thread.
- The video file extension and codec is now configurable.
- Improved the reliability of dash-cam operation.
- The file saving back-end now handles sudden time jumps into the future much more reliably.
- Instead of creating each segment between the original time and new time, Predator skips to the next segment.
Expand Down
4 changes: 4 additions & 0 deletions assets/support/configdefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
"dashcam": {
"saving": {
"directory": "saved_dashcam",
"file": {
"codec": "XVID",
"extension": "avi"
},
"trigger": "dashcam_lock_trigger",
"trigger_gpio": {
},
Expand Down
4 changes: 4 additions & 0 deletions assets/support/configoutline.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
"dashcam": {
"saving": {
"directory": "str",
"file": {
"codec": "str",
"extension": "str"
},
"trigger": "str",
"trigger_gpio": "dict",
"segment_length": "+float",
Expand Down
15 changes: 8 additions & 7 deletions dashcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,15 +893,16 @@ def dashcam_output_handler(directory, device, width, height, framerate):
time.sleep(0.01)

previous_loop_segment_name = current_segment_name[device] # Initialize to be the same.
output = cv2.VideoWriter(current_segment_name[device] + ".avi", cv2.VideoWriter_fourcc(*'XVID'), float(framerate), (width, height)) # Initialize the first video output.
output_codec = list(config["dashcam"]["saving"]["file"]["codec"])
output = cv2.VideoWriter(current_segment_name[device] + "." + config["dashcam"]["saving"]["file"]["extension"], cv2.VideoWriter_fourcc(output_codec[0], output_codec[1], output_codec[2], output_codec[3]), float(framerate), (width, height)) # Initialize the first video output.

while True:
time.sleep(0.01)

video_filepath = current_segment_name[device] + ".avi"
video_filepath = current_segment_name[device] + "." + config["dashcam"]["saving"]["file"]["extension"]
audio_filepath = current_segment_name[device] + "." + config["dashcam"]["capture"]["audio"]["extension"]
if (last_segment_name != ""):
last_video_file = last_segment_name + ".avi"
last_video_file = last_segment_name + "." + config["dashcam"]["saving"]["file"]["extension"]
last_audio_file = last_segment_name + "." + config["dashcam"]["capture"]["audio"]["extension"]
else:
last_video_file = ""
Expand Down Expand Up @@ -939,18 +940,18 @@ def dashcam_output_handler(directory, device, width, height, framerate):
last_segment_name = previous_loop_segment_name

output = None # Release the previous video output file.
output = cv2.VideoWriter(current_segment_name[device] + ".avi", cv2.VideoWriter_fourcc(*'XVID'), float(calculated_framerate[device]), (width, height)) # Start the new video output file.
output = cv2.VideoWriter(current_segment_name[device] + "." + config["dashcam"]["saving"]["file"]["extension"], cv2.VideoWriter_fourcc(output_codec[0], output_codec[1], output_codec[2], output_codec[3]), float(calculated_framerate[device]), (width, height)) # Start the new video output file.

process_timing("start", "Dashcam/Calculations")
if (calculated_framerate[device] > float(config["dashcam"]["capture"]["video"]["devices"][device]["framerate"]["max"])): # Check to see if the calculated frame-rate exceeds the maximum allowed frame-rate.
calculated_framerate[device] = float(config["dashcam"]["capture"]["video"]["devices"][device]["framerate"]["max"]) # Set the frame-rate to the maximum allowed frame-rate.
process_timing("end", "Dashcam/Calculations")
process_timing("start", "Dashcam/Writing")
output = cv2.VideoWriter(current_segment_name[device] + ".avi", cv2.VideoWriter_fourcc(*'XVID'), float(calculated_framerate[device]), (width, height)) # Update the video output.
output = cv2.VideoWriter(current_segment_name[device] + "." + config["dashcam"]["saving"]["file"]["extension"], cv2.VideoWriter_fourcc(output_codec[0], output_codec[1], output_codec[2], output_codec[3]), float(calculated_framerate[device]), (width, height)) # Update the video output.
process_timing("end", "Dashcam/Writing")


last_video_path = last_segment_name + ".avi"
last_video_path = last_segment_name + "." + config["dashcam"]["saving"]["file"]["extension"]
last_audio_path = last_segment_name + "." + str(config["dashcam"]["capture"]["audio"]["extension"])


Expand Down Expand Up @@ -979,7 +980,7 @@ def dashcam_output_handler(directory, device, width, height, framerate):

# Now that the merged file has been saved, go back and delete the separate files that were saved to the locked folder previously.
base_file = config["general"]["working_directory"] + "/" + config["dashcam"]["saving"]["directory"] + "/" + os.path.splitext(os.path.basename(last_filename_merged))[0]
os.system("rm '" + base_file + ".avi'")
os.system("rm '" + base_file + "." + config["dashcam"]["saving"]["file"]["extension"] + "'")
os.system("rm '" + base_file + "." + str(config["dashcam"]["capture"]["audio"]["extension"]) + "'")
else: # If the merged video file doesn't exist, it is likely something went wrong with the merging process.
display_message("The merged video/audio file did exist when Predator tried to save it. It is likely the merge process has failed unexpectedly. The separate files are being saved as a fallback.", 2)
Expand Down

0 comments on commit 10ec776

Please sign in to comment.