Skip to content

Commit 951a967

Browse files
committed
Add SE_VIDEO_MIN_RECORDING_DURATION to ensure file is not corrupted
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 98b8da6 commit 951a967

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Video/video.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ max_attempts=${SE_VIDEO_WAIT_ATTEMPTS:-50}
1919
file_ready_max_attempts=${SE_VIDEO_FILE_READY_WAIT_ATTEMPTS:-5}
2020
wait_uploader_shutdown_max_attempts=${SE_VIDEO_WAIT_UPLOADER_SHUTDOWN_ATTEMPTS:-5}
2121
graceful_stop_delay=${SE_VIDEO_GRACEFUL_STOP_DELAY:-5}
22+
min_recording_duration=${SE_VIDEO_MIN_RECORDING_DURATION:-5}
2223
ts_format=${SE_LOG_TIMESTAMP_FORMAT:-"%Y-%m-%d %H:%M:%S,%3N"}
2324
process_name="video.recorder"
2425

@@ -243,9 +244,32 @@ function stop_ffmpeg_graceful_async() {
243244
echo "$(date -u +"${ts_format}") [${process_name}] - [Session: ${session_id_param}] FFmpeg graceful stop initiated in background (PID: ${bg_pid}) for file: ${video_file_name_param}"
244245
}
245246

247+
function ensure_minimum_recording_duration() {
248+
# Ensure recording has run for minimum duration to avoid corrupted files
249+
# IMPORTANT: recording_started must remain "true" during this function
250+
# to prevent new sessions from starting (checked at line ~406)
251+
252+
if [[ -n "${recording_start_time}" ]]; then
253+
local current_time=$(date +%s)
254+
local recording_duration=$((current_time - recording_start_time))
255+
256+
if [[ ${recording_duration} -lt ${min_recording_duration} ]]; then
257+
local wait_time=$((min_recording_duration - recording_duration))
258+
echo "$(date -u +"${ts_format}") [${process_name}] - [Session: ${prev_session_id}] Recording duration ${recording_duration}s is less than minimum ${min_recording_duration}s, waiting ${wait_time}s more (new sessions blocked)"
259+
sleep ${wait_time}
260+
echo "$(date -u +"${ts_format}") [${process_name}] - [Session: ${prev_session_id}] Minimum recording duration ${min_recording_duration}s reached, new sessions can start"
261+
else
262+
echo "$(date -u +"${ts_format}") [${process_name}] - [Session: ${prev_session_id}] Recording duration ${recording_duration}s meets minimum ${min_recording_duration}s requirement"
263+
fi
264+
fi
265+
}
266+
246267
function stop_recording() {
247268
local use_async=${1:-true}
248269

270+
# Ensure minimum recording duration before stopping
271+
ensure_minimum_recording_duration
272+
249273
if [[ "${use_async}" = "true" ]]; then
250274
# Async stop - allows immediate start of new recording
251275
# Upload will be triggered AFTER FFmpeg finalization in the background
@@ -276,6 +300,7 @@ function stop_recording() {
276300

277301
recorded_count=$((recorded_count + 1))
278302
recording_started="false"
303+
recording_start_time=""
279304
}
280305

281306
function check_if_ffmpeg_running() {
@@ -373,6 +398,7 @@ else
373398
video_file_name=""
374399
video_file=""
375400
prev_session_id=""
401+
recording_start_time=""
376402
attempts=0
377403
max_recorded_count=${SE_DRAIN_AFTER_SESSION_COUNT:-0}
378404
recorded_count=0
@@ -402,6 +428,7 @@ else
402428
if ps -p $FFMPEG_PID >/dev/null; then
403429
recording_started="true"
404430
prev_session_id=$session_id
431+
recording_start_time=$(date +%s)
405432
fi
406433
echo "$(date -u +"${ts_format}") [${process_name}] - [Session: ${session_id}] Video recording started (FFmpeg PID: ${FFMPEG_PID})"
407434
sleep ${poll_interval}

0 commit comments

Comments
 (0)