Fix recording

This commit is contained in:
proitlab committed 2026-07-29 07:57:39 +07:00
1 parent e92857c6a1
commit 5eb122f422
8 files changed
+1787 -41

No files matched your search

+55 -24
View File
@@ -11,9 +11,11 @@
VideoBatchWriter::VideoBatchWriter(const std::string& shm_dir,
const std::string& output_dir,
int w, int h, int fps,
float delay_sec)
float delay_sec,
float end_delay_sec)
: shm_dir_(shm_dir), output_dir_(output_dir),
w_(w), h_(h), fps_(fps), delay_sec_(delay_sec)
w_(w), h_(h), fps_(fps),
delay_sec_(delay_sec), end_delay_sec_(end_delay_sec)
{
ensure_dir(shm_dir_);
ensure_dir(output_dir_);
@@ -33,51 +35,69 @@ void VideoBatchWriter::ensure_dir(const std::string& dir) {
std::filesystem::create_directories(dir);
}
void VideoBatchWriter::on_new_batch(int batch_number) {
current_batch_ = batch_number;
delaying_ = true;
delay_start_ = std::chrono::steady_clock::now();
void VideoBatchWriter::note_motion() {
if (state_ == IDLE) {
start_recording();
state_ = SPECULATING;
state_start_ = std::chrono::steady_clock::now();
}
}
void VideoBatchWriter::note_ayam_detection() {
if (state_ == SPECULATING) {
state_ = CONFIRMED;
}
}
void VideoBatchWriter::on_batch_closed(int batch_number) {
if (recording_ && current_batch_ == batch_number) {
stop_and_move(batch_number);
current_batch_ = batch_number;
if (state_ == CONFIRMED) {
state_ = STOPPING;
state_start_ = std::chrono::steady_clock::now();
}
delaying_ = false;
recording_ = false;
current_batch_ = 0;
}
void VideoBatchWriter::feed_frame(const cv::Mat& raw) {
if (delaying_) {
auto elapsed = std::chrono::duration<float>(
std::chrono::steady_clock::now() - delay_start_).count();
auto now = std::chrono::steady_clock::now();
if (state_ == SPECULATING) {
float elapsed = std::chrono::duration<float>(now - state_start_).count();
if (elapsed >= delay_sec_) {
delaying_ = false;
start_recording(current_batch_);
stop_and_delete();
state_ = IDLE;
return;
}
}
if (recording_ && writer_.isOpened()) {
if (state_ == STOPPING) {
float elapsed = std::chrono::duration<float>(now - state_start_).count();
if (elapsed >= end_delay_sec_) {
stop_and_move(current_batch_);
state_ = IDLE;
return;
}
}
if (state_ != IDLE && writer_.isOpened()) {
writer_.write(raw);
}
}
void VideoBatchWriter::start_recording(int batch_number) {
void VideoBatchWriter::start_recording() {
if (writer_.isOpened()) writer_.release();
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::ostringstream name;
name << "batch_" << batch_number << "_"
name << "batch_pending_"
<< std::put_time(std::localtime(&t), "%Y%m%d_%H%M%S") << ".mp4";
current_path_ = shm_dir_ + "/" + name.str();
writer_.open(current_path_,
cv::VideoWriter::fourcc('a','v','c','1'),
fps_, cv::Size(w_, h_));
recording_ = writer_.isOpened();
if (recording_) {
std::cout << "[RECORD] Started: " << current_path_ << std::endl;
if (writer_.isOpened()) {
std::cout << "[RECORD] Speculating: " << current_path_ << std::endl;
} else {
std::cerr << "[RECORD] Failed to open: " << current_path_ << std::endl;
}
@@ -97,6 +117,18 @@ static void copy_and_clean(const std::string& src, const std::string& dst) {
std::filesystem::remove(src, ec);
}
void VideoBatchWriter::stop_and_delete() {
if (writer_.isOpened()) {
writer_.release();
}
if (!current_path_.empty()) {
std::cout << "[RECORD] Discarding: " << current_path_ << std::endl;
std::error_code ec;
std::filesystem::remove(current_path_, ec);
current_path_.clear();
}
}
void VideoBatchWriter::stop_and_move(int batch_number) {
if (writer_.isOpened()) {
writer_.release();
@@ -126,8 +158,7 @@ void VideoBatchWriter::shutdown() {
if (writer_.isOpened()) {
writer_.release();
}
recording_ = false;
delaying_ = false;
state_ = IDLE;
if (!current_path_.empty()) {
std::string src = current_path_;