Modern web apps deal with media every day β from playing videos to recording streams and building custom players.
This post walks you through everything you need to play, stream, record, and capture video/audio in the browser β step by step.
π§ What Youβll Learn
By the end of this guide, youβll understand:
- π¬ How to play static or linked video/audio files
- π¨ How to create custom controls and show subtitles
- π‘ How streaming works (HLS/DASH, bitrates, manifests)
- π§ How to manage multiple audio/subtitle tracks
- π₯οΈ How to capture and record media from camera, mic, or screen
- π§ Which file formats and codecs to use and why
- πΌοΈ How to use Canvas to capture video frames
- βοΈ Key HTML attributes and APIs (
<video>,<audio>,MediaRecorder, etc.)
π¬ VIDEO HANDLING
Letβs start with how to play and control video in your web app.
βΆοΈ Playing Video
1. Play Static File
<video src="/assets/sample.mp4" controls></video>
2. Play by Link
<video src="https://example.com/video.mp4" controls></video>
β οΈ CORS Alert:
If your video is hosted on another domain, the server must allow cross-origin access using headers like:Access-Control-Allow-Origin: https://yourwebsite.com
π¨ Control Customization
Show or Hide Controls
<!-- Default browser controls -->
<video src="movie.mp4" controls></video>
<!-- Hide controls (for custom UI) -->
<video src="movie.mp4"></video>
Enable Download
<a href="/assets/video.mp4" download>Download Video</a>
Custom Controls Example
<video id="myVideo" src="video.mp4"></video>
<button onclick="document.getElementById('myVideo').play()">Play</button>
<button onclick="document.getElementById('myVideo').pause()">Pause</button>
π‘ Tip: Support keyboard shortcuts like
Space(play/pause) orβ/β(volume) for accessibility.
π Streaming Video
Streaming allows smooth playback without downloading the entire file upfront.
How It Works
- The backend stores multiple versions of the same video at different bitrates.
- The player fetches small chunks (
.tsor.mp4segments). - Based on network speed, it automatically adjusts quality.
Common Streaming Formats
| Format | Extension | Used For | Frontend Library |
|---|---|---|---|
| HLS | .m3u8 |
Apple devices / most browsers | hls.js |
| MPEG-DASH | .mpd |
Android / Smart TVs | dash.js |
Example (Using HLS.js)
const video = document.getElementById('video');
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource('https://example.com/video.m3u8');
hls.attachMedia(video);
}
π§ Multi-Track Support
Multiple Audio Tracks
- Supported in MP4 and MKV formats.
- Access via the
audioTracksAPI.
Multiple Subtitles
Keep separate .vtt files for each language:
<video controls>
<source src="movie.mp4" type="video/mp4">
<track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
<track src="subtitles_es.vtt" kind="subtitles" srclang="es" label="Spanish">
</video>
π§© Useful Attributes
| Attribute | Description |
|---|---|
poster="thumbnail.jpg" |
Shows a preview image before playback |
preload="none", preload="metadata", preload="auto"
|
Controls how the browser preloads video data |
controlsList="nodownload" |
Disables the default download button in the browserβs video controls |
crossorigin="anonymous" |
Enables cross-origin requests for external video sources (required when using subtitles or CORS-enabled videos) |
ποΈ Video Codecs & Formats
| Format | Codec | Notes |
|---|---|---|
| MP4 | H.264 | Most common, supported everywhere |
| WebM | VP8/VP9 | Open-source, efficient |
| MKV | H.264/HEVC | For multi-track videos |
| AVI | β | Old, rarely used now |
β Best Choice: MP4 (H.264) for broad compatibility and quality balance.
π΅ AUDIO HANDLING
βΆοΈ Playing Audio
1. Static File
<audio src="/assets/song.mp3" controls></audio>
2. By Link
<audio src="https://example.com/song.mp3" controls></audio>
3. Enable Download
<a href="/assets/song.mp3" download>Download Audio</a>
π¨ Custom Controls
<audio id="myAudio" src="song.mp3"></audio>
<button onclick="document.getElementById('myAudio').play()">Play</button>
<button onclick="document.getElementById('myAudio').pause()">Pause</button>
π‘ Streaming Audio
Audio streaming also uses HLS or DASH formats.
- Backend stores multiple bitrates.
- Player adapts based on bandwidth.
- You can include lyrics/subtitles using
<track>.
<audio controls>
<source src="song.mp3" type="audio/mp3">
<track src="lyrics_en.vtt" kind="subtitles" srclang="en" label="English">
</audio>
π§ Common Audio Formats
| Format | Type | Use Case |
|---|---|---|
| MP3 | Lossy | Most supported |
| AAC / M4A | Lossy | Better compression, ideal for streaming |
| OGG | Open-source | Not supported on Safari |
| FLAC | Lossless | High quality, large size |
β Preferred: MP3 (for support) or AAC (for quality).
π‘ STREAMING OVERVIEW
Hereβs how adaptive streaming works behind the scenes π
- The backend stores video/audio in different bitrates.
- The player downloads small chunks as per current network speed.
- The manifest file (
.m3u8or.mpd) defines all available versions. - Subtitles and audio tracks are synced via timestamps in the manifest.
π₯ RECORDING & CAPTURING MEDIA
Modern browsers let you capture or record directly β no plugins required.
πΈ Capture Photo (Webcam + Canvas Frame)
You can capture a live photo (frame) from a video stream using the <canvas> element.
<video id="camera" autoplay playsinline width="400" height="300"></video>
<canvas id="snapshot" width="400" height="300"></canvas>
<button id="capture">πΈ Capture Photo</button>
<script>
// Step 1: Start webcam stream
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
document.getElementById('camera').srcObject = stream;
})
.catch(err => console.error('Camera access denied:', err));
// Step 2: Capture a frame on click
document.getElementById('capture').addEventListener('click', () => {
const video = document.getElementById('camera');
const canvas = document.getElementById('snapshot');
const ctx = canvas.getContext('2d');
// Draw current frame on canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Optional: Convert to image
const imageData = canvas.toDataURL('image/png');
console.log('Captured frame:', imageData);
// Download captured frame
const link = document.createElement('a');
link.href = imageData;
link.download = 'photo.png';
link.click();
});
</script>
π§ How it works:
- The
<video>displays live feed.<canvas>takes a snapshot usingdrawImage().- You can save it, preview it, or send to your backend.
π₯ Capture Video + Audio
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
document.querySelector('video').srcObject = stream;
});
π₯οΈ Capture Screen or Tab
navigator.mediaDevices.getDisplayMedia({ video: true, audio: true })
.then(stream => {
document.querySelector('video').srcObject = stream;
});
π΄ Record the Stream
const recorder = new MediaRecorder(stream);
const chunks = [];
recorder.ondataavailable = e => chunks.push(e.data);
recorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
const url = URL.createObjectURL(blob);
document.querySelector('video').src = url;
};
recorder.start();
π Sending or Combining Streams
- Send as Stream: Use WebRTC or WebSocket for live transfer.
- Send as File: Upload the recorded Blob/File to backend.
-
Combine Screen + Video: Capture both and merge via Canvas or
MediaStreamTrack.
π§ Final Notes
β
Play local and linked video/audio
β
Build custom players and controls
β
Stream using HLS/DASH
β
Manage subtitles and multi-audio tracks
β
Capture frames using Canvas
β
Record from webcam, mic, or screen
β
Choose the right formats and codecs
π‘ Bonus Tips
- Use
preload="metadata"for faster page loads - Whitelist domains for CORS when using video links
- Use CDNs for smooth video delivery
- Test across browsers and devices
- Use Canvas for filters or real-time frame capture
π Quick Summary
| Topic | API/Tag | Description |
|---|---|---|
| Video Playback | <video> |
Play, control, preload, poster |
| Audio Playback | <audio> |
Play with captions or custom UI |
| Streaming | HLS/DASH | Adaptive bitrate playback |
| Subtitles | <track> |
For multiple languages |
| Capture | getUserMedia() |
Access webcam/mic |
| Screen Share | getDisplayMedia() |
Capture tab/window |
| Frame Capture | <canvas> |
Snapshot from video stream |
| Recording | MediaRecorder |
Record and export video/audio |
π Wrap-Up
Building rich media experiences in the browser is easier than ever.
With HTML5 media elements and powerful APIs like MediaRecorder, getUserMedia, and <canvas>, you can create experiences similar to YouTube or Zoom β right inside your web app.
Top comments (0)