From 322f2f3fa7fd9a6dc4aa89ecebeeaee3cccd5828 Mon Sep 17 00:00:00 2001 From: Yanko Aleksandrov Date: Tue, 24 Jun 2025 13:00:46 +0000 Subject: [PATCH] added log to video storage --- installer/sc.sh | 74 ++++++++++++++----- .../video_storage_encoder_with_audio.sh | 74 ++++++++++++++----- 2 files changed, 110 insertions(+), 38 deletions(-) diff --git a/installer/sc.sh b/installer/sc.sh index 01acc42..db4513a 100755 --- a/installer/sc.sh +++ b/installer/sc.sh @@ -1383,29 +1383,20 @@ check_audio_encoder() { fi } -log_message "Starting HLS recording with audio support and 60-minute playlist rotation" - -# Check for audio encoder availability -check_audio_encoder -AUDIO_AVAILABLE=\$? - -# Main recording loop - runs indefinitely -while true; do - # Generate timestamp for this hour's recording - TIMESTAMP=\$(date +%Y%m%d_%H%M%S) +# Function to run GStreamer with logging +run_gstreamer_with_logging() { + local timestamp=\$1 + local audio_available=\$2 - log_message "Creating new playlist: \${TIMESTAMP}_playlist.m3u8" - - if [ \$AUDIO_AVAILABLE -eq 0 ]; then + if [ \$audio_available -eq 0 ]; then log_message "Recording with audio from UDP stream (port 5011)" # Enhanced pipeline with audio support (video + UDP audio stream) - # Try hardware encoding first, fallback to software if it fails timeout 3630 gst-launch-1.0 -e \\ mpegtsmux name=mux ! \\ hlssink playlist-root=file://\$OUTPUT_DIR \\ target-duration=60 playlist-length=60 max-files=0 \\ - playlist-location="\$OUTPUT_DIR/\${TIMESTAMP}_playlist.m3u8" \\ - location="\$OUTPUT_DIR/\${TIMESTAMP}_segment_%05d.ts" \\ + playlist-location="\$OUTPUT_DIR/\${timestamp}_playlist.m3u8" \\ + location="\$OUTPUT_DIR/\${timestamp}_segment_%05d.ts" \\ rtspsrc location=rtsp://${TARGET_IP}:8554/main.264 latency=50 drop-on-latency=true ! \\ rtph264depay ! h264parse ! \\ avdec_h264 ! videoconvert ! \\ @@ -1418,7 +1409,7 @@ while true; do audioconvert ! audioresample ! \\ audio/x-raw,rate=48000,channels=2 ! \\ queue max-size-buffers=200 max-size-time=2000000000 ! \\ - avenc_aac bitrate=128000 ! aacparse ! mux. + avenc_aac bitrate=128000 ! aacparse ! mux. & else log_message "Recording video only (no audio device available)" # Video-only pipeline using software encoding @@ -1434,10 +1425,55 @@ while true; do target-duration=60 \\ playlist-length=60 \\ max-files=0 \\ - playlist-location="\$OUTPUT_DIR/\${TIMESTAMP}_playlist.m3u8" \\ - location="\$OUTPUT_DIR/\${TIMESTAMP}_segment_%05d.ts" + playlist-location="\$OUTPUT_DIR/\${timestamp}_playlist.m3u8" \\ + location="\$OUTPUT_DIR/\${timestamp}_segment_%05d.ts" & fi + local gst_pid=\$! + + # Keep track of logged files to avoid duplicates + local logged_files_list="/tmp/logged_segments_\${timestamp}.txt" + touch "\$logged_files_list" + + # Monitor for new .ts files while GStreamer is running + while kill -0 \$gst_pid 2>/dev/null; do + # Find all current .ts files for this timestamp + for ts_file in "\$OUTPUT_DIR"/\${timestamp}_segment_*.ts; do + if [ -f "\$ts_file" ]; then + local filename=\$(basename "\$ts_file") + # Check if we've already logged this file + if ! grep -q "^\$filename\$" "\$logged_files_list" 2>/dev/null; then + log_message "Created segment file: \$filename" + echo "\$filename" >> "\$logged_files_list" + fi + fi + done + sleep 2 + done + + # Clean up the temporary file + rm -f "\$logged_files_list" + + wait \$gst_pid + return \$? +} + +log_message "Starting HLS recording with audio support and 60-minute playlist rotation" + +# Check for audio encoder availability +check_audio_encoder +AUDIO_AVAILABLE=\$? + +# Main recording loop - runs indefinitely +while true; do + # Generate timestamp for this hour's recording + TIMESTAMP=\$(date +%Y%m%d_%H%M%S) + + log_message "Creating new playlist: \${TIMESTAMP}_playlist.m3u8" + + # Run GStreamer with logging + run_gstreamer_with_logging "\$TIMESTAMP" \$AUDIO_AVAILABLE + # Check if gst-launch exited due to an error EXIT_CODE=\$? if [ \$EXIT_CODE -ne 0 ] && [ \$EXIT_CODE -ne 124 ]; then diff --git a/installer/video_encoders/video_storage_encoder_with_audio.sh b/installer/video_encoders/video_storage_encoder_with_audio.sh index 3b4e10d..4f1551c 100755 --- a/installer/video_encoders/video_storage_encoder_with_audio.sh +++ b/installer/video_encoders/video_storage_encoder_with_audio.sh @@ -26,29 +26,20 @@ check_audio_encoder() { fi } -log_message "Starting HLS recording with audio support and 60-minute playlist rotation" - -# Check for audio encoder availability -check_audio_encoder -AUDIO_AVAILABLE=$? - -# Main recording loop - runs indefinitely -while true; do - # Generate timestamp for this hour's recording - TIMESTAMP=$(date +%Y%m%d_%H%M%S) - - log_message "Creating new playlist: ${TIMESTAMP}_playlist.m3u8" +# Function to run GStreamer with logging +run_gstreamer_with_logging() { + local timestamp=$1 + local audio_available=$2 - if [ $AUDIO_AVAILABLE -eq 0 ]; then + if [ $audio_available -eq 0 ]; then log_message "Recording with audio from UDP stream (port 5011)" # Enhanced pipeline with audio support (video + UDP audio stream) - # Try hardware encoding first, fallback to software if it fails timeout 3630 gst-launch-1.0 -e \ mpegtsmux name=mux ! \ hlssink playlist-root=file://$OUTPUT_DIR \ target-duration=60 playlist-length=60 max-files=0 \ - playlist-location="$OUTPUT_DIR/${TIMESTAMP}_playlist.m3u8" \ - location="$OUTPUT_DIR/${TIMESTAMP}_segment_%05d.ts" \ + playlist-location="$OUTPUT_DIR/${timestamp}_playlist.m3u8" \ + location="$OUTPUT_DIR/${timestamp}_segment_%05d.ts" \ rtspsrc location=rtsp://192.168.144.25:8554/main.264 latency=50 drop-on-latency=true ! \ rtph264depay ! h264parse ! \ avdec_h264 ! videoconvert ! \ @@ -61,7 +52,7 @@ while true; do audioconvert ! audioresample ! \ audio/x-raw,rate=48000,channels=2 ! \ queue max-size-buffers=200 max-size-time=2000000000 ! \ - avenc_aac bitrate=128000 ! aacparse ! mux. + avenc_aac bitrate=128000 ! aacparse ! mux. & else log_message "Recording video only (no audio device available)" # Video-only pipeline using software encoding @@ -77,10 +68,55 @@ while true; do target-duration=60 \ playlist-length=60 \ max-files=0 \ - playlist-location="$OUTPUT_DIR/${TIMESTAMP}_playlist.m3u8" \ - location="$OUTPUT_DIR/${TIMESTAMP}_segment_%05d.ts" + playlist-location="$OUTPUT_DIR/${timestamp}_playlist.m3u8" \ + location="$OUTPUT_DIR/${timestamp}_segment_%05d.ts" & fi + local gst_pid=$! + + # Keep track of logged files to avoid duplicates + local logged_files_list="/tmp/logged_segments_${timestamp}.txt" + touch "$logged_files_list" + + # Monitor for new .ts files while GStreamer is running + while kill -0 $gst_pid 2>/dev/null; do + # Find all current .ts files for this timestamp + for ts_file in "$OUTPUT_DIR"/${timestamp}_segment_*.ts; do + if [ -f "$ts_file" ]; then + local filename=$(basename "$ts_file") + # Check if we've already logged this file + if ! grep -q "^$filename$" "$logged_files_list" 2>/dev/null; then + log_message "Created segment file: $filename" + echo "$filename" >> "$logged_files_list" + fi + fi + done + sleep 2 + done + + # Clean up the temporary file + rm -f "$logged_files_list" + + wait $gst_pid + return $? +} + +log_message "Starting HLS recording with audio support and 60-minute playlist rotation" + +# Check for audio encoder availability +check_audio_encoder +AUDIO_AVAILABLE=$? + +# Main recording loop - runs indefinitely +while true; do + # Generate timestamp for this hour's recording + TIMESTAMP=$(date +%Y%m%d_%H%M%S) + + log_message "Creating new playlist: ${TIMESTAMP}_playlist.m3u8" + + # Run GStreamer with logging + run_gstreamer_with_logging "$TIMESTAMP" $AUDIO_AVAILABLE + # Check if gst-launch exited due to an error EXIT_CODE=$? if [ $EXIT_CODE -ne 0 ] && [ $EXIT_CODE -ne 124 ]; then