Here, I’ve used Angular’s MediaRecorder to record a video. To do so, follow the instructions below for an example.
Step 1: Create a new Project
Create a new project by using below command.
ng new recordvideodemo
Step 2 : Install Module
Install media capture by following command.
npm i @types/dom-mediacapture-record
Step 3 : Import MeadiaRecorder
Now that the MediaRecorder variable has been declared, you must import a few fundamental packages from @angular/core into the component where you intend to utilize it. Update app.component.ts file by pasting below code.
import { Component, VERSION, ViewChild, OnInit, ElementRef } from '@angular/core'; declare var MediaRecorder: any; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { @ViewChild('recordedVideo') recordVideoElementRef: ElementRef; @ViewChild('video') videoElementRef: ElementRef; videoElement: HTMLVideoElement; recordVideoElement: HTMLVideoElement; mediaRecorder: any; recordedBlobs: Blob[]; isRecording: boolean = false; downloadUrl: string; stream: MediaStream; constructor() {} async ngOnInit() { navigator.mediaDevices .getUserMedia({ video: { width: 360 } }) .then(stream => { this.videoElement = this.videoElementRef.nativeElement; this.recordVideoElement = this.recordVideoElementRef.nativeElement; this.stream = stream; this.videoElement.srcObject = this.stream; }); } startRecording() { this.recordedBlobs = []; let options: any = { mimeType: 'video/webm' }; try { this.mediaRecorder = new MediaRecorder(this.stream, options); } catch (err) { console.log(err); } this.mediaRecorder.start(); // collect 100ms of data this.isRecording = !this.isRecording; this.onDataAvailableEvent(); this.onStopRecordingEvent(); } stopRecording() { this.mediaRecorder.stop(); this.isRecording = !this.isRecording; console.log('Recorded Blobs: ', this.recordedBlobs); } playRecording() { if (!this.recordedBlobs || !this.recordedBlobs.length) { console.log('cannot play.'); return; } this.recordVideoElement.play(); } onDataAvailableEvent() { try { this.mediaRecorder.ondataavailable = (event: any) => { if (event.data && event.data.size > 0) { this.recordedBlobs.push(event.data); } }; } catch (error) { console.log(error); } } onStopRecordingEvent() { try { this.mediaRecorder.onstop = (event: Event) => { const videoBuffer = new Blob(this.recordedBlobs, { type: 'video/webm' }); this.downloadUrl = window.URL.createObjectURL(videoBuffer); // you can download with <a> tag this.recordVideoElement.src = this.downloadUrl; }; } catch (error) { console.log(error); } } }
In this section, I’ve written a few functions related to our video recording capabilities. If you want to try out other mime types, feel free to do so in accordance with your needs.
Step 4: Update app.component.html
Copy below code & paste it to html file.
<div style="text-align:center"> <video class="video" #video autoplay controls></video> <span class="m-1"></span> <video class="video" style="width:360 !important;" controls #recordedVideo></video> <br> <button class="btn btn-primary btn-lg" *ngIf="!isRecording" (click)="startRecording()">Start Recording</button> <button class="btn btn-warning btn-lg" *ngIf="isRecording" (click)="stopRecording()">Stop Recording</button> </div>
Step 5 : Run application
Run the project using following command.
ng server // npm start