Skip to content

Commit

Permalink
Improved audio configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
connervieira committed Oct 29, 2024
1 parent a0c153b commit e568695
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,11 @@ This update emphasizes improving the reliability of Predator, especially when op
- Updated audio recording.
- Improved the reliability of audio recording.
- Fixed unexpected behavior when the working directory path contained spaces.
- Added configuration option to use a different audio recording device.
- Added configuration option to determine which user will be used to run the audio recording process.
- Added configuration options for audio recording.
- Added configuration option to use a different audio recording device.
- Added configuration option to determine which user on the system will be used to run the audio recording process.
- Added configuration option to set the format used by the recording device.
- Added configuration option to slightly delay the start of audio recording to prevent the capture device from being opened by two threads at once.
- Added per-device resolution configuration.
- Added the ability to disable capture devices in the configuration without removing them from the configuration entirely.
- Predator now changes the status light color when a video is being locked.
Expand Down
4 changes: 3 additions & 1 deletion assets/support/configdefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@
"enabled": false,
"extension": "wav",
"device": "",
"format": "S16_LE",
"merge": true,
"record_as_user": "pi"
"record_as_user": "pi",
"start_delay": 0.2
}
},
"parked": {
Expand Down
4 changes: 3 additions & 1 deletion assets/support/configoutline.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@
"enabled": "bool",
"extension": "str",
"device": "str",
"format": ["S8", "U8", "S16_LE", "S16_BE", "U16_LE", "U16_BE", "S24_LE", "S24_BE", "U24_LE", "U24_BE", "S32_LE", "S32_BE", "U32_LE", "U32_BE", "FLOAT_LE", "FLOAT_BE", "FLOAT64_LE", "FLOAT64_BE", "IEC958_SUBFRAME_LE", "IEC958_SUBFRAME_BE", "MU_LAW", "A_LAW", "IMA_ADPCM", "MPEG", "GSM", "S20_LE", "S20_BE", "U20_LE", "U20_BE", "SPECIAL", "S24_3LE", "S24_3BE", "U24_3LE", "U24_3BE", "S20_3LE", "S20_3BE", "U20_3LE", "U20_3BE", "S18_3LE", "S18_3BE", "U18_3LE", "U18_3BE", "G723_24", "G723_24_1B", "G723_40", "G723_40_1B", "DSD_U8", "DSD_U16_LE", "DSD_U32_LE", "DSD_U16_BE", "DSD_U32_BE"],
"merge": "bool",
"record_as_user": "str"
"record_as_user": "str",
"start_delay": "+float"
}
},
"parked": {
Expand Down
9 changes: 4 additions & 5 deletions dashcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@
audio_recorders = {} # This will hold each audio recorder process.
first_segment_started_time = 0

audio_record_delay = 0.2 # This is the length of time (in seconds) that Predator will wait before starting the next audio recorder. This prevents the capture device from being opened by two threads at once.
audio_record_command = "sudo -u " + str(config["dashcam"]["capture"]["audio"]["record_as_user"]) + " arecord --format=S16_LE"
audio_record_command = "sudo -u " + str(config["dashcam"]["capture"]["audio"]["record_as_user"]) + " arecord --format=" + str(config["dashcam"]["capture"]["audio"]["format"])
if (config["dashcam"]["capture"]["audio"]["device"] != ""): # Check to see if a custom device has been set.
audio_record_command += " --device=" + str(config["dashcam"]["capture"]["audio"]["device"]) + ""

Expand Down Expand Up @@ -683,7 +682,7 @@ def capture_dashcam_video(directory, device="main", width=1280, height=720):
audio_base_name = "_".join(os.path.basename(current_segment_name[device]).split("_")[0:3])
audio_filepath = directory + "/" + audio_base_name + "." + str(config["dashcam"]["capture"]["audio"]["extension"])
if (audio_base_name not in audio_recorders or audio_recorders[audio_base_name].poll() is not None): # Check to see if the audio recorder hasn't yet been started by another thread.
command = "sleep " + str(audio_record_delay) + "; " + audio_record_command + " \"" + str(audio_filepath) + "\""
command = "sleep " + str(float(config["dashcam"]["capture"]["audio"]["start_delay"])) + "; " + audio_record_command + " \"" + str(audio_filepath) + "\""
audio_recorders[audio_base_name] = subprocess.Popen(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # Start the next segment's audio recorder.
del command
process_timing("end", "Dashcam/Audio Processing")
Expand Down Expand Up @@ -717,7 +716,7 @@ def capture_dashcam_video(directory, device="main", width=1280, height=720):
audio_filepath = directory + "/" + audio_base_name + "." + str(config["dashcam"]["capture"]["audio"]["extension"])
if (audio_base_name not in audio_recorders or audio_recorders[audio_base_name].poll() is not None): # Check to see if the audio recorder hasn't yet been started by another thread.
subprocess.Popen(("sudo -u " + str(config["dashcam"]["capture"]["audio"]["record_as_user"]) + " killall arecord").split(" "), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # Kill the previous arecord instance (if one exists)
command = "sleep " + str(audio_record_delay) + "; " + audio_record_command + " \"" + str(audio_filepath) + "\""
command = "sleep " + str(float(config["dashcam"]["capture"]["audio"]["start_delay"])) + "; " + audio_record_command + " \"" + str(audio_filepath) + "\""
audio_recorders[audio_base_name] = subprocess.Popen(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # Start the next segment's audio recorder.
del command
process_timing("end", "Dashcam/Audio Processing")
Expand Down Expand Up @@ -1178,7 +1177,7 @@ def dashcam_output_handler(directory, device, width, height, framerate):
audio_offset = -(config["dashcam"]["parked"]["recording"]["buffer"] / calculated_framerate[device]) # Calculate the audio offset based on the size of the frame-buffer
print("Applying offset of " + str(audio_offset))
else: # Otherwise, the video was recorded during normal operating.
audio_offset = audio_record_delay # Don't apply an offset, because the audio and video file should start at the same time.
audio_offset = float(config["dashcam"]["capture"]["audio"]["start_delay"]) # Apply the normal configured offset.
merge_audio_video(last_video_path, last_audio_path, last_filename_merged, audio_offset) # Run the audio/video merge.
process_timing("end", "Dashcam/File Merging")

Expand Down
9 changes: 7 additions & 2 deletions docs/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,16 @@ This document describes the configuration values found `config.json`.
- `width` sets the width of the video, measured in pixels.
- `height` sets the height of the video, measured in pixels.
- `audio` contains settings for configuring Predator's audio recording behavior.
- `enabled` is a boolean that determines whether or not audio will be recorded at all.
- `enabled` is a boolean that determines whether or not audio will be recorded during dash-cam operation.
- `extension` sets the file extension that audio will be saved with.
- `device` specifies the device ID (as determined by `arecord --list-pcms`) that will be used to capture audio.
- `format` specifies the format that will be used by the `arecord` process.
- `merge` is a boolean that determines whether or not Predator will merge the separate audio and video files when each segment is done recording.
- `record_as_user` specifies the user on the system that the audio recording process will be run as. This is useful if the user you normally run Predator with does not have permission to access audio devices.
- `record_as_user` specifies the user on the system that the audio recording process will be run as.
- This is useful if the user you normally run Predator with does not have permission to access audio devices.
- `start_delay` determines a length of time (in seconds) that Predator will wait before starting audio recording for each segment.
- This delay allows the audio capture device to be release by the previous segment before it is re-opened for the current segment.
- This value should be as short as possible to reduce the period of silence at the start of each video, but not so short that the audio capture device fails to open due to being already claimed by the previous segment.
- `parked` contains settings to configure the dashcam's parking behavior. Parking mode is experimental, and not recommended for daily use.
- `enabled` is a boolean that determines whether Predator will ever go into a parked state.
- When this value is set to `false` Predator will never enable parked mode, even if the conditions defined in this configuration section are met.
Expand Down
17 changes: 17 additions & 0 deletions docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Troubleshooting

This document lists specific problems that you may encounter, and suggested solutions.

All of these solutions assume you have already completed the full installation/configuration process.


## Dash-cam

### The audio recording process works for the first segment, but then occasionally fails for subsequent segments.

Try increasing the `dashcam>capture>audio>start_delay` value. It's likely the audio capture device is not being fully released by the previous segment before it tries to be accessed by the new segment.


### The audio is not synced up with the video (the audio is longer or shorter than the video).

Ensure that the video recording is running at a consistent frame-rate (and therefore, is a consistent length of time). For example, if your video flucuates between 32 and 37 FPS, try capping it at 30FPS to ensure a consistent frame-rate. This can be done with the `dashcam>capture>video>devices>DEVICE_NAME>frame_rate>max` configuration value.

0 comments on commit e568695

Please sign in to comment.