Skip to content

Commit bdb801d

Browse files
committed
Checks performed on ffmpeg encoder functionality
1 parent 282559d commit bdb801d

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

GUI/camera_setup_tab.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
QSizePolicy,
1414
QHeaderView,
1515
)
16-
from PyQt6.QtCore import QTimer
16+
from PyQt6.QtCore import QTimer, Qt
17+
from PyQt6.QtGui import QIcon
18+
1719
from dataclasses import asdict
1820

1921
from config.config import paths_config, default_camera_config

GUI/camera_widget.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import csv
33
import json
44
import subprocess
5+
import signal
56
import numpy as np
67
from datetime import datetime
78
from collections import deque
@@ -52,8 +53,6 @@ def __init__(self, parent, label, subject_id):
5253
self.label = label
5354
self.settings = self.camera_setup_tab.get_camera_settings_from_label(label)
5455
self.camera_api = init_camera_api_from_module(settings=self.settings)
55-
self.cam_width = self.camera_api.get_width()
56-
self.cam_height = self.camera_api.get_height()
5756
self._image_data = None
5857
self.frame_timestamps = deque(maxlen=10)
5958
self.controls_visible = True
@@ -174,6 +173,8 @@ def fetch_image_data(self) -> None:
174173
if self.recording:
175174
self.recorded_frames += len(new_images["images"])
176175
for frame in new_images["images"]: # Send new images to FFMPEG for encoding.
176+
# Downsample frame
177+
frame = frame[::self.settings.downsampling_factor, ::self.settings.downsampling_factor]
177178
self.ffmpeg_process.stdin.write(frame.tobytes())
178179
for gpio_pinstate in new_images["gpio_data"]: # Write GPIO pinstate to file.
179180
self.gpio_writer.writerow(gpio_pinstate)
@@ -199,28 +200,28 @@ def start_recording(self) -> None:
199200
"subject_ID": self.subject_id,
200201
"camera_unique_id": self.settings.unique_id,
201202
"recorded_frames": 0,
203+
"downsampling_factor":self.settings.downsampling_factor,
202204
"begin_time": self.record_start_time.isoformat(timespec="milliseconds"),
203205
"end_time": None,
204206
}
205207
with open(self.metadata_filepath, "w") as meta_data_file:
206208
json.dump(self.metadata, meta_data_file, indent=4)
207209

208210
# Initalise ffmpeg process
209-
downsampled_width = int(self.cam_width / self.settings.downsampling_factor)
210-
downsampled_height = int(self.cam_height / self.settings.downsampling_factor)
211+
self.downsampled_width = int(self.camera_api.get_width() / self.settings.downsampling_factor)
212+
self.downsampled_height = int(self.camera_api.get_height() / self.settings.downsampling_factor)
211213
ffmpeg_command = " ".join(
212214
[
213215
self.ffmpeg_path, # Path to binary
214-
"-y", # overwrite output file if it exists
215216
"-f rawvideo", # Input codec (raw video)
216-
"-pix_fmt gray", # Input Pixel format?
217-
f"-s {downsampled_width}x{downsampled_height}", # Input resolution
217+
"-pix_fmt gray", # Input Pixel Format: 8-bit grayscale input to ffmpeg process. Input array 1D
218+
f"-s {self.downsampled_width}x{self.downsampled_height}", # Input resolution
218219
f"-r {self.settings.fps}", # Frame rate
219220
"-i -", # input comes from a pipe (stdin)
220-
f"-vcodec {self.video_capture_tab.ffmpeg_encoder_map[ffmpeg_config['compression_standard']]}", # Output codec
221+
f"-c:v {self.video_capture_tab.ffmpeg_encoder_map[ffmpeg_config['compression_standard']]}", # Output codec
221222
"-pix_fmt yuv420p", # Output pixel format
222223
f"-preset {ffmpeg_config['encoding_speed']}", # Encoding speed [fast, medium, slow]
223-
f"-crf {ffmpeg_config['crf']}", # Controls quality vs filesize
224+
f"-qp {ffmpeg_config['crf']}", # Controls quality vs filesize
224225
f'"{self.video_filepath}"', # Output file path
225226
]
226227
)
@@ -253,6 +254,7 @@ def stop_recording(self) -> None:
253254
# Close FFMPEG process
254255
self.ffmpeg_process.stdin.close()
255256
self.ffmpeg_process.wait()
257+
256258
# Update GUI
257259
self.stop_recording_button.setEnabled(False)
258260
self.start_recording_button.setEnabled(True)

GUI/icons/refresh.svg

+3
Loading

config/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
ffmpeg_config = {
1616
# "pxl_fmt": {"Mono8": "yuv420p", "Mono16": "?"},
17-
"crf": 23, # between 1 - 51
17+
"crf": 23, # between 1 - 51 https://slhck.info/video/2017/02/24/crf-guide.html
1818
"encoding_speed": "slow", # ["fast", "medium", "slow"]
19-
"compression_standard": "h265", # ["h265" , "h264"]
19+
"compression_standard": "h264", # ["h265" , "h264"]
2020
}
2121

2222
# Paths -------------------------------------------------------------------------------

0 commit comments

Comments
 (0)