This project demonstrates how to record video using the MediaRecorder API, process the data into chunks, and upload the chunks to a specified server in real-time as they are recorded.
- Real-time Video Recording: Records video directly from the user's webcam or mobile device.
- Chunked Uploads: Processes recorded data into 8MB chunks (configurable) for efficient uploads.
- Retry Logic: Retries failed uploads with exponential backoff for resilience.
- Web Locks API Integration: Ensures chunks are uploaded sequentially without overlap.
- Visual Feedback: Logs upload progress, buffer sizes, and errors in a scrolling log area.
- A Mux Direct Upload URL
- A modern browser that supports the MediaRecorder API and
navigator.mediaDevices
.
-
Clone the repository:
git clone https://github.com/muxinc/examples.git cd examples/media-recorder-streaming-uploads
-
Run a basic server to host the example:
npx serve
-
Provide a valid direct upload URL in the input field.
-
Start recording by clicking the Start Recording button.
-
Stop recording by clicking the Stop Recording button.
- The app requests access to the user's webcam and microphone.
- Initializes the MediaRecorder with the appropriate MIME type and bitrate.
- Begins recording data in chunks every 500ms.
mediaRecorder.start(500); // Collect chunks every 500ms
- Each chunk is buffered until the size exceeds CHUNK_SIZE (8MB by default).
- When the buffer is full, the chunk is uploaded to the server.
- If there is remaining data after a chunk upload, it stays in the buffer for the next chunk.
- Chunks are uploaded using PUT requests with Content-Range headers to indicate byte ranges.
- Failed uploads are retried up to 3 times with exponential backoff.
- The Web Locks API is used to ensure that chunks are uploaded sequentially without overlap.