Const
Gets the user's media devices (cameras, microphones, speakers).
Promise that resolves with an array of media devices
const devices = await MediaUtils.getMediaDevices();
devices.forEach(device => console.log(device.kind, device.label));
Requests access to the user's camera.
Media constraints (default: { video: true })
Promise that resolves with a MediaStream
const stream = await MediaUtils.requestCameraAccess();
// Attach to video element
videoElement.srcObject = stream;
Requests access to the user's microphone.
Media constraints (default: { audio: true })
Promise that resolves with a MediaStream
const stream = await MediaUtils.requestMicrophoneAccess();
// Attach to audio element
audioElement.srcObject = stream;
Requests access to the user's screen (screen sharing).
Media constraints (default: { video: true })
Promise that resolves with a MediaStream
const stream = await MediaUtils.requestScreenAccess();
// Attach to video element
videoElement.srcObject = stream;
Creates a MediaRecorder instance for recording a MediaStream.
MediaStream to record
MediaRecorder options
MediaRecorder instance
const recorder = MediaUtils.createMediaRecorder(stream);
recorder.start();
Records a MediaStream to a Blob.
MediaStream to record
MediaRecorder options
Promise that resolves with the recorded Blob
const blob = await MediaUtils.recordMediaStream(stream);
// Download the recording
MediaUtils.downloadBlob(blob, 'recording.webm');
Takes a screenshot of a video element and returns it as a PNG Blob.
Video element to capture
Promise that resolves with the screenshot as a Blob
If the canvas context cannot be obtained
const screenshot = await MediaUtils.takeScreenshot(videoElement);
MediaUtils.downloadBlob(screenshot, 'screenshot.png');
Gets the duration of a video file in seconds.
Video file
Promise that resolves with the duration in seconds
const duration = await MediaUtils.getVideoDuration(videoFile);
console.log(`Duration: ${duration} seconds`);
Gets the dimensions of a video file.
Video file
Promise that resolves with the video dimensions
const { width, height } = await MediaUtils.getVideoDimensions(videoFile);
console.log(`Width: ${width}, Height: ${height}`);
Gets the duration of an audio file in seconds.
Audio file
Promise that resolves with the duration in seconds
const duration = await MediaUtils.getAudioDuration(audioFile);
console.log(`Audio duration: ${duration} seconds`);
Gets the dimensions of an image file.
Image file
Promise that resolves with the image dimensions
const { width, height } = await MediaUtils.getImageDimensions(imageFile);
console.log(`Image: ${width}x${height}`);
Resizes an image file to fit within the specified maximum width and height.
Image file to resize
Maximum width
Maximum height
Promise that resolves with the resized image as a Blob
const resizedImage = await MediaUtils.resizeImage(imageFile, 800, 600);
MediaUtils.downloadBlob(resizedImage, 'resized.jpg');
Compresses an image file to reduce its size.
Image file to compress
Compression quality (0-1, default: 0.8)
Promise that resolves with the compressed image as a Blob
const compressedImage = await MediaUtils.compressImage(imageFile, 0.7);
MediaUtils.downloadBlob(compressedImage, 'compressed.jpg');
Checks if the browser supports a specific media type.