Skip to content

Commit

Permalink
Use preview quality setting to set image height and webp quality (#10502
Browse files Browse the repository at this point in the history
)

* Use preview quality setting to set image height and webp quality

* Increase keyframe interval as well with higher quality

* Don't use dynamic height
  • Loading branch information
NickM-27 authored Mar 17, 2024
1 parent e4d0e22 commit bb6f153
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions frigate/output/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@
PREVIEW_CACHE_DIR = os.path.join(CACHE_DIR, FOLDER_PREVIEW_FRAMES)
PREVIEW_SEGMENT_DURATION = 3600 # one hour
# important to have lower keyframe to maintain scrubbing performance
PREVIEW_KEYFRAME_INTERVAL = 60
PREVIEW_BIT_RATES = {
RecordQualityEnum.very_low: 5120,
RecordQualityEnum.low: 7168,
PREVIEW_KEYFRAME_INTERVAL = 40
PREVIEW_HEIGHT = 180
PREVIEW_QUALITY_WEBP = {
RecordQualityEnum.very_low: 70,
RecordQualityEnum.low: 80,
RecordQualityEnum.medium: 80,
RecordQualityEnum.high: 80,
RecordQualityEnum.very_high: 86,
}
PREVIEW_QUALITY_BIT_RATES = {
RecordQualityEnum.very_low: 7168,
RecordQualityEnum.low: 8196,
RecordQualityEnum.medium: 9216,
RecordQualityEnum.high: 13312,
RecordQualityEnum.very_high: 17408,
RecordQualityEnum.high: 9864,
RecordQualityEnum.very_high: 10096,
}


Expand Down Expand Up @@ -69,7 +77,7 @@ def __init__(
self.ffmpeg_cmd = parse_preset_hardware_acceleration_encode(
config.ffmpeg.hwaccel_args,
input="-f concat -y -protocol_whitelist pipe,file -safe 0 -i /dev/stdin",
output=f"-g {PREVIEW_KEYFRAME_INTERVAL} -fpsmax 2 -bf 0 -b:v {PREVIEW_BIT_RATES[self.config.record.preview.quality]} {FPS_VFR_PARAM} -movflags +faststart -pix_fmt yuv420p {self.path}",
output=f"-g {PREVIEW_KEYFRAME_INTERVAL} -fpsmax 2 -bf 0 -b:v {PREVIEW_QUALITY_BIT_RATES[self.config.record.preview.quality]} {FPS_VFR_PARAM} -movflags +faststart -pix_fmt yuv420p {self.path}",
type=EncodeTypeEnum.preview,
)

Expand Down Expand Up @@ -131,7 +139,7 @@ def __init__(self, config: CameraConfig) -> None:
self.start_time = 0
self.last_output_time = 0
self.output_frames = []
self.out_height = 180
self.out_height = PREVIEW_HEIGHT
self.out_width = (
int((config.detect.width / config.detect.height) * self.out_height) // 4 * 4
)
Expand Down Expand Up @@ -245,7 +253,10 @@ def write_frame_to_cache(self, frame_time: float, frame) -> None:
cv2.imwrite(
get_cache_image_name(self.config.name, frame_time),
small_frame,
[int(cv2.IMWRITE_WEBP_QUALITY), 80],
[
int(cv2.IMWRITE_WEBP_QUALITY),
PREVIEW_QUALITY_WEBP[self.config.record.preview.quality],
],
)

def write_data(
Expand Down

2 comments on commit bb6f153

@tungmeister
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NickM-27 how come you reverted the preview resolution based on quality?

@NickM-27
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we are using yuv420p, downsizing requires specific dimensions otherwise the source image can not be resized causing a black bar at the bottom. In my testing it also greatly increased storage usage which is one of the main priorities

Please sign in to comment.