This service records CCTV footage from an RTSP stream, saves clips, and uploads them to Google Drive. It uses a modular architecture with separate recorder and uploader services that communicate via MQTT.
The system consists of two main services:
- Recorder Service: Records CCTV footage from RTSP stream and stores metadata in SQLite database
- Uploader Service: Monitors for new recordings and uploads them to Google Drive
The services communicate via MQTT messages and share a SQLite database to track recording status.
The application uses a simple migration system to manage database schema changes. Migrations are applied automatically when the application starts.
- Migrations are stored as JavaScript files in the
db/migrationsdirectory - Each migration has a unique ID, name, and SQL statement
- Migrations are applied in order of their IDs
- Applied migrations are tracked in the
migrationstable - Only new migrations that haven't been applied yet will run
To add a new migration:
- Create a new file in the
db/migrationsdirectory with formatXX-description.jswhere XX is the next sequential number - The file should export an object with
id,name, andsqlproperties. The migrations will be tracked and applied in the order ofid. - Example:
module.exports = {
id: 3,
name: 'add_new_column',
up: [`
ALTER TABLE recordings
ADD COLUMN new_column TEXT;
`],
down: [`ALTER TABLE recordings
DROP COLUMN new_column TEXT;`]
};-
Go to the Google Cloud Console
-
Create a new project
-
Enable the Google Drive API for your project
-
Create service account credentials:
- Go to "APIs & Services" > "Credentials"
- Click "Create credentials" > "Service account"
- Fill in the service account details
- Grant this service account access to the project (Role: Editor)
- Click "Create key" (JSON format)
- Download the JSON file
-
Rename the downloaded JSON file to
credentials.jsonand place it in the root directory of this project.
An MQTT broker is required for the services to communicate. You can:
-
Use a local broker such as Mosquitto:
# Install on macOS brew install mosquitto # Start the broker mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
Update the MQTT configuration in .env if using a remote broker.
npm install- Create a
.envfile in the project root. Look at the.env.eamplefor the variables that needs to be defined.
You can run both services together or separately.
node index.js
# or
node index.js allnode recorder.js
# or
node index.js recordernode uploader.js
# or
node index.js uploadernode uploaderCron.js-
Recorder Service:
- Records clips from the RTSP stream in parts.
- Stores part recordings in folder and its corresponding metadata in SQLite database.
-
Uploader Service:
- Subscribes to new recording topic.
- Uploads recordings to Google Drive when ever recording is generated.
- Updates the database with upload status and links.
-
Uploader Cron:
- Cron which periodically check the table and process the pending/failed uploads.
- Uploads recordings to Google Drive.
- Updates the database with upload status and links.
-
Cleanup Service:
- Monitors total storage used by recordings and deletes oldest files when the size exceeds the configured threshold.
- Updates the database to mark recordings as DELETED after removal.
Recordings in Google Drive will be organized as:
- CCTV Recordings (main folder)
- 2023-05-01 (date folder)
- segment_2025-06-15_15-40-00.mp4 (segments based on the segment duration from env)
- segment_2025-06-15_16-40-00.mp4
- ...
- 2023-05-02
- ...
- 2023-05-01 (date folder)
- Integrate Docker for local development.
- Add support for other cloud providers for upload.
- Add support for recording from multiple streams.
- Batch upload to google drive keeping in mind the rate limits