A virtual camera is a camera without a live video feed. You can upload video files to a virtual camera, and the Media Server saves the footage to the archive as if a physical camera recorded it.
This guide outlines the general workflow for importing video files to a virtual camera using the Media Server REST API.
For more detailed information and fully functional code examples, check the Nx Open Integrations repository.
Import workflow
The REST API import process consists of the following steps:
Authenticate: Send a
POSTrequest to{server}/rest/v4/login/sessionswith your credentials to obtain a bearer session token.Create the virtual camera: Send a
POSTrequest to{server}/rest/v4/devices/*/virtualwith a camera name. The server creates the virtual camera and returns its uniqueid.Create the upload: Send a
POSTrequest to{server}/rest/v4/devices/{id}/virtual/uploadswith the file metadata (filename, byte size, base64-encoded MD5 hash, and chunk size). The response confirms the upload parameters and returns anuploadId.Upload file chunks: Send sequential
PUTrequests to{server}/rest/v4/devices/{id}/virtual/uploads/{uploadId}?chunk=<n>containing raw binary chunk data (Content-Type: application/octet-stream).Verify upload status: Send a
GETrequest to{server}/rest/v4/devices/{id}/virtual/uploads/{uploadId}to confirm thatuploadProgressPercentreaches100.Lock the virtual camera: Send a
PATCHrequest to{server}/rest/v4/devices/{id}/virtual/lockwith a time-to-live (ttlMs) value. This action prevents concurrent imports and returns a locktoken.Start the import (consume): Send a
PATCHrequest to{server}/rest/v4/devices/{id}/virtual/consumewith theuploadId,token, andstartTimeMsto process the uploaded file into the camera archive.Monitor progress and extend lock: Periodically send a
PATCHrequest to{server}/rest/v4/devices/{id}/virtual/extendwith thetoken. This keeps the lock active and returns the currentprogresspercentage (0–100).Release the lock: Send a
PATCHrequest to{server}/rest/v4/devices/{id}/virtual/releasewith your locktokento free the device for future operations.
Key implementation notes
Time-to-live (
ttlMs): Always implement a lock extension loop during long imports to prevent the camera lock from expiring before the process finishes.Error handling: Always release the lock in a
finallyblock or equivalent error handler, ensuring the virtual device is unlocked even if the upload or import process fails.Video duration: If uploaded footage does not appear on the timeline after a successful import, the server might have failed to extract duration metadata from the video file. To fix this, explicitly provide a
durationMsfield in the file metadata payload during registration.
Code samples and resources
For complete, production-ready code samples with error handling, progress monitoring, and CLI parameters:
Python sample: Review the complete script and execution instructions in the Virtual Camera Upload Sample.
Other languages: Explore JavaScript, Node.js, and browser-based integration examples in the Nx Open Integrations Repository.
Comments
0 comments
Article is closed for comments.