“Live” video

Author:

it’s not “live,” exactly, but this system captures video and audio for Scootercam

Reolink On-Demand Video Capture

A Python script for capturing 10-second live video clips with audio from Reolink IP cameras. Designed for on-demand use cases like motion detection triggers, security alerts, or manual surveillance recording.

Overview

This script connects to a Reolink camera via RTSP stream, records 10 seconds of live video with audio, processes it into a web-optimized MP4 file, and automatically uploads it to an FTP server. Perfect for security systems, wildlife monitoring, or any scenario requiring quick video capture.

Features

  • Live RTSP Capture: Records directly from camera’s live video stream
  • Audio Recording: Captures synchronized audio along with video
  • Automatic Processing: Converts to web-optimized MP4 format
  • FTP Upload: Automatically uploads to remote server
  • Error Handling: Comprehensive connectivity and validation checks
  • File Management: Automatic cleanup of temporary and old files
  • Fast Execution: Typical runtime 15-20 seconds total

Requirements

Hardware

  • Raspberry Pi or Linux system with network access to camera
  • Reolink IP camera with RTSP stream support
  • Internet connection for FTP uploads
  • Minimum 500MB free disk space

Software Dependencies

  • Python 3.6+
  • ffmpeg
  • curl
  • ping utility

Installation

1. Install System Dependencies

Raspberry Pi/Debian/Ubuntu:

bash

sudo apt update
sudo apt install python3 ffmpeg curl iputils-ping

CentOS/RHEL:

bash

sudo yum install python3 ffmpeg curl iputils

2. Download Script

bash

wget https://your-repo/reolink_video_capture.py
# or
curl -O https://your-repo/reolink_video_capture.py

3. Make Executable

bash

chmod +x reolink_video_capture.py

4. Create Output Directory

bash

sudo mkdir -p /var/www/html/mp
sudo chown $USER:$USER /var/www/html/mp

Configuration

Camera Settings

Edit the script variables at the top:

python

# Reolink Camera Configuration
REOLINK_USER = "admin"
REOLINK_PASS = "your_camera_password"
REOLINK_IP = "192.168.1.211"
REOLINK_PORT = "554"

# FTP Server Configuration  
FTP_USER = "your_ftp_username"
FTP_PASS = "your_ftp_password"
FTP_HOST = "your_ftp_server"

Security Configuration (Recommended)

Option 1: Environment Variables

bash

# Add to ~/.bashrc or ~/.profile
export REOLINK_PASS="your_camera_password"
export FTP_PASS="your_ftp_password"

# Modify script to use:
REOLINK_PASS = os.environ.get('REOLINK_PASS', 'default_password')
FTP_PASS = os.environ.get('FTP_PASS', 'default_password')

Option 2: Config File

bash

# Create ~/.camera_config.py
REOLINK_PASS = "your_camera_password"
FTP_PASS = "your_ftp_password"

# Import in main script:
try:
    from camera_config import *
except ImportError:
    pass  # Use defaults

Camera RTSP URL Formats

Different Reolink models use various RTSP paths. The script defaults to:

rtsp://user:pass@ip:554/h264Preview_01_main

If this doesn’t work, try these alternatives:

Stream TypeRTSP PathMain Stream (High Quality)/h264Preview_01_mainSub Stream (Lower Quality)/h264Preview_01_subAlternative Main/Preview_01_mainSimple Path/live

Test your RTSP URL manually:

bash

ffmpeg -i "rtsp://admin:password@192.168.1.211:554/h264Preview_01_main" -t 5 test.mp4

Usage

Basic Usage

bash

python3 reolink_video_capture.py

Command Line Integration

bash

# Add to PATH for easy access
sudo cp reolink_video_capture.py /usr/local/bin/capture-video
sudo chmod +x /usr/local/bin/capture-video

# Run from anywhere:
capture-video

Integration Examples

Motion Detection Trigger:

bash

# Add to motion detection script
if motion_detected; then
    python3 /path/to/reolink_video_capture.py
    notify_admin "Motion detected - video captured"
fi

Cron Job for Regular Captures:

bash

# Every hour during business hours
0 9-17 * * 1-5 /usr/bin/python3 /home/pi/reolink_video_capture.py

# Daily at midnight
0 0 * * * /usr/bin/python3 /home/pi/reolink_video_capture.py

Web Interface Button:

html

<!-- Simple web button -->
<button onclick="fetch('/cgi-bin/capture-video.py')">Capture Video</button>

Home Assistant Integration:

yaml

# configuration.yaml
shell_command:
  reolink_capture: "python3 /home/pi/reolink_video_capture.py"

# Use in automations or scripts
- service: shell_command.reolink_capture

File Output

Local Storage

  • Temporary files/tmp/reolink_capture_TIMESTAMP.mp4
  • Final files/var/www/html/mp/live_TIMESTAMP.mp4
  • Naming formatlive_YYYYMMDDHHMMSS.mp4

Remote Storage

  • FTP locationftp://server/live/live_TIMESTAMP.mp4
  • Web accessible: Depends on your FTP server configuration

Example Filenames

live_20240315143022.mp4  # March 15, 2024 at 2:30:22 PM
live_20240315143055.mp4  # March 15, 2024 at 2:30:55 PM

Video Specifications

  • Duration: 10 seconds
  • Resolution: 1280×720 (720p)
  • Video Codec: H.264
  • Audio Codec: AAC at 128 kbps
  • Quality: CRF 23 (high quality)
  • Frame Rate: Camera’s native rate (usually 15-30 fps)
  • File Size: Typically 2-8 MB depending on scene complexity

Monitoring & Logs

Real-time Monitoring

bash

# Monitor script execution
python3 reolink_video_capture.py

# Watch output directory
watch ls -la /var/www/html/mp/live_*.mp4

Log Output Example

[2024-03-15 14:30:22] === Starting on-demand Reolink video capture ===
[2024-03-15 14:30:22] Checking camera connectivity...
[2024-03-15 14:30:23] Camera is reachable
[2024-03-15 14:30:23] Starting 10-second video capture...
[2024-03-15 14:30:35] Video capture completed successfully
[2024-03-15 14:30:35] Video file created: live_20240315143022.mp4 (3248576 bytes)
[2024-03-15 14:30:35] Uploading live_20240315143022.mp4 to FTP server...
[2024-03-15 14:30:38] Upload successful: live_20240315143022.mp4
[2024-03-15 14:30:38] Cleanup completed
[2024-03-15 14:30:38] === On-demand capture completed ===

Troubleshooting

Common Issues

“Camera is not reachable”

  • Verify IP address and network connectivity
  • Check camera power and network connection
  • Test: ping 192.168.1.211

“ffmpeg failed” or RTSP connection errors

  • Verify camera credentials
  • Check RTSP port (554) isn’t blocked
  • Try alternative RTSP paths (see configuration section)
  • Test manually: ffmpeg -i "rtsp://user:pass@ip:554/h264Preview_01_main" -t 5 test.mp4

“Video file too small” or corrupt videos

  • Camera may not support audio – try video-only capture
  • Network bandwidth issues during capture
  • Try lower quality settings: -crf 28

Upload failures

  • Verify FTP server credentials and accessibility
  • Check internet connectivity
  • Test manual upload: curl -T test.mp4 ftp://server/ --user user:pass

Permission errors

bash

# Fix directory permissions
sudo chown -R $USER:$USER /var/www/html/mp/
sudo chmod 755 /var/www/html/mp/

Debug Mode

Enable verbose ffmpeg output:

python

# Remove 'capture_output=True' and add '-v verbose'
ffmpeg_cmd = [
    'ffmpeg', '-v', 'verbose',
    # ... rest of command
]
result = subprocess.run(ffmpeg_cmd, timeout=30)

Test RTSP stream:

bash

# Test if stream is accessible
ffprobe "rtsp://admin:password@192.168.1.211:554/h264Preview_01_main"

# Test 5-second capture
ffmpeg -i "rtsp://admin:password@192.168.1.211:554/h264Preview_01_main" -t 5 test.mp4

Customization

Capture Duration

python

'-t', '15',  # Change from 10 to 15 seconds

Video Quality

python

# Higher quality (larger files):
'-crf', '18',

# Lower quality (smaller files):
'-crf', '28',

# Different resolution:
'-vf', 'scale=1920x1080:flags=lanczos',  # 1080p
'-vf', 'scale=640x480:flags=lanczos',    # 480p

Audio-Only or Video-Only

python

# Video only (no audio):
'-an',  # Add this flag

# Audio only:
'-vn',  # Add this flag

Different Upload Locations

python

# Different FTP directory
f'ftp://{FTP_HOST}/live/'       # Current default
f'ftp://{FTP_HOST}/ss/'         # Timelapse directory
f'ftp://{FTP_HOST}/security/'   # For security alerts  
f'ftp://{FTP_HOST}/alerts/'     # For motion alerts
f'ftp://{FTP_HOST}/manual/'     # For manual captures

Integration Patterns

Motion Detection System

bash

#!/bin/bash
# motion-alert.sh
echo "Motion detected at $(date)"
python3 /home/pi/reolink_video_capture.py
notify-send "Security Alert" "Motion detected - video captured"

Web API Endpoint

python

# Simple Flask app
from flask import Flask
import subprocess

app = Flask(__name__)

@app.route('/capture')
def capture_video():
    result = subprocess.run(['python3', 'reolink_video_capture.py'])
    return f"Capture {'successful' if result.returncode == 0 else 'failed'}"

Scheduled Captures with Variation

bash

# Random captures during business hours
*/15 9-17 * * 1-5 [ $((RANDOM % 4)) -eq 0 ] && python3 /home/pi/reolink_video_capture.py

Performance Notes

  • Typical execution time: 15-20 seconds
  • Network usage: ~3-8 MB download + upload
  • CPU usage: Moderate during ffmpeg processing
  • Disk usage: ~2-8 MB per video file

Security Considerations

  • Keep camera and FTP credentials secure
  • Consider using HTTPS/FTPS for uploads if available
  • Regularly rotate passwords
  • Monitor for unauthorized access attempts
  • Use network segmentation for camera traffic

Compatibility

Tested With:

  • Reolink RLC-410, RLC-520, RLC-810A series
  • Raspberry Pi 3B+, 4B
  • Ubuntu 20.04+, Raspberry Pi OS

Known Issues:

  • Some older Reolink models may require different RTSP paths
  • Audio capture may not work on all camera models
  • Very old firmware may have RTSP authentication issues

Advanced Usage

Batch Capture

python

# Modify main() to capture multiple clips
for i in range(5):
    video_file = capture_video()
    time.sleep(30)  # 30-second intervals

Quality Profiles

python

# Add command line arguments for different quality presets
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--quality', choices=['low', 'medium', 'high'], default='medium')

Integration with Existing Timelapse System

bash

# Add to your dual camera timelapse script
if motion_detected; then
    python3 reolink_video_capture.py
fi

This script complements your existing timelapse system perfectly – use the timelapse for long-term monitoring and this script for immediate incident recording!

Leave a Reply

Your email address will not be published. Required fields are marked *